这可能是由于未正确指定Content-Type或Content-Disposition标头而导致的。在生成预签名URL时,需要手动设置这些标头。以下是一个示例,显示如何在Node.js中使用aws-sdk生成预签名URL并设置Content-Type和Content-Disposition标头:
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const params = {
Bucket: 'example-bucket',
Key: 'example-image.jpg',
Expires: 60 // URL有效期为1分钟
};
const url = s3.getSignedUrl('getObject', params);
// 将Content-Type和Content-Disposition标头附加到URL字符串
const contentTypeHeader = 'image/jpeg';
const contentDispositionHeader = 'attachment; filename="example-image.jpg"';
const signedUrlWithHeaders = `${url}&response-content-type=${encodeURIComponent(contentTypeHeader)}&response-content-disposition=${encodeURIComponent(contentDispositionHeader)}`;
注意:Content-Disposition标头中的文件名应与原始文件名相同,否则将无法在浏览器中正确显示。