我正在使用 upload方法来自 aws-sdk从浏览器中的 React 应用程序将文件上传到 S3 存储桶。
original callback based upload method如下所示:
var params = {Bucket: 'bucket', Key: 'key', Body: stream};
s3.upload(params, function(err, data) {
console.log(err, data);
});
我用 promisify 包裹了它像下面的 Async-await 一样工作:
const AWS = require('aws-sdk');
const { promisify } = require('es6-promisify');
... <in my React component> ...
async uploadFile() {
try {
var params = {
Bucket: <BucketName>,
Key: <KeyName>,
Body: <File Stream>
};
const res = await uploadToS3Async(params);
console.log(res);
} catch (error) {
console.log(error);
}
}
现在,当我在某些事件上调用 uploadFile
函数时,它会产生以下错误:
TypeError: service.getSignatureVersion is not a function
at ManagedUpload.bindServiceObject
at ManagedUpload.configure
at new ManagedUpload
at upload
at new Promise (<anonymous>)
最佳答案
你不需要使用es6-promisify
你可以这样做:
try {
const params = {Bucket: 'bucket', Key: 'key', Body: stream};
const data = await s3.upload(params).promise();
console.log(data);
}
catch (err) {
console.log(err);
}
https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-promises.html
关于javascript - 使用 Promisify 从回调转换为 Async-Await 格式后,AWS S3 上传不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57644283/