emiya
emiya

Reputation: 103

Laravel problem showing image in Amazon S3

Right now I am currently having a problem trying to show the image in my Amazon S3 folder.

.env

AWS_ACCESS_KEY_ID=somekeyid
AWS_SECRET_ACCESS_KEY=somekey
AWS_DEFAULT_REGION=eu-central-1
AWS_BUCKET=https://awsq-s3.s3.eu-central-1.amazonaws.com

controller.php

        $tempUrl =  Storage::disk('s3')->url('randomImage.jpg');

This shows the url, but when I try to access/show the image, it always says: access denied.

However, this one creating file

        $value = Storage::disk('s3')->put('file.txt', 'Contents');

works and it creates the file in my bucket. I tried providing full access to my account (apply AmazonS3FullAccess policy) and it still says access denied for showing picture.

Anyone knows what is the problem ?

Upvotes: 1

Views: 1421

Answers (1)

Lijesh Shakya
Lijesh Shakya

Reputation: 2542

The uploaded image might be private. Because of it, you might not be able to display using the url. To overcome this problem, Laravel has provided the helper function temporaryUrl($path, $expiration, array $options = []).

$expiryDate = now()->addDay(); //The link will be expire after 1 day
$temporaryUrl = Storage::disk('s3')->temporaryUrl($url, $expiryDate);

You can now then use the $temporaryUrl to access the private files under provided expiry date.

For more information, visit the URL documentation

Upvotes: 4

Related Questions