Return S3 私有文件作为流

Return S3 private file as stream

我目前正在为一个项目制定路线,我需要从 S3 获取特定文件,读取其内容,然后 return 作为二进制文件。我不能只使用 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);

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

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

我使用 return 图片的另一种方法是:

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

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