Paul
Paul

Reputation: 858

Hosting uploads on amazon s3 in a private bucket, accessing url's from within Laravel

I'm using a s3 bucket for my application's user's uploads. This bucket is private.

When i use the following code the generated url is not accessible from within the application:

return Storage::disk('s3')->url($this->path);

I can solve this by generating a temporary url, this is accessible:

return Storage::disk('s3')->temporaryUrl($this->path, Carbon::now()->addMinutes(10));

Is this the only way to do this? Or are there other alternatives?

Upvotes: 1

Views: 224

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269091

When objects are private in Amazon S3, they cannot be accessed by an "anonymous" URL. This is what makes them private.

An objects can be accessed via an AWS API call from within your application if the IAM credentials associated with the application have permission to access the object.

If you wish to make the object accessible via a URL in a web browser (eg as the page URL or when referencing within a tag such as <img>), then you will need to create an Amazon S3 pre-signed URLs, which provides time-limited access to a private object. The URL includes authorization information.

While I don't know Laravel, it would appear that your first code sample is just provide a normal "anonymous" URL to the object in Amazon S3 and is therefore (correctly) failing. Your second code sample is apparently generating a pre-signed URL, which will work for the given time period. This is the correct way for making a URL that you can use in the browser.

Upvotes: 2

Related Questions