Question:
This is driving me crazy — I have been working on this problem for several days with little success. I’ve finally hit a brick wall and need help. A lot of the articles and forums that I have searched are not for AWSSDK for PHP 2.
We have been using Amazon’s S3 to load images through iOS for the past couple of years.
Now, I need to implement the upload in a browser.
I have downloaded and successfully installed AWSSDK for PHP 2 on our Ubuntu server. I can get connected to our AWS S3 account and display the contents of the buckets. But I am unable to put an image in a bucket.
The Exception from AWS is:
Aws\S3\Exception\NotImplementedException: AWS Error Code: NotImplemented, Status Code: 501, AWS Request ID: CEDC4BBAA83CF70C, AWS Error Type: server, AWS Error Message: A header you provided implies functionality that is not implemented.
Here is the URL that I got the below example code from, under the heading called Uploading a File to Amazon S3: https://github.com/aws/aws-sdk-php#quick-start
And I updated my code based on this: AWS PHP SDK Version 2 S3 putObject Error
But it still doesn’t work.
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
require_once("../config.php"); //local config vars require_once('AWSSDKforPHP/aws.phar'); use Aws\S3\S3Client; use Aws\Common\Enum\Region; use Aws\Common\Aws; use Aws\S3\Enum\CannedAcl; use Aws\S3\Exception\S3Exception; use Guzzle\Http\EntityBody; //get the $s3 object $config = array( 'key' => AMAZON_ACCESS_KEY, 'secret' => AMAZON_ACCESS_SECRET, 'region' => Region::US_EAST_1 ); $s3 = S3Client::factory($config); try { $bucketname = 'my_bucket_name'; //my bucket name on s3 $filename = 'filename.jpg'; //my image on my server $path = 'http://my.website.com/app/cache/remote'; //the path where the image is located $fullfilename = $path."/".$filename; //this successfully lists the contents of the bucket I am interested in foreach ($s3->getIterator('ListBuckets') as $bucket) { foreach ($s3->getIterator('ListObjects', array('Bucket' => $bucket['Name'])) as $object) { if ( $bucket['Name'] == $bucketname ) { echo $bucket['Name'] . '/' . $object['Key'] . PHP_EOL; } } } //HERE ME HERE, PLEASE! this is the code that throws the exception $s3->putObject(array( 'Bucket' => $bucketname, 'Key' => $filename, 'Body' => EntityBody::factory(fopen($fullfilename, 'r')), 'ACL' => CannedAcl::PUBLIC_READ_WRITE, 'ContentType' => 'image/jpeg' )); } catch (S3Exception $e) { echo $e; } ?> |
Can someone please provide me with an example so I can upload the JPG image into our bucket on S3 using AWSSDK for PHP 2?
RESOLUTION:
From ppostma1’s reply, I have modified my code as follows, and it now works:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$bucketname = 'my_bucket_name'; //must be all lowercase $filename = 'filename.jpg'; //my image on my server $path = 'var/www/html/root-website-folder/images/'; //the physical path where the image is located $fullfilename = $path.$filename; $s3->putObject(array( 'Bucket' => $bucketname, 'Key' => $filename, 'Body' => EntityBody::factory(fopen($fullfilename, 'r')), 'ACL' => CannedAcl::PUBLIC_READ_WRITE, 'ContentType' => 'image/jpeg' )); |
Answer:
First, you are missing a s3 ‘filename’ aka key:
1 2 |
'Key' => '/files/imgage/fuzzykitten.jpg' |
Next, I have had far fewer complications with:
1 2 3 4 5 6 7 8 9 10 |
//use Aws\S3\S3Client; use Aws\Common\Enum\Region; //use Aws\Common\Aws; use Aws\S3\Enum\CannedAcl; use Aws\S3\Exception\S3Exception; use Guzzle\Http\EntityBody; $amazon = Aws\S3\S3Client::factory($config) |
with being able to find the class files. Every time I try to include ./common/aws or ./s3/s3client, it start giving me “cannot find Aws\S3\Aws\S3Client” which leaves me at wt???