简体   繁体   English

使用 lumen/php 从 Amazon S3 下载文件

[英]Download file from Amazon S3 with lumen/php

According to the resource given in the AWS documentation, downloading object can be done with the following code:根据AWS文档中给出的资源,可以使用以下代码下载object:

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'];
    }

According the docs the above code should display a pdf file (in my case), it opens a pdf but says根据文档,上面的代码应该显示一个 pdf 文件(在我的情况下),它会打开一个 pdf 但说

Failed to load PDF document.加载 PDF 文档失败。

The file is the s3 don't have any issue, I know it the code.该文件是 s3 没有任何问题,我知道它的代码。 But what can I do to the browser to read and also download automatically?但是我可以对浏览器做些什么来阅读和自动下载呢?

For detail info, I followed this link https://docs.aws.amazon.com/AmazonS3/latest/userguide/download-objects.html and went to using AWS SDKs有关详细信息,我点击此链接https://docs.aws.amazon.com/AmazonS3/latest/userguide/download-objects.html并使用 AWS 开发工具包

EDIT编辑

I changed 'ResponseContentType' => 'plain/text' to 'ResponseContentType' => 'application/pdf' , it opened the pdf but now I need to download it我将'ResponseContentType' => 'plain/text'更改为'ResponseContentType' => 'application/pdf' ,它打开了 pdf 但现在我需要下载它

This should most likely be:这很可能是:

'ResponseContentType' => 'application/pdf',

Else it will be transferred as text and will result in a corrupt file.否则它将作为文本传输并导致文件损坏。

Adding header in the code like below solved my issue:在如下代码中添加 header 解决了我的问题:

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'];
    }

I already wrote on EDIT that changing 'ResponseContentType' => 'plain/text' to 'ResponseContentType' => 'application/pdf' opened the pdf but I choose to not include ResponseContentType on my code so that it can it any file type.我已经在EDIT上写道,将'ResponseContentType' => 'plain/text'更改为'ResponseContentType' => 'application/pdf'打开了 pdf 但我选择不在我的代码中包含ResponseContentType以便它可以处理任何文件类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM