[英]How to force download on amzon s3 only sometimes
我有一個場景,我有一個公共 s3 存儲桶,其中包含網站上使用的圖像。
現在,我已經讀到你可以使用額外的獲取參數?response-content-disposition=attachment;filename=somefile.jpg
,但是當我使用它時我收到一條錯誤消息,說這對於公共文件和“匿名”獲取是不可能的要求。
<Error>
<Code>InvalidRequest</Code>
<Message>Request specific response headers cannot be used for anonymous GET requests.</Message>
<RequestId>...</RequestId>
<HostId>...</HostId>
</Error>
然后,我讀到您可以通過將文件的附加元數據設置為Key: Content-Disposition Value: Attachment
in Properties > Metadata > Add more metadata
來指定應該始終下載文件(或在通過 api 上傳文件時設置這些元數據)。 但該解決方案並不好,因為我有時需要顯示文件,有時需要將其作為附件下載。
有沒有辦法讓公共文件有時只能在 Amazon S3 存儲桶中下載?
您必須提出一個額外的請求,創建一個臨時簽名的請求,如下所示:
使用本地 AWS php 客戶端:
$cmd = $client->getCommand('GetObject', array(
'Bucket' => $bucket,
'Key' => $pathToFileOnAWS,
'ResponseContentDisposition' => 'attachment'
));
return (string) $s3Client->createPresignedRequest($cmd, '+20 minutes')->getUri();
簡單地使用 Laravel 時:
return Storage::disk('s3')->temporaryUrl( $pathToFileOnAWS, now()->addMinutes(20), ['ResponseContentDisposition' => 'attachment']);
你也可以使用'attachment, filename=somename.ext'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.