The specified key does not exist - Laravel

Viewed 1797

I'm trying to create a temporary URL for a file in my application. I'm able to upload the file to S3 bucket and I'm able to use the method \Storage::temporaryUrl($this->url, now()->addHour(1)) generating the following URL

https://xxxxxx.s3.eu-west-2.amazonaws.com/https%3A//xxxxxxx.s3.eu-west-2.amazonaws.com/images/fYr20cgYh3nAwoEEQCOTaVTLLo7nRFrXjp7cYcCz.jpg?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVHH6TLEV3Z2FBWLY%2F20210622%2Feu-west-2%2Fs3%2Faws4_request&X-Amz-Date=20210622T191649Z&X-Amz-SignedHeaders=host&X-Amz-Expires=3600&X-Amz-Signature=6300aa81e69c6f4c96cb6f319a2b5ed2bfc2b2767138994928a49f3f93906745

When I click on this URL I get the following error:

The specified key does not exist.

From this question https://stackoverflow.com/a/28654035/4581336 I have checked the following:

  • the file name exists in my bucket and it is a copy-paste of the file under the Object URL in my bucket
  • I tried removing the extension from the file to see if it affects the URL but no luck as well. Having xxxx.jpg vs xxxxx as the file name is the same

file name from my bucket

I'm a fairly new guy in the AWS world so I will copy-paste things that I think might be important to help solve the issue.

The IAM user created has the following permissions:

  • AmazonS3FullAccess

The bucket Block Public Access settings for this account has everything blocked:

Block Public Access settings for this account

My bucket public permission has everything enabled as well:

my bucket permissions

I'm currently logged in as a root user (which therefore I'm assuming I can do whatever I want since I'm the root)

If I do all my buckets public I'm able to access the files using the extracted URL generated by the temporaryUrl method

The final goal

The objective of the bucket is to have a place to store files that are uploaded by users in my application. I don't want to have all the files public so I would like to restrict users to the files they own so I create a temporary URL with

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

Since I'm fairly new to storing files in S3 my logic may be flawed. Please, correct me if this is not how it's supposed to go.

Questions I have looked at but didn't help me

1 Answers

In your first URL, it seems like you've got the hostname there twice - https://xxxxxx.s3.eu-west-2.amazonaws.com appears once, and then a second time encoded. Are you storing the full hostname in the $url parameter to temporaryUrl? You should only be passing the key (the actual filename) to that method.

The error doesn't sound like a permission error - it appears as though you have access to the bucket but just aren't getting the file path correct.

Related