aws-mock-sdk s3.getObject无效

时间:2016-07-20 16:31:17

标签: node.js aws-lambda

这是我使用各种AWS服务的lambda处理程序。

exports.handler = function(event,context){
    var body = event;
    var isValidBody = validate.doValidation(body);

    var s3DetailsObj = {
        eTag: body.s3Details.eTag, 
        location: body.s3Details.location,
        key: body.s3Details.key,
        bucket: body.s3Details.bucket
    }
    var s3DeletionErrorObject = {
        status : "SERVER_ERROR",
        sourceUid : body.sourceUid,
        s3Details : s3DetailsObj
    };

    if(isValidBody.status){
        console.log("Request validation successfull");
        async.waterfall([
            function downloadFromS3(uploadToGlacier){
                //console.log('KEY ====== :',body.s3Details.key);
                var params = {
                        Bucket: 'imagebucketgsk',
                        Key: body.s3Details.key
                    };
                s3.getObject(params,function(error, response){
                    //console.log("Into S3 get object");
                    if(!error){
                        //console.log('Repsonse = ', response.Body);
                        uploadToGlacier(null,response);
                    }else{
                        //console.error("S3 get error : ", error);
                        context.succeed(s3DeletionErrorObject);
                    }
                });
            },
            function uploadToGlacier(response, deleteFromS3){
                var params = {
                    vaultName: vaultName,
                    body: response.Body
                };
                glacier.uploadArchive(params, function(error, response){
                    if(!error){
                        deleteFromS3(null,response);
                    }else{
                        console.error("Galcier error : ", error);
                        context.succeed(s3DeletionErrorObject);
                    }
                });
            },
            function deleteFromS3(glacierData, done){
                var params = {
                    Bucket : s3Bucket,
                    Key: body.s3Details.key
                };

                s3.deleteObject(params, function(error, response){
                    if(!error){
                        var s3AndGlacierResponseObject = {
                            status : 'OK',
                            glacierLocation : glacierData.location,
                            glacierChecksum : glacierData.checksum,
                            glacierArchiveId : glacierData.archiveId,
                            s3deleteMarker : response.DeleteMarker,
                            s3DeleteVersionId : response.VersionId,
                            s3DeletedImageSourceUid : body.sourceUid,
                            s3DeletedKey : body.s3Details.key 
                        }
                        context.succeed(s3AndGlacierResponseObject);    
                    }else{
                        console.error('Deletion from s3 failed', error);
                        context.succeed(s3DeletionErrorObject);
                    }
                });
            }
        ], function(error, glacierResponse, deletedImageResponse){
                console.log('Glacier details : ', glacierResponse);
                console.log('Deleted image : ', deletedImageResponse);
        });
    }
};

这是我想测试上述使用服务的测试用例。但是当嘲笑没有被调用时。没有错误。

var chai = require('chai');
var AWSMock = require('aws-sdk-mock');
var index = require('../index');

describe('Testing the image delete function', function(){
    describe('Testing the validity of the request', function(){
        it('should delete image as all the fields are valid in the request', function(done){
      var context = {
        succeed : function(result){
          console.log(result);
          done();
        } 
      };

      AWSMock.mock('S3', 'getObject', function(parmas,callback){
        console.log("Callback received for getObject");
        callback(null,'Success');
      });


      AWSMock.mock('Glacier','uploadArchive', function(params,callback){
        callback(null,{'DeleteStatus': true 
          ,'glacierLocation': 'LOCATION' 
          ,'glacierChecksum': 'CHECKSUM' 
          ,'glacierArchiveId': 'ARCHIVE ID'})
      });

      AWSMock.mock('S3', 'deleteObject', function(param,callback){
        callback(null,{'s3deleteMarker': true 
          ,'s3DeleteVersionId': 'VERSION ID' 
          ,'s3DeletedImageSourceUid': 'SOURCE' 
          ,'s3DeletedKey': 'KEY'});
      });

    index.handler({
        'sourceUid': 'UID' 
        ,'s3Details': {
          'eTag': 'ETAG' 
          ,'location': 'LOCATION' 
          ,'key': 'KEY' 
          ,'bucket': 'BUCKET'
        }
      },context);
        });
    })
}); 

但是没有嘲笑被调用。请帮助。

2 个答案:

答案 0 :(得分:5)

尝试在处理程序

中声明s3变量
exports.handler = function(event,context){
   var s3 = new aws.S3();
}

答案 1 :(得分:1)

除了在处理程序中声明s3变量外,在模拟getObject响应时,还可以设置响应中的Body属性。

AWS.mock("S3", "getObject", function(parmas, callback) {
    callback(null, {
        Body: Buffer.from(require("fs").readFileSync("file.json"))
    })
})