'Laravel S3 - arn and kms key

Client has provided me below details:-

AWS_ACCESS_KEY_ID=XXX
AWS_SECRET_ACCESS_KEY=XXX
AWS_DEFAULT_REGION=XXX
AWS_BUCKET=XXX
ARN: arn:aws:s3:::XXX
KMS Key: XXX

Image is uploaded successfully on aws server. but when i tried to open the url, access denied is coming.

Image upload code as below :-

$filePath = "users/" . $user_id . "/". $name;
Storage::disk('s3')->put($filePath, file_get_contents($file));
$fileName = Storage::disk('s3')->url($filePath);


Solution 1:[1]

Yes, by default, AWS S3 implements this restriction for security purposes. You can meet your needs by doing this :

$publicTime = 10; // Your file will be available 10 minutes
$filePath = "users/" . $user_id . "/". $name;

// Upload file 
Storage::disk('s3')->put($filePath, file_get_contents($file));

// Generate temporary URL
$url = Storage::disk('s3')->temporaryUrl($filePath, now()->addMinutes($publicTime))

With this method, you can keep your S3 buckets private while providing public access for your files.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Théo Champion