强制下载由Flysystem从AWS S3读取的文件

时间:2018-09-26 16:54:28

标签: symfony amazon-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

是否有只下载文件内容以及所有文件类型的想法?非常感谢你们,纪尧姆

0 个答案:

没有答案