Dynamic S3 Link 用于下载或查看 PDF
Dynamic S3 Link for downloading or viewing a PDF
我将一些客户 PDF 存储在 S3 中,供多方在浏览器中查看或下载。问题是我只能在 S3 中获取一个文件,以便始终下载或始终在浏览器中查看。
我可以只上传同一个文件两次,每个文件都有自己的 ContentDisposition
,但是这似乎很浪费,理想情况下它可以像在 [=28= 中添加 ?ContentDisposition=inline
这样简单] 桶 URL.
我的问题: 如何为单个 S3 文件动态设置 ContentDisposition
?
对于上下文,我当前的代码如下所示:
$s3_object = array(
'ContentDisposition' => sprintf('attachment; filename="%s"', addslashes($basename)),
'ACL' => 'public-read',
'ContentType' => 'pdf',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Bucket' => 'sample',
'Key' => static::build_file_path($path, $filename, $extension),
'Body' => $binary_content,
);
$result = $s3_client->putObject($s3_object);
此外,我确实尝试在 SO 的其他地方搜索这个,但大多数人似乎只是在寻找一个或另一个,所以我没有找到任何说明如何执行此操作的 SO 答案。
今天(一个多月后)我在查看其他 S3 文档时偶然发现了这个问题的最终答案。转到 GetObject docs for the S3 API 并在标有“Overriding Response Header Values”的部分下,我们发现以下内容:
Note: You must sign the request, either using an Authorization header or a presigned URL, when using these parameters. They cannot be used with an unsigned (anonymous) request.
- response-content-language
- response-expires
- response-cache-control
- response-content-disposition
- response-content-encoding
This answer's how to dynamically change any S3 object's content-disposition in the URL. 然而,至少对我来说,这是一个不完美的解决方案,因为我的预期用例是将 URL 作为发票存档的一部分存储多年,但已签名的 URL 最多仅在 1 周内有效。
从技术上讲,我也可以尝试找到一种方法让 Authorization header 为我工作,或者每次我想 [=22] 时查询 S3 API 以获得新的签名 URL =],但这对我来说还有其他安全性、性能和投资回报率的影响,所以不值得。
我将一些客户 PDF 存储在 S3 中,供多方在浏览器中查看或下载。问题是我只能在 S3 中获取一个文件,以便始终下载或始终在浏览器中查看。
我可以只上传同一个文件两次,每个文件都有自己的 ContentDisposition
,但是这似乎很浪费,理想情况下它可以像在 [=28= 中添加 ?ContentDisposition=inline
这样简单] 桶 URL.
我的问题: 如何为单个 S3 文件动态设置 ContentDisposition
?
对于上下文,我当前的代码如下所示:
$s3_object = array(
'ContentDisposition' => sprintf('attachment; filename="%s"', addslashes($basename)),
'ACL' => 'public-read',
'ContentType' => 'pdf',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Bucket' => 'sample',
'Key' => static::build_file_path($path, $filename, $extension),
'Body' => $binary_content,
);
$result = $s3_client->putObject($s3_object);
此外,我确实尝试在 SO 的其他地方搜索这个,但大多数人似乎只是在寻找一个或另一个,所以我没有找到任何说明如何执行此操作的 SO 答案。
今天(一个多月后)我在查看其他 S3 文档时偶然发现了这个问题的最终答案。转到 GetObject docs for the S3 API 并在标有“Overriding Response Header Values”的部分下,我们发现以下内容:
Note: You must sign the request, either using an Authorization header or a presigned URL, when using these parameters. They cannot be used with an unsigned (anonymous) request.
- response-content-language
- response-expires
- response-cache-control
- response-content-disposition
- response-content-encoding
This answer's how to dynamically change any S3 object's content-disposition in the URL. 然而,至少对我来说,这是一个不完美的解决方案,因为我的预期用例是将 URL 作为发票存档的一部分存储多年,但已签名的 URL 最多仅在 1 周内有效。
从技术上讲,我也可以尝试找到一种方法让 Authorization header 为我工作,或者每次我想 [=22] 时查询 S3 API 以获得新的签名 URL =],但这对我来说还有其他安全性、性能和投资回报率的影响,所以不值得。