Q:

Upload file to AWS S3 using php

I am trying to create a php script which will be able to upload a text file to my ASW S3 bucket.
I have tried the method which is there on AWS site but sadly that ain't proper, I mean it's not end to end.
I have installed the AWS PHP SDK on my instance.
Then I did what's written in the sample code i.e. 
<?php
    use Aws\S3\S3Client;

$bucket = 'cst';
$keyname = 'sampleUpload';
// $filepath should be absolute path to a file on disk                      
$filepath = '/var/www/html/po/si/mag/sahara.txt';

// Instantiate the client.
$s3 = S3Client::factory();

// Upload a file.
$result = $s3->putObject(array(
    'Bucket'       => $bucket,
    'Key'          => $keyname,
    'SourceFile'   => $filepath,
    'ContentType'  => 'text/plain',
    'ACL'          => 'public-read',
    'StorageClass' => 'REDUCED_REDUNDANCY',
    'Metadata'     => array(    
        'param1' => 'value 1',
        'param2' => 'value 2'
    )
));

echo $result['ObjectURL'];

?>

Obviously, I haven't added the aws key nor the aws secret key so it won't work. But then nothing is specified in the tutorial either. So am kinda lost.
Secondly, I tried using this code :
It's also not working.
Thirdly, I tried this article.
It's working when am using it with html but I am not really able to create a php only script where I can just specify the file location, and the file get's uploaded to the server.
Any help is highly appreciated. I searched a lot, but couldn't find anything useful.

A:

Just a guess, but did you add your credentials inside your HTML code using hidden inputs? Cause I just had a very quick look at this page: https://aws.amazon.com/articles/1434/ and it seems like you can set your credentials using HTML. And my guess is the class will automatically take care of that.
If my guess is right, you do need to add the credentials to your instance:
// Instantiate the client.
$s3 = S3Client::factory();

like
// Instantiate the client.
$s3 = S3Client::factory(array(
  'version' => 'latest',
  'region'  => 'us-west-2', //add correct region
  'credentials' => array(
    'key' => <YOUR_AWS_KEY>,
    'secret'  => <YOUR_AWS_SECRET>
  )
));

It probably depends on the version of the sdk you're using, whether you need above mentioned code or this one (notice the missing credentials array):
// Instantiate the client.
$s3 = S3Client::factory(array(
  'version' => 'latest',
  'region'  => 'us-west-2', //add correct region
  'key' => <YOUR_AWS_KEY>,
  'secret'  => <YOUR_AWS_SECRET>
));

EDIT:
Just to show what exactly worked in my case, this is my complete code. The path I executed:
http://myurl.com/index.php?path=./test.txt
code:
require __DIR__ . '/vendor/autoload.php';

use Aws\S3\S3Client;

$bucket = 'sdl-images';
$keyname = '*** Your Object Key ***';
// $filepath should be absolute path to a file on disk                      
$filepath = $_GET['path'];

// Instantiate the client.
$s3 = S3Client::factory(array(
  'version' => 'latest',
  'region'  => <YOUR_REGION E.G. eu-west-1>,
  'credentials' => array(
    'key' => <YOUR_AWS_KEY>,
    'secret'  => <YOUR_AWS_SECRET>
  )
));

// Upload a file.
$result = $s3->putObject(array(
    'Bucket'       => $bucket,
    'Key'          => $keyname,
    'SourceFile'   => $filepath,
    'ContentType'  => 'text/plain',
    'ACL'          => 'public-read',
    'StorageClass' => 'REDUCED_REDUNDANCY'
));

echo $result['ObjectURL'];

