John Davidson

php - Laravel 8 Download file from AWS S3 Buckets

0 comments
Message:


I have tried a few different methods from various Stackoverflow and I am kinda stumped.


I am trying to return a presigned url so that I can display it to the end user for them to download requested file.


My Code


    $this->setDestination($documents->location); // folder/path/to/file
$this->setFileName($documents->name); // filename.pdf, filename.doc, etc

$client = Storage::disk('s3')->getDriver()->getAdapter()->getClient();
$bucket = Config::get('filesystems.disks.s3.bucket');

$command = $client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => 'docs/c1e09c74-fe9a-4bd8-b624-99249e7d4f98/123456/123456-1/209999/Document/3c0e772b-97cb-4e2e-bc33-fdbfae6a4adc.pdf'
/*
'Key' => $this->getDestination() . $this->getFileName()
*/
]);

$request = $client->createPresignedRequest($command, '+20 minutes');

// Get the actual presigned-url
$presignedUrl = (string)$request->getUri();

It will return a URL as such


https://MYSECRET.s3.us-west-2.amazonaws.com/docs/c1e09c74-fe9a-4bd8-b624-99249e7d4f98/123456/123456-1/209999/Document/3c0e772b-97cb-4e2e-bc33-fdbfae6a4adc.pdf3c0e772b-97cb-4e2e-bc33-fdbfae6a4adc.pdf?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAZCGODPV3AFHI2YM4%2F20201128%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20201128T051428Z&X-Amz-SignedHeaders=host&X-Amz-Expires=1200&X-Amz-Signature=0232040d7d9c46a51ee6c9424eec52151f6adc48191357d5022094603cbc58e5

When I visit the URL it will display the following.


NoSuchKeyThe specified key does not exist.docs/c1e09c74-fe9a-4bd8-b624-99249e7d4f98/123456/123456-1/209999/Document/3c0e772b-97cb-4e2e-bc33-fdbfae6a4adc.pdf3c0e772b-97cb-4e2e-bc33-fdbfae6a4adc.pdf9739CAE34DDF00A89ysmJVtjo8oZ1eckvRAW5autyeRLjjhhHc+xZEdStC2iEthhnRBSZHMBYorOt3RKLaZ/yiCMKOo=

When I view it on the S3 Bucket console I will have the


The Key is showing


docs/c1e09c74-fe9a-4bd8-b624-99249e7d4f98/123456/123456-1/209999/Document/3c0e772b-97cb-4e2e-bc33-fdbfae6a4adc.pdf

Object Url as defined


https://MYSECRET.s3-us-west-2.amazonaws.com/docs/c1e09c74-fe9a-4bd8-b624-99249e7d4f98/123456/123456-1/209999/Document/3c0e772b-97cb-4e2e-bc33-fdbfae6a4adc.pdf

What am I missing?



My Answer:
It looks like the issue might be with the Key that you are passing to the `GetObject` command. In your code, you are setting the Key manually to a specific path, but it seems like the actual file path in your S3 bucket might be different.

Instead of manually setting the Key, try using the actual file path in your S3 bucket. You can concatenate the destination and filename to get the correct Key. Here's an updated version of your code:

php
$this->setDestination($documents->location); // folder/path/to/file
$this->setFileName($documents->name); // filename.pdf, filename.doc, etc

$client = Storage::disk('s3')->getDriver()->getAdapter()->getClient();
$bucket = Config::get('filesystems.disks.s3.bucket');

$key = $this->getDestination() . $this->getFileName();

$command = $client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => $key
]);

$request = $client->createPresignedRequest($command, '+20 minutes');

// Get the actual presigned-url
$presignedUrl = (string)$request->getUri();


By using the actual file path in your S3 bucket as the Key, you should be able to generate a valid presigned URL that allows users to download the file successfully.

Rate this post

4 of 5 based on 6195 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us