Getting file on AWS S3 without bucket being public with Laravel

Viewed 924

I'm working with S3 files with Laravel.

My S3 has no public access, but Laravel is configured with a AWS User that has admin rights on S3.

So, I can upload a file in S3 with no problem.

My issue comes from getting file from there.

If I use:

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

I will generate a link with format:

https://bucket.s3.region.amazonaws.com/path/filename.pdf

But as public access is not allowed on S3, it will give me

Access Denied

If I use

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

I can download the data from file, but don't know what to do with it.

EDIT: It seams that doing:

Storage::disk('local')->put(
    'filename.txt', 
    Storage::disk('s3')->get('cloud-filename.txt'));

Will copy the S3 file in storage folder. Now, how should I generate a working link ?

1 Answers

Have you tried generating temporary urls?

// link will expire 5 hours from now.
$expires_at = now()->addHours(5);

Storage::disk('s3')->temporaryUrl($this->path, $expires_at);

For your second question, I think it should be the other way around:

// retrieve file 'filename.txt' from local disk and put it on s3 disk as 'cloud-filename.txt'
Storage::disk('s3')->put(
    'cloud-filename.txt',
    Storage::disk('local')->get('filename.txt')
);
Related