Я решил эту проблему, используя последний API, доступный для этой вещи, из AWS SDK для NodeJS, используя следующий код:
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>,
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>,
ResponseContentDisposition: `inline; filename="${docName}"`
});
return url;
};
module.exports = {
getS3SignedDocumentURL,
getS3SignedDocumentURLPreviewable
};
Примечание. Не забудьте заменить заполнители (<...>) с фактическими значениями, чтобы заставить его работать.