首页 > 解决方案 > 强制从 AWS S3 下载 Flysystem 读取的文件

问题描述

大家,早安,

我的问题很简单,我使用 Flysystem(在 Symfony 上)从 S3(通过 DigitalOcean)恢复资源。我希望能够将其返回给用户下载。这是我的代码:

    $client = new S3Client([
        'credentials' => [
            'key'    => '',
            'secret' => '',
        ],
        'region' => '',
        'version' => 'latest',
        'endpoint' => 'https://ams3.digitaloceanspaces.com',
    ]);

    $adapter = new AwsS3Adapter($client, '');

    $filesystem = new Filesystem($adapter);
    $filename = 'file.png';
    $stream = $filesystem->readStream('file.png');
    $contents = stream_get_contents($stream);
    fclose($stream);

    $response = new Response($stream);
    $disposition = $response->headers->makeDisposition(
        ResponseHeaderBag::DISPOSITION_ATTACHMENT,
        $filename
    );
    $response->headers->set('Content-Disposition', $disposition);

    return $response;

=> 文件已损坏...

有任何想法吗?非常感谢您的帮助。纪尧姆

更新当我检查文本文件时,我收到了文件的内容,但带有标题:

HTTP/1.1 200 OK
Date: Thu, 27 Sep 2018 06:59:35 GMT
Server: Apache/2.4.29 (Ubuntu)
Vary: Authorization,Accept-Encoding
Cache-Control: no-cache, private
Content-Disposition: attachment; filename=toto.png
Link: <http://localhost/api/public/api/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"
Content-Length: 517
Content-Type: text/html; charset=UTF-8

仅下载文件内容和所有文件类型的任何想法?非常感谢你们,纪尧姆

标签: symfonyamazon-s3flysystem

解决方案


推荐阅读