【问题标题】:Download file from Amazon S3 with lumen/php使用 lumen/php 从 Amazon S3 下载文件
【发布时间】:2021-05-24 08:37:10
【问题描述】:

根据AWS文档中给出的资源,可以使用以下代码下载对象:

public function download($folder, $file)
    {
        $s3 = new S3Client([
            'region'  => env('AWS_REGION'),
            'version'=>'latest',
            'credentials' => [
                'key'    =>env('AWS_ACCESS_KEY_ID'),
                'secret' =>env('AWS_SECRET_ACCESS_KEY')
            ],
        ]);
        $result = $s3->getObject([
            'Bucket'                     => env('AWS_BUCKET'),
            'Key'                        => $folder.'/'.$file,
            'ResponseContentType'        => 'text/plain',
            'ResponseContentLanguage'    => 'en-US',
            'ResponseContentDisposition' => 'attachment; filename='.$file,
            'ResponseCacheControl'       => 'No-cache',
            'ResponseExpires'            => gmdate(DATE_RFC2822, time() + 3600),
        ]);
        header("Content-Type: {$result['ContentType']}");
        echo $result['Body'];
    }

根据文档,上面的代码应该显示一个 pdf 文件(在我的情况下),它会打开一个 pdf,但会说

加载 PDF 文档失败。

该文件是 s3 没有任何问题,我知道它的代码。但是我可以对浏览器做些什么来自动阅读和下载呢?

有关详细信息,我点击此链接https://docs.aws.amazon.com/AmazonS3/latest/userguide/download-objects.html 并使用 AWS 开发工具包

编辑

我将'ResponseContentType' => 'plain/text'更改为'ResponseContentType' => 'application/pdf',它打开了pdf但现在我需要下载它

【问题讨论】:

  • 检查上传对象到 S3 时设置的内容类型

标签: php amazon-web-services amazon-s3 lumen


【解决方案1】:

这很可能是:

'ResponseContentType' => 'application/pdf',

否则它将作为文本传输并导致文件损坏。

【讨论】:

    【解决方案2】:

    在下面的代码中添加标题解决了我的问题:

    public function download($folder, $file)
        {
            $s3 = new S3Client([
                'region'  => env('AWS_REGION'),
                'version'=>'latest',
                'credentials' => [
                    'key'    =>env('AWS_ACCESS_KEY_ID'),
                    'secret' =>env('AWS_SECRET_ACCESS_KEY')
                ],
            ]);
            $result = $s3->getObject([
                'Bucket'                     => env('AWS_BUCKET'),
                'Key'                        => $folder.'/'.$file,
                'ResponseContentLanguage'    => 'en-US',
                'ResponseCacheControl'       => 'No-cache',
                'ResponseExpires'            => gmdate(DATE_RFC2822, time() + 3600),
            ]);
            header("Cache-Control: public");
            header("Content-Description: File Transfer");
            header("Content-Disposition: attachment; filename=" . $file);
            header("Content-Type: {$result['ContentType']}");
            echo $result['Body'];
        }
    

    我已经在 EDIT 上写道,将 'ResponseContentType' => 'plain/text' 更改为 'ResponseContentType' => 'application/pdf' 会打开 pdf,但我选择不在我的代码中包含 ResponseContentType,以便它可以处理任何文件类型。

    【讨论】:

      猜你喜欢
      • 2014-12-01
      • 2012-10-10
      • 2012-08-29
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      • 2020-05-29
      相关资源
      最近更新 更多