2016-07-20 161 views
1

這是我使用各種AWS服務的lambda處理程序。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

請只顯示代碼能夠重現問題的最低金額。 – Ares

+0

您是否爲此問題獲得任何解決方案? – roeygol

回答

3

嘗試宣告處理器中S3可變

exports.handler = function(event,context){ 
    var s3 = new aws.S3(); 
}