aws-sdk s3 upload not working from mocha test

二次信任 提交于 2020-01-06 04:20:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!