使用Laravel强制从S3下载图像


Force image download from S3 with Laravel

我正在尝试下载带有将我路由到此方法的链接的图像:

    $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));
    }

如果我尝试调试我在$image中所拥有的内容,我从S3获得正确的URL,但是该方法失败,说明:

The file "path_to_my_bucket/uploads/14424179653mYJLut2zVEnyR30YQEm.jpg" does not exist

我猜这是由于我必须在S3/AWS上设置的东西,但我不知道它是什么,以及如何将它绑定到Laravel的Response::download()。

替别人回答同样的问题。

结果S3与这个问题无关。而不是使用Laravel的Response::download(),我切换到手动,原始的php头设置和工作。我不知道为什么常规的Laravel解决方案返回404,但我需要一个简单快速的修复,这是我能找到的最好的。

所以,与其返回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);

还可以返回到图像URL的重定向

return response()->redirectTo("https://s3-us-west-2.amazonaws.com/" . $document->path);

来源: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);