S3 预签名 URL 多内容配置 Headers
S3 Presigned URL Multiple Content Disposition Headers
我有一个 S3 存储桶,其中包含 objects 的 PDF 文件,并且所有文件都是私有的。我以编程方式创建 S3 预签名 URL 以获取 object。它工作正常。现在,我希望它可以预览为 PDF。每个 object 已经有一个 Content-Type
header 设置为 application/pdf
。现在,如果我将 response-content-disposition
header 设置为查询参数,它会被设置但不会覆盖已经存在的 Content-disposition
header,而是创建一个新的.如果我在 S3 object 的元数据中设置 Content-Disposition
header 而不是将其添加到 S3 Presigned URL 作为查询参数,它仍然显示 2 header秒。这是 AWS S3 方面的某种错误吗?
下面是响应的截图Header以供参考。
任何帮助将不胜感激。谢谢。
我已经使用适用于 NodeJS 的 AWS SDK 的最新 API 解决了这个问题,代码如下:
const aws = require('aws-sdk');
const AWS_SIGNATURE_VERSION = 'v4';
const s3 = new aws.S3({
accessKeyId: <aws-access-key>,
secretAccessKey: <aws-secret-access-key>,
region: <aws-region>,
signatureVersion: AWS_SIGNATURE_VERSION
});
/**
* Return a signed document URL given a Document instance
* @param {object} document Document
* @return {string} Pre-signed URL to document in S3 bucket
*/
const getS3SignedDocumentURL = (docName) => {
const url = s3.getSignedUrl('getObject', {
Bucket: <aws-s3-bucket-name>,
Key: <aws-s3-object-key>,
Expires: <url-expiry-time-in-seconds>,
ResponseContentDisposition: `attachment; filename="${docName}"`
});
return url;
};
/**
* Return a signed document URL previewable given a Document instance
* @param {object} document Document
* @return {string} Pre-signed URL to previewable document in S3 bucket
*/
const getS3SignedDocumentURLPreviewable = (docName) => {
const url = s3.getSignedUrl('getObject', {
Bucket: <aws-s3-bucket-name>,
Key: <aws-s3-object-key>,
Expires: <url-expiry-time-in-seconds>,
ResponseContentDisposition: `inline; filename="${docName}"`
});
return url;
};
module.exports = {
getS3SignedDocumentURL,
getS3SignedDocumentURLPreviewable
};
注意:不要忘记用实际值替换占位符 (<...>) 以使其正常工作。
奇怪的是我们经常忽略文件名之类的东西,如果它是用户生成的名称,comma (,)
可能很常见。
设置 response-content-disposition
时确保去除特殊字符或正确转义文件名属性
参考了解更多详情
我有一个 S3 存储桶,其中包含 objects 的 PDF 文件,并且所有文件都是私有的。我以编程方式创建 S3 预签名 URL 以获取 object。它工作正常。现在,我希望它可以预览为 PDF。每个 object 已经有一个 Content-Type
header 设置为 application/pdf
。现在,如果我将 response-content-disposition
header 设置为查询参数,它会被设置但不会覆盖已经存在的 Content-disposition
header,而是创建一个新的.如果我在 S3 object 的元数据中设置 Content-Disposition
header 而不是将其添加到 S3 Presigned URL 作为查询参数,它仍然显示 2 header秒。这是 AWS S3 方面的某种错误吗?
下面是响应的截图Header以供参考。
任何帮助将不胜感激。谢谢。
我已经使用适用于 NodeJS 的 AWS SDK 的最新 API 解决了这个问题,代码如下:
const aws = require('aws-sdk');
const AWS_SIGNATURE_VERSION = 'v4';
const s3 = new aws.S3({
accessKeyId: <aws-access-key>,
secretAccessKey: <aws-secret-access-key>,
region: <aws-region>,
signatureVersion: AWS_SIGNATURE_VERSION
});
/**
* Return a signed document URL given a Document instance
* @param {object} document Document
* @return {string} Pre-signed URL to document in S3 bucket
*/
const getS3SignedDocumentURL = (docName) => {
const url = s3.getSignedUrl('getObject', {
Bucket: <aws-s3-bucket-name>,
Key: <aws-s3-object-key>,
Expires: <url-expiry-time-in-seconds>,
ResponseContentDisposition: `attachment; filename="${docName}"`
});
return url;
};
/**
* Return a signed document URL previewable given a Document instance
* @param {object} document Document
* @return {string} Pre-signed URL to previewable document in S3 bucket
*/
const getS3SignedDocumentURLPreviewable = (docName) => {
const url = s3.getSignedUrl('getObject', {
Bucket: <aws-s3-bucket-name>,
Key: <aws-s3-object-key>,
Expires: <url-expiry-time-in-seconds>,
ResponseContentDisposition: `inline; filename="${docName}"`
});
return url;
};
module.exports = {
getS3SignedDocumentURL,
getS3SignedDocumentURLPreviewable
};
注意:不要忘记用实际值替换占位符 (<...>) 以使其正常工作。
奇怪的是我们经常忽略文件名之类的东西,如果它是用户生成的名称,comma (,)
可能很常见。
设置 response-content-disposition
时确保去除特殊字符或正确转义文件名属性
参考了解更多详情