我正在使用AWS S3为文件生成createPresignedRequest()。当用户点击它在新窗口中打开的链接时,我希望它强制下载。我能做些什么来启用此功能。我查看了文档,但我没有看到我需要的东西。
这是我创建网址的代码:
$s3 = new S3Client([
'credentials'=>[
'key'=>$key,
'secret'=>$secret,
],
'region'=>'us-west-2',
'version'=>'2006-03-01',
]);
$cmd = $s3->getCommand('GetObject', [
'Bucket' => $buckname,
'Key' => $file,
]);
$request = $s3->createPresignedRequest($cmd, '+7 days');
$secureUrl = (string) $request->getUri();
echo $secureUrl; // When the user clicks on the link - how can I force download the file instead of open in a new window.
答案 0 :(得分:2)
您可以在生成预签名网址时强制下载,方法是将ResponseContentDisposition
添加到GetObject
个参数。
'ResponseContentDisposition' => '<string>',
这会指示S3在使用签名URL时设置Content-Disposition
响应标头,其值为<string>
。对象保持不变。
'<string>'
使用的值可以是以下值之一:
'attachment'
'attachment; filename="some-filename.jpg"'
第二个示例将导致大多数浏览器使用您指定的名称保存文件,而不是在S3中保存其名称。在;
之后的attachment
后面有一个空格。
执行此操作时,您会发现签名的网址现在包含S3 API Reference中提及的&response-content-dispositon=...
。
或者,您可以在将对象上传到S3时将Content-Disposition
设置为所需的值,这将自动出现在所有下载中。
答案 1 :(得分:0)
这取决于内容类型集。如果假设内容类型是浏览器可以显示的jpeg或txt;然后它会在浏览器中显示..如果浏览器无法查看内容类型,那么它会自动下载它,例如exe文件。