【问题标题】:Return S3 private file as stream将 S3 私有文件作为流返回
【发布时间】:2022-01-13 01:58:28
【问题描述】:

我目前正在为需要从 S3 获取特定文件、读取其内容并以二进制文件返回的项目制定路线。我不能只使用 S3 文件 url,因为它是私有的并且是有意的(我们只是不希望存储桶文件打开)。
但问题是,我想在新页面中流式传输它而不需要下载文件。
服务:

$file = $this->s3Provider->getFileContents(false); // gets the file binary
return $file;

控制器:

return response()->streamDownload(function() use($file) {
    echo $file; // streams data to download it, but I want just to show the file...
  }, $filename);

【问题讨论】:

    标签: php laravel amazon-s3 laravel-8


    【解决方案1】:

    我以为你在使用 Laravel,如果是这样,那么你可以像这样返回你的图像:

    return response($file, $filename, [
        'Content-Type' => 'image/png',
        'Content-Length' => filesize($file)
      ]);
    

    我用来返回图像的另一种方式是:

        return response()->file($path, array('Content-Type'=>'image/jpg'));
    

    Content-Type 应该是您的图像类型(JPG、PNG 等)。

    【讨论】:

    • 这正是我一直在寻找的,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 2016-11-04
    • 2018-10-26
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    相关资源
    最近更新 更多