前提・実現したいこと
お世話になっております。
Laravelを通じてS3バケットからファイルをダウンロードする機能を作りましたが、本番環境のみで動かない状態です。
画像のみ404になってしまい、ハマっているので、何かお気づきの点があれば教えていただけますと幸いです。
現在の状況
/fileDownload/24238/CnqOBtS3GpetBXmtoy9u.jpg?name=jpgtest.jpg
このようなルーティングでS3にあるファイル名とダウンロード名(リアルファイル名)をダウンロードしたいのですが、本番環境の画像のみ404になってしまいます。
コントローラーは以下のようになります。
php
1 public function downloadFile($id, $file, Request $request) 2 { 3 $originalName = $request->get('name'); 4 $content = Storage::disk('s3')->get("documents/$id/$file"); 5 6 $explode = explode('.', $file); 7 $contentType = last($explode); 8 9 $headers = [ 10 'Content-Type' => $contentType, 11 'Content-Description' => 'File Transfer', 12 'Content-Disposition' => "attachment; filename={$originalName}", 13 'filename' => $originalName, 14 ]; 15 16 return response($content, 200, $headers); 17 }
以下のような画像以外の拡張子は正常にダウンロードされます。
/contact/fileDownload/24238/drrnyNrAUheXcxEbTDoJ.csv?name=user.csv
本番環境とローカル環境の違いはサーバーで、
ローカル環境: php7.3-apacheのdocker-image
本番環境: AWS Lightsail php7.3 + nginx1.10
なので、nginxあたりが問題なのかなと考えています。
発生している問題・エラーメッセージ
該当のソースコード
ルーティング
php
1 Route::get('fileDownload/{id}/{file}', 'ContactController@downloadFile')->name('download.file');
View
php
1 <a class="btn btn-primary" href="{{ route('download.file', ['id' => $contact->id,'file' => $contact->document3, 'name' => $contact->document3_original_name ]) }}">ダウンロード</a>
試したこと
URLパラメーター化
クエリパラメーターではなく、URLパラメーターに設定しましたが、同じ症状でした。
ルーティング
php
1 Route::get('fileDownload/{id}/{file}/{originalName}', 'ContactController@downloadFile')->name('download.file');
コントローラー
php
1 public function downloadFile($id, $file, $originalName) 2 { 3 $content = Storage::disk('s3')->get("documents/$id/$file"); 4 5 $explode = explode('.', $file); 6 $contentType = last($explode); 7 8 $headers = [ 9 'Content-Type' => $contentType, 10 'Content-Description' => 'File Transfer', 11 'Content-Disposition' => "attachment; filename={$originalName}", 12 'filename' => $originalName, 13 ]; 14 15 return response($content, 200, $headers); 16 }
View
php
1 <a class="btn btn-primary" href="{{ route('download.file', ['id' => $contact->id,'file' => $contact->document3, 'name' => $contact->document3_original_name ]) }}">ダウンロード</a>
nginx設定の確認
conf
1http { 2 3 ## 4 # Basic Settings 5 ## 6 7 sendfile on; 8 tcp_nopush on; 9 tcp_nodelay on; 10 keepalive_timeout 65; 11 types_hash_max_size 2048; 12 # server_tokens off; 13 14 # server_names_hash_bucket_size 64; 15 # server_name_in_redirect off; 16 17 include /etc/nginx/mime.types; 18 default_type application/octet-stream;
mimetypesにも記述してあり問題ありませんでした。
URLから拡張子を削除
以下のようにURLから拡張子を除くとダウンロードが成功します。
補足情報(FW/ツールのバージョンなど)
php: 7.39
nginx: 1.10
larval 6.0
以上になります。どうぞよろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/01 21:57