我正在将文件上传到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 给我这样的二进制文件
请帮忙
最佳答案
尝试
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/