Home > OS >  Access Denied when trying to get an image through AWS s3
Access Denied when trying to get an image through AWS s3

Time:06-06

The way I'm uploading the image is the following:

        $file = $request->featured_image;
        $filename = time().'.'.$request->featured_image->extension();
        $file->storeAs('images/', $filename, 's3');

        $course = Course::create([
            'featured_image' => $filename,
        ]);

The problem is when I tried to get it with blade:

<img src="{{Storage::disk('s3')->url($course->featured_image)}}" alt="course">

When I try to access the URL, I get something like

<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>0W0NVXNHFRJY7MYP</RequestId>
<HostId>Xex73PhCOr1fm6cnhkDTOeqppblAYIrUtZ1jO81Jh3yjEhIHzvRcWsAg22VL5QrIoXcgefX6GTc=</HostId>
</Error>

For the permissions of AWS, I gave full access to S3. So I'm not sure why is this happening. The file DOES get saved.

CodePudding user response:

It's probably because you have a private bucket, you can issue temporary URLs really easily though to get around this. (or open up the permissions on your bucket)

example:

Storage::disk('s3')->temporaryUrl($image, now()->addMinutes(30))

Edit: Bucket privacy selection -> enter image description here

CodePudding user response:

When saving the image to S3 replace the following line

$file->storeAs('images/', $filename, 's3');

with

$file->storePublicly('images/', $filename, 's3');

also it looks like you are saving the image to images/filename.jpg however when attempting to retrieve, you're only passing the filename, try the below line and let me know how you go

{{Storage::disk('s3')->url('images/'.$course->featured_image)}}

  • Related