试图设置的头ContentDisposition在PutObjectCommand结果403禁止

0

的问题

我把文件上传到S3成功地与我的应用程序。 我做一个直接的上传自浏览器的使用signedUrl,我的服务器生成为我作的流程应用程序。v3。

得到签署URL它看起来有点像这个

const s3Params = {
        Bucket : bucketName,
        Key : fileName,
        ContentType:fileType,
        // Metadata:{'Content-Disposition':'attachment'}
        // ContentDisposition:'attachment'
    };


    try {
        const command = new PutObjectCommand(s3Params);

        const url = await getSignedUrl(s3v3,command,{expiresIn:60});
        return url;
    } catch (e) {
        console.log('************** there was an error signing th url');
        console.log(e);
        throw e;
    }
};

这是工作完全正常的,但后来因为我读了一些文件我看到了,我应当能够设置头ContentDisposition. 在 这个文件 ,它说,所输入的PutObjectCommand延伸从 PutObjectRequest

后者有一个可选择的参数所谓 ContentDisposition 因为我想设置为附件,请允许我提示的"下载"窗口对于我的用户。 然而,当我使用signedURL上,但添加 ContentDisposition:'attachment' 领域,我获得一个禁止的错误。

没有人知道,如果我丢失任何东西在这里? 这不是一个实际的选择或者是否需要修改么东西在我的权限的S3为了这个?

1

最好的答案

1

我们必须指定 ContentDispositionPutObjectCommand param也为 getSignedUrl 功能为这样的:

async function main(fileName, bucketName, fileType) {
    const s3Params = {
        Bucket: bucketName,
        Key: fileName,
        ContentType: fileType,
        ContentDisposition: 'attachment'
    };

    const client = new S3Client({region: 'us-east-1'});
    const command = new PutObjectCommand(s3Params);

    const url = await getSignedUrl(client, command, {expiresIn: 60, ContentDisposition: 'attachment'});

    const file = await fs.readFile(fileName);

    const result = await axios({
        method: 'put',
        url,
        data: file,
        headers: {
            'Content-Type': fileType,
            'Content-Disposition': 'attachment'
        }
    });

    return result;
}
2021-10-30 20:29:21

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................