[英]force download file from s3 url
I have an s3 URL without a file extension in the URL.我在 URL 中有一个没有文件扩展名的 s3 URL。 is there any way I can force download file from the URL directly有什么办法可以直接从 URL 强制下载文件
https://s3-external-1.amazonaws.com/media.twiliocdn.com/AC451bab3cca7e01e20ee6bf1e746bed1f/10b62b6d5fe2f69be39840ce1201a2e7
In the database, I am storing this URL to display images or any file but now I have a requirement to download this image in user system on click of download button在数据库中,我存储此 URL 以显示图像或任何文件,但现在我需要单击下载按钮在用户系统中下载此图像
FYI: this is not our s3 bucket but this is Twilio service bucket where our media is getting stored.仅供参考:这不是我们的 s3 存储桶,但这是存储我们的媒体的 Twilio 服务存储桶。
From your controller, you're able to return a download response that lets you specify a file with a custom name.从 controller 中,您可以返回下载响应,让您指定具有自定义名称的文件。 Like so...像这样...
return response()
->download(
$file,
'download.jpeg',
);
You can temporarily store the image in your filesystem then send it as a download response.您可以将图像临时存储在文件系统中,然后将其作为下载响应发送。
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
$response = Http::get(
'https://s3-external-1.amazonaws.com/media.twiliocdn.com/AC451bab3cca7e01e20ee6bf1e746bed1f/10b62b6d5fe2f69be39840ce1201a2e7'
);
file_put_contents($file = '/tmp/' . Str::uuid(), $response->body());
preg_match('/filename=\"(.+)\"/', $response->header('content-disposition'), $matches);
return response()->download($file, $matches[1], [
'Content-Type' => $response->header('content-type'),
])->deleteFileAfterSend();
Finally, after investing so many hours I found solution for this.最后,在投入了这么多小时后,我找到了解决方案。
public function download($media){
$name = now()->format('m-d-Y-h:i:s') . '.' . Illuminate\Http\Testing\MimeType::search($media->content_type);
return response()->streamDownload(function () use ($media){
echo file_get_contents($media->url);
}, $name);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.