我在 amazon s3 中上传文件时出错。 代码如下:
const s3 = require('s3'); const client = s3.createClient({
maxAsyncS3: 100,
s3RetryCount: 3,
s3RetryDelay: 30000,
multipartUploadThreshold: 20971520,
multipartUploadSize: 15728640,
s3Options: {
accessKeyId: "xxxx",
secretAccessKey: "yyyy",
region: "us-east-2",
}, });
const params = {
localDir: "file-path",
s3Params: {
Bucket: "bucket-name",
Prefix: "images/image.jpg"
}, };
const uploader = client.uploadDir(params);
uploader.on('error', (err) => {
console.error("unable to upload:", err.stack);
});
uploader.on('progress', () => {
console.log("progress", uploader.progressMd5Amount,
uploader.progressAmount, uploader.progressTotal); });
uploader.on('end', () => {
console.log("done uploading"); });
我得到的错误是:
unable to upload: Error: Non-file stream objects are not supported with SigV4 in AWS.S3
最佳答案
您应该向上传方法传递一个文件。 这是一个例子:
var file = files[0];
var fileName = file.name;
var albumPhotosKey = encodeURIComponent(albumName) + '//';
var photoKey = albumPhotosKey + fileName;
s3.upload({
Key: photoKey,
Body: file,
ACL: 'public-read'
}, function(err, data) {
if (err) {
return alert('There was an error uploading your photo: ', err.message);
}
alert('Successfully uploaded photo.');
viewAlbum(albumName);
});
}
更多示例位于:http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album-full.html
关于javascript - Amazon s3 Node 文件上传错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47811684/