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

标签 php laravel amazon-web-services amazon-s3

我尝试了各种 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/

相关文章:

php - 如何在php中读取任何附加文件的内容

php - Laravel - 防止错误显示在控制台中

php - 禁用 Laravel 内置的错误处理方法

python - 在 AWS Sagemaker 中为 scikit learn 模型创建端点

php - 如何使用动态id制作多个ajax触发器

php - 动态生成 map

php - AngularJS + Laravel 5 认证

ruby - 如何将 laravel 数组转换为 ruby​​ 哈希访问

node.js - 如何指定亚马逊产品 API 返回的内容

amazon-web-services - 如何调试cloudformation模板?策略中的语法错误。 YAML 文件