s3.listObjects()数据变量返回null

时间:2017-12-13 15:21:21

标签: javascript amazon-s3 aws-sdk serverless

我正在尝试使用无服务器框架编写我的第一个lambda函数,该框架将文件从texts/文件夹复制到名为target/的S3存储桶中的s3-trigger-serverless-test文件夹。在调用listObjects()函数时,我收到以下错误:

TypeError: Cannot read property 'Contents' of null
    at Response.<anonymous> (/var/task/src/handler.js:14:12)
    at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:364:18)
    at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
    at Request.emit (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
    at Request.emit (/var/task/node_modules/aws-sdk/lib/request.js:683:14)
    at Request.transition (/var/task/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/var/task/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /var/task/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:685:12)

handler.js:

var AWS = require('aws-sdk');
var bucketName = 's3-trigger-serverless-test';
var oldPrefix = 'texts/';
var s3 = new AWS.S3({params: {Bucket: bucketName}});

module.exports.copyFilesToS3 = (event, context, callback) =>  {
    s3.listObjects({Bucket: bucketName, Prefix: oldPrefix}, function(err, data) {
        if (data.Contents.length) {           // this line results in the error above

serverless.yml:

functions:
  copyFilesToS3:
    handler: src/handler.copyFilesToS3
    events:
      - s3: 
          bucket: s3-trigger-serverless-test
          event: s3:ObjectCreated:*
          rules:
            - prefix: 'texts/'

aws-sdk版本是最新的,正如另一个stackoverflow答案所建议的那样。我想这可能是因为我传入listObjects()的Bucket和Prefix参数没有从S3存储桶返回任何数据,但我已经玩过其他参数和存储桶设置了,我还没有能够提出解决方案。

提前致谢

0 个答案:

没有答案