python - 每当在源存储桶中上传 zip 文件时,AWS lambda 函数都会触发从源 s3 到目标 s3 的复制
问题描述
我创建了一个基于事件的触发 lambda 函数,只要将 zip 文件加载到源 s3 存储桶中,它就会将 zip 文件从源 s3 复制到目标 s3。下面是python中的lambda函数:
from __future__ import print_function
import json
import urllib
import boto3
print('Loading function')
s3 = boto3.client('s3')
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']
['key'].encode("utf8"))
target_bucket = 'bucket name'
copy_source = {'Bucket' :bucket , 'Key' : key}
try:
response = s3.get_object(Bucket=bucket, Key=key)
print("CONTENT TYPE: " + response['ContentType'])
return response['ContentType']
print("copying from source to target")
s3.copy_object(Bucket=target_bucket,
Key=key,CopySource=copy_source)
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
以下是我通过执行代码得到的错误消息:
'Records': KeyError Traceback (most recent call last):
File "/var/task/lambda_function.py", line 13, in lambda_handler
bucket = event['Records'][0]['s3']['bucket']['name']
KeyError: 'Records'
任何解决此问题的帮助将不胜感激。
解决方案
推荐阅读
- c++ - 防止在静态库中隐式实例化 std::string/std::wstring
- javascript - 更改 sidenav 视图
- vue.js - 在 vue 中使用 d3.js
- php - 如何为返回空结果的现有单行输出查询
- android - 什么是android中aab(android app bundle)的仅资产模块和按需功能?
- c# - 带有“+”号且字符之间没有空格的参数值在响应 URL 中得到 404 Not Found
- python - 重组熊猫数据框
- java - 使用 txt 文件中的数据创建对象
- windows-10 - 在 Windows 10 中运行 .sh 文件
- flutter - 圆边,但 Inkwell customborder 中的尖角矩形