Laravel 5.1 - 如何从 S3 存储桶下载 pdf 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31508013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.1 - how to download pdf file from S3 bucket
提问by Joel Joel Binks
I am using Laravel's Storage facade and I am able to upload the pdf to S3 and I am also able to get() its contents but I cannot display or download it to the end user as an actual pdf file. It just looks like raw data. Here is the code:
我正在使用 Laravel 的存储外观,我能够将 pdf 上传到 S3,我也能够 get() 其内容,但我无法将其作为实际的 pdf 文件显示或下载给最终用户。它只是看起来像原始数据。这是代码:
$file = Storage::disk($storageLocation)->get($urlToPDF);
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename='file.pdf'");
echo $file;
How can this be done? I have checked several articles (and SO) and none of them have worked for me.
如何才能做到这一点?我检查了几篇文章(和 SO),但没有一篇对我有用。
回答by star18bit
I think something like this will do the job in L5.2:
我认为这样的事情将在 L5.2 中完成工作:
public function download($path)
{
$fs = Storage::getDriver();
$stream = $fs->readStream($path);
return \Response::stream(function() use($stream) {
fpassthru($stream);
}, 200, [
"Content-Type" => $fs->getMimetype($path),
"Content-Length" => $fs->getSize($path),
"Content-disposition" => "attachment; filename=\"" .basename($path) . "\"",
]);
}
回答by Jigs Virani
you can create a download url, using the getObjectUrl method
您可以使用 getObjectUrl 方法创建下载 url
somthing like this:
像这样的东西:
$downloadUrl = $s3->getObjectUrl($bucketname, $file, '+5 minutes', array(
'ResponseContentDisposition' => 'attachment; filename=$file,'Content-Type' => 'application/octet-stream',
));
and pass that url to the user. that will direct the user to an amzon page which will start the file download (the link will be valid for 5 minutes - but you can change that)
并将该网址传递给用户。这会将用户定向到一个 amzon 页面,该页面将开始下载文件(该链接将在 5 分钟内有效 - 但您可以更改它)
another option, is first saving that file to your server, and then let the user download the file from your server
另一种选择是首先将该文件保存到您的服务器,然后让用户从您的服务器下载该文件
回答by elena
In Laravel 5.7 it can be done with streamDownload
:
在 Laravel 5.7 中,它可以通过以下方式完成streamDownload
:
return response()->streamDownload(function() use ($attachment) {
echo Storage::get($attachment->path);
}, $attachment->name);
回答by Freddy Barrera
If your bucket is private, this is the way to obtain a url to download the file.
如果您的存储桶是私有的,这是获取下载文件的 url 的方法。
$disk = \Storage::disk('s3');
if ($disk->exists($file)) {
$command = $disk->getDriver()->getAdapter()->getClient()->getCommand('GetObject', [
'Bucket' => \Config::get('filesystems.disks.s3.bucket'),
'Key' => $file,
'ResponseContentDisposition' => 'attachment;'
]);
$request = $disk->getDriver()->getAdapter()->getClient()->createPresignedRequest($command, '+5 minutes');
$url = (string)$request->getUri();
return response()->json([
'status' => 'success',
'url' => $url
]);
}
回答by Krishna Gawali
$d = file_full_path_here...
$d = str_replace(' ', '%20', $d); //remove the white space in url
ob_end_clean();
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=" . $d);
header("Content-Type: application/pdf");
return readfile($d);
回答by Rodrigo Fraga
$filename = 'test.pdf';
$filePath = storage_path($filename);
$header = [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="'.$filename.'"'
];
return Response::make(file_get_contents($filePath), 200, $header);
回答by Joel Joel Binks
I figured it out. Silly mistake. I had to remove the single quotes from filename.
我想到了。愚蠢的错误。我不得不从文件名中删除单引号。
Fix:
使固定:
$file = Storage::disk($storageLocation)->get($urlToPDF);
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=file.pdf");
echo $file;