gpt4 book ai didi

php - Laravel 8 从 AWS S3 Buckets 下载文件

转载 作者:行者123 更新时间:2023-12-05 06:56:49 25 4
gpt4 key购买 nike

我尝试了各种 Stackoverflow 中的几种不同方法,但我有点难过。

我正在尝试返回一个预签名的 url,以便我可以将其显示给最终用户,以便他们下载请求的文件。

我的代码

    $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();

它将返回一个 URL 本身

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

当我访问该 URL 时,它将显示以下内容。

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=

当我在 S3 Bucket 控制台上查看它时,我将拥有

显示

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

定义的对象 Url

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

我错过了什么?

最佳答案

//$filename is file name
Route::get('/user/{$filename}', [UserController::class, 'index']);
public function filedownload($filename){
$headers = ['Content-Type' => 'application/jpeg','Content-
Disposition' =>'attachment; filename=one.png',];
return \Response::make(Storage::disk('s3')->get('foldername/'.$keyid),
200,$headers);
}

关于php - Laravel 8 从 AWS S3 Buckets 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65046655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com