【问题标题】:uplode image to amazon s3 using @aws-sdk/client-s3 ang get its location使用 @aws-sdk/client-s3 将图像上传到 amazon s3 并获取其位置
【发布时间】:2022-01-03 22:42:01
【问题描述】:

我正在尝试将图像文件上传到 s3,但收到此错误消息:

ERROR: MethodNotAllowed: The specified method is not allowed against this resource.

我的代码使用@aws-sdk/client-s3 包上传此代码:

const s3 = new S3({
    region: 'us-east-1',
    credentials: {
        accessKeyId: config.accessKeyId,
        secretAccessKey: config.secretAccessKey,
    }
});

exports.uploadFile = async options => {
    options.internalPath = options.internalPath || (`${config.s3.internalPath + options.moduleName}/`);
    options.ACL = options.ACL || 'public-read';

    logger.info(`Uploading [${options.path}]`);
    const params = {
        Bucket: config.s3.bucket,
        Body: fs.createReadStream(options.path),
        Key: options.internalPath + options.fileName,
        ACL: options.ACL
    };

    try {
        const s3Response = await s3.completeMultipartUpload(params);
        if (s3Response) {
            logger.info(`Done uploading, uploaded to: ${s3Response.Location}`);
            return { url: s3Response.Location };
        }
    } catch (err) {
        logger.error(err, 'unable to upload:');
        throw err;
    }
};

我不确定这个错误是什么意思,一旦文件上传,我需要在 s3 中获取他的位置

感谢您的帮助

【问题讨论】:

    标签: amazon-web-services amazon-s3 aws-sdk-js aws-sdk-nodejs


    【解决方案1】:

    要上传单个图像文件,您需要调用s3.upload() 而不是s3.completeMultipartUpload()

    如果您有非常大的文件并想分多个部分上传,则工作流程如下所示:

    s3.createMultipartUpload()
    s3.uploadPart()
    s3.uploadPart()
    ...
    s3.completeMultipartUpload()
    

    查看official documentation,看起来在 JavaScript SDK 中进行简单 S3 上传的新方法是这样的:

    s3.send(new PutObjectCommand(uploadParams));
    

    【讨论】:

    • 我明白了,当我尝试它时,它说:“TypeError: s3.upload is not a function”我错过了什么吗?
    • 这里是upload()函数文档的链接。 docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/…
    • 使用aws-sdk包可以,但是使用这个包@aws-sdk/client-s3它没有上传功能
    • @spinker 查看我的更新答案
    • 我明白了,它有效,现在唯一的问题是我没有像以前使用 aws-sdk 包时那样获得 response.Location
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 2015-05-30
    • 2020-12-04
    • 2020-10-02
    • 2016-01-30
    • 2021-12-07
    • 2014-10-24
    相关资源
    最近更新 更多