调用S3 PutObj测试事件时,AWS Lambda没有此类密钥错误

时间:2017-12-08 19:07:48

标签: python-2.7 amazon-s3 aws-lambda boto3 amazon-cloudwatch

我在github下面的代码片段中引用了一系列lambda测试事件(Hello World,S3PutObj,S3Deleteobj)之后,我仍然遇到以下错误说"调用时发生错误(NoSuchKey) GetObject操作"即使我已经创造了" HappyFace.jpg"文件并压缩并上传到存储桶。不知道我在这里失踪了什么。任何帮助是极大的赞赏。我的目标是使用Python Lambda脚本来解压缩s3存储桶。

from __future__ import print_function

import urllib
import zipfile
import boto3
import io


print('Loading function')

s3 = boto3.client('s3')
bucket = 'cwlogstestbucket'

def lambda_handler(event, context):

    key = urllib.unquote_plus(event['Records'][0]['s3']['object']
    ['key'].encode('utf8'))

    try:
        obj = s3.get_object(Bucket=bucket, Key=key)
        putObjects = []
        with io.BytesIO(obj["Body"].read()) as tf:
        # rewind the file
        tf.seek(0)

        # Read the file as a zipfile and process the members
        with zipfile.ZipFile(tf, mode='r') as zipf:
            for file in zipf.infolist():
                fileName = file.filename
                putFile = s3.put_object(Bucket=bucket, Key=fileName, 
                Body=zipf.read(file))
                putObjects.append(putFile)
                print(putFile)


    # Delete zip file after unzip
    if len(putObjects) > 0:
        deletedObj = s3.delete_object(Bucket=bucket, Key=key)
        print('deleted file:')
        print(deletedObj)

    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they 
              exist and your bucket is in the same region as this 
              function.'.format(key, bucket))
        raise e


"Here is the Error Message"


 "START RequestId: 83e72961-dc48-11e7-be00-b580169f8a7f Version: 
 $LATEST
 An error occurred (NoSuchKey) when calling the GetObject operation: 
 The specified key does not exist.
 Error getting object HappyFace.jpg from bucket cwlogstestbucket. 
 Make sure they exist and your bucket is in the same region as this 
 function.
 An error occurred (NoSuchKey) when calling the GetObject operation: 
 The specified key does not exist.: NoSuchKey
 Traceback (most recent call last):
 File "/var/task/lambda_function.py", line 43, in lambda_handler
 raise e
 NoSuchKey: An error occurred (NoSuchKey) when calling the GetObject 
 operation: The specified key does not exist."



 "Here is the test event configuration"

    {
    "Records": [
    {
     "eventVersion": "2.0",
     "eventTime": "1970-01-01T00:00:00.000Z",
     "requestParameters": {
     "sourceIPAddress": "0.0.0.0"
    },
     "s3": {
      "configurationId": "testConfigRule",
      "object": {
        "eTag": "0123456789abcdef0123456789abcdef",
      "sequencer": "0A1B2C3D4E5F678901",
      "key": "HappyFace.jpg",
      "size": 1024
    },
    "bucket": {
      "arn": "arn:aws:s3:::cwlogstestbucket",
      "name": "sourcebucket",
      "ownerIdentity": {
        "principalId": "EXAMPLE"
      }
     },
      "s3SchemaVersion": "1.0"
     },
      "responseElements": {
       "x-amz-id-2": 
  "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH",
    "x-amz-request-id": "EXAMPLE123456789"
    },
      "awsRegion": "us-east-2",
      "eventName": "ObjectCreated:Put",
      "userIdentity": {
      "principalId": "EXAMPLE"
    },
      "eventSource": "aws:s3"
   }
  ]
  }

0 个答案:

没有答案