2016-07-20 13 views
1

これは私がさまざまなAWSサービスを使用している私のラムダハンドラです。aws-mock-sdk s3.getObjectが動作しない

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); 
     }); 
    }) 
}); 

しかし、モック以外のものが呼び出されています。親切に助けてください。

+0

内S3変数を宣言してみてください。 – Ares

+0

この問題の解決方法はありますか? – roeygol

答えて

3

は、問題のみを複製することが可能に最小限のコードをご提示くださいハンドラ

exports.handler = function(event,context){ 
    var s3 = new aws.S3(); 
} 
関連する問題