[英]Force image download from S3 with Laravel
I'm trying to download images with a link that routes me to this method:我正在尝试使用将我路由到此方法的链接下载图像:
$image = $st->getFullPathWithImage(null, $type, true, true);
if ($image) {
$content_type = 'image/jpeg';
$basename = basename($image);
Download::add($st->id);
return Response::download($image, $basename, array('Content-Type: ' . $content_type));
}
If I try and debug what I have in $image I get the correct URL from S3, however the method fails stating that:如果我尝试调试$image 中的内容,我会从 S3 获得正确的 URL,但是该方法无法说明:
The file "path_to_my_bucket/uploads/14424179653mYJLut2zVEnyR30YQEm.jpg" does not exist
I'm guessing that this is due to something I must set on S3/AWS, but I'm clueless to what it is, and how I can bind it to Laravel's Response::download().我猜这是因为我必须在 S3/AWS 上设置一些东西,但我对它是什么以及如何将它绑定到 Laravel 的 Response::download() 一无所知。 Any help is appreciated.任何帮助表示赞赏。
Answering for someone with the same issue.回答有同样问题的人。
Turns out S3 had nothing to do with the issue.原来 S3 与这个问题无关。 Instead of using Laravel's Response::download(), I switched for manual, raw php header setting and things do work.我没有使用 Laravel 的 Response::download(),而是切换到手动的、原始的 php 标头设置,并且一切正常。 I'm not sure why the regular Laravel solution returned a 404, but I needed an easy and quick fix and this is the best I could find.我不确定为什么常规的 Laravel 解决方案会返回 404,但我需要一个简单快捷的解决方案,这是我能找到的最好的解决方案。
So, instead of returning Response::download(), do this:因此,不要返回 Response::download(),而是这样做:
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=" . basename($image));
header("Content-Type: image/jpeg");
return readfile($image);
Can also return a redirect to the image URL还可以返回重定向到图像 URL
return response()->redirectTo("https://s3-us-west-2.amazonaws.com/" . $document->path);
Source: https://laracasts.com/discuss/channels/laravel/file-response-from-s3来源: https : //laracasts.com/discuss/channels/laravel/file-response-from-s3
$d = image full path here..
$d = str_replace(' ', '%20', $d); //remove the whitespace in url
ob_end_clean();
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=" . $d);
header("Content-Type: image/png");
return readfile($d);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.