php - 无法使用 return response()->download($file) 从 s3 下载文件;

标签 php laravel amazon-s3

我正在将文件上传到s3 我希望在我的 Controller 中使用以下代码安全地下载它,但它不起作用。

我正在使用 Laravel 5.5,文件可见性在 s3 上不公开。

if( Storage::disk('s3')->exists($file_path) ) {
      $file =  Storage::disk('s3')->get($file_path);
      return response()->download($file);
}

abort(404, 'File not found.');

它给我这个错误

is_file() expects parameter 1 to be a valid path, string given
...
/home/vagrant/spark-etr/vendor/symfony/http-foundation/File/File.php:36
#1 /home/vagrant/spark-etr/vendor/symfony/http-foundation/File/File.php(36): is_file('\\xFF\\xD8\\xFF\\xE0\\x00\\x10JFIF\\x00\\x01\\x01\\x00\\x00...')
#2 /home/vagrant/spark-etr/vendor/symfony/http-foundation/BinaryFileResponse.php(94): Symfony\\Component\\HttpFoundation\\File\\File->__construct('\\xFF\\xD8\\xFF\\xE0\\x00\\x10JFIF\\x00\\x01\\x01\\x00\\x00...')
#3 /home/vagrant/spark-etr/vendor/symfony/http-foundation/BinaryFileResponse.php(53): Symfony\\Component\\HttpFoundation\\BinaryFileResponse->setFile('\\xFF\\xD8\\xFF\\xE0\\x00\\x10JFIF\\x00\\x01\\x01\\x00\\x00...', 'attachment', false, true)
#4 /home/vagrant/spark-etr/vendor/laravel/framework/src/Illuminate/Routing/ResponseFactory.php(125): Symfony\\Component\\HttpFoundation\\BinaryFileResponse->__construct('\\xFF\\xD8\\xFF\\xE0\\x00\\x10JFIF\\x00\\x01\\x01\\x00\\x00...', 200, Array, true, 'attachment')

文件在 s3 上,因为我在下载前检查是否存在。

更新

转储 $file var 给我这样的二进制文件

enter image description here

请帮忙

最佳答案

尝试

if( Storage::disk('s3')->exists($file_path) ) {
      $file =  Storage::disk('s3')->get($file_path);

      $headers = [
        'Content-Type' => 'your_content_type', 
        'Content-Description' => 'File Transfer',
        'Content-Disposition' => "attachment; filename={$yourFileName}",
        'filename'=> $yourFileName
     ];

    return response($file, 200, $headers);
}

更新

您可以阅读有关下载 文件的更多信息 Why don't large files download easily in Laravel?

关于php - 无法使用 return response()->download($file) 从 s3 下载文件;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46762997/

相关文章:

php - 在 array_walk() 的回调函数中删除行

php - 计算 "as the crow flies"距离php

javascript - 预定的电子邮件

laravel - Laravel 中的自定义主键不起作用

javascript - 将 Amazon S3 文件导入数据库

java - 如何使用亚马逊s3路径在java中列出亚马逊s3文件夹

php - MySQL 中的排序/

laravel - 如何使用环境变量作为其他环境变量的引用?

php - 可以运行php artisan :clear commands on a production laravel website?

amazon-s3 - Elasticsearch v0.19.0的S3网关设置问题