问题
Trying to run s3 upload from a mocha test:
'use strict';
describe('S3 test', function() {
it.only('S3 test 1', function*() {
var AWS = require('aws-sdk');
//AWS.config.region = 'us-west-2';
var s3 = new AWS.S3({
params: { Bucket: 'test-1-myBucket', Key: 'myKey' }
});
s3.createBucket(function(err) {
if (err) {
console.log("Error:", err);
} else {
s3.upload({
Body: 'Hello!'
}, function() {
console.log("Successfully uploaded data to myBucket/myKey");
});
}
});
});
});
but nothing happens, it is not sending the http request at all. Why is that?
回答1:
Doh. Its asynchronous so I need to use the done callback:
'use strict';
describe('S3 test', function() {
it.only('S3 test 1', function(done) {
var AWS = require('aws-sdk');
//AWS.config.region = 'us-west-2';
var s3 = new AWS.S3({
params: {
Bucket: 'mkruk-myBucket',
Key: 'myKey'
}
});
s3.createBucket(function(err) {
if (err) {
console.log("Error:", err);
done();
} else {
s3.upload({
Body: 'Hello!'
}, function() {
console.log("Successfully uploaded data to myBucket/myKey");
done();
});
}
});
});
});
回答2:
may be missing accessKeyId and secretAccessKey
var s3 = new AWS.S3({
accessKeyId: "",
secretAccessKey: ""
});
then
s3.upload({
Bucket: 'test-1-myBucket',
Key: 'myKey'
Body: 'Hello!',
}
来源:https://stackoverflow.com/questions/37337620/aws-sdk-s3-upload-not-working-from-mocha-test