gpt4 book ai didi

node.js - S3 预签名 URL 多内容配置 header

转载 作者:太空宇宙 更新时间:2023-11-04 01:19:55 25 4
gpt4 key购买 nike

我有一个 S3 存储桶,其中包含 PDF 文件作为对象,并且所有这些文件都是私有(private)的。我以编程方式创建一个 S3 预签名 URL 来获取该对象。效果很好。现在,我希望它可以以 PDF 形式预览。每个对象都已将 Content-Type header 设置为 application/pdf。现在,如果我将 response-content-disposition header 设置为查询参数,它会被设置,但不会覆盖已经存在的 Content-disposition header ,而是创建一个新 header 。如果我在 S3 对象的元数据中设置 Content-Disposition header ,而不是将其作为查询参数添加到 S3 预签名 URL 中,它仍然显示 2 个 header 。这是 AWS S3 端的某种错误吗?

下面是响应头的屏幕截图,供引用。

Con

任何帮助将不胜感激。谢谢。

最佳答案

我已经使用 AWS SDK for NodeJS 提供的最新 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
};

注意:不要忘记将占位符 (<...>) 替换为实际值以使其正常工作。

关于node.js - S3 预签名 URL 多内容配置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59684182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com