簡體   English   中英

使用 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM