Friday, February 10, 2017

Storage::get( ) using Amazon S3 returns false

Leave a Comment

Combining both Intervention Image and Amazon S3, I'd like to be able to pull a file from S3 and then use Image to do some cropping. This is what I have so far, why does Storage::get() return false?

$path = 'uploads/pics/123.jpeg';  $exists = Storage::disk('s3')->exists($path); // returns true  $image = Storage::disk('s3')->get($path);     // returns false 

From the S3 side of things, the bucket permissions are set to 'Everyone', the Storage::getVisibility() returns public... I'm not sure why I can't load the image as if it were a local image.

3 Answers

Answers 1

After digger deeper into the code I found this message

"Error executing "GetObject" on "file"; AWS HTTP error: file_exists(): open_basedir restriction in effect. File(/etc/pki/tls/certs/ca-bundle.crt) is not within the allowed path(s): (paths)" 

First it seems that my server don't have this file, but it have! The file is located in another folder.

/etc/ssl/certs/ca-certificates.crt 

So, to solve my problem in Ubuntu I have to create this folder /etc/pki/tls/certs and after that, symlink to the correct file:

cd /etc/pki/tls/certs; sudo ln -s /etc/ssl/certs/ca-certificates.crt ca-bundle.crt; 

Edit your php.ini and add /etc/pki/tls/certs/ca-bundle.crt to the open_basedir configuration.

Restart your php server!

For me it solves the problem, hope it helps!

Answers 2

From the Amazon S3 documentation:

Amazon S3 provides read-after-write consistency for PUTS of new objects in your S3 bucket in all regions with one caveat. The caveat is that if you make a HEAD or GET request to the key name (to find if the object exists) before creating the object, Amazon S3 provides eventual consistency for read-after-write.

Given the example code where the path is static and the exists call is made prior to the get, I'm conjecturing that you're being hit with eventual consistency. Your get should eventually return. Try:

$backoff = 0; while (false === ($image = Storage::disk('s3')->get($path))) {     if (5 < $backoff) {         throw new \RuntimeException;     }     sleep(pow(2, $backoff++)); } 

Answers 3

If you are using laravel 5 , than apply for this method.

$photo = $attributes['banner_image'];             $s3    = AWS::createClient('s3');             try {                 $response = $s3->putObject([                     'Bucket' => 'gfpressreleasephotos',                     'Key'    => str_random(8) . '.' . str_replace(' ', '-', strtolower($photo->getClientOriginalName())),                     'Body'   => fopen($photo->getRealPath(), 'r'),                     'ACL'    => 'public-read',                 ]);                 if ($response->get('ObjectURL') != null) {                     $photourl = $response->get('ObjectURL');                 } else {                     $photourl = $response->get('Location');                 }                 $attributes['banner_image'] = $photourl;             } catch (S3Exception $e) {                 return "There was an error uploading the file.\n";             } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment