安装AWS CLI并使用以下代码示例:
import boto3
import os
import uuid
import urllib.parse
s3 = boto3.client('s3')
def lambda_handler(event, context):
for record in event['Records']:
#获取事件数据
bucket = record['s3']['bucket']['name']
key = urllib.parse.unquote_plus(record['s3']['object']['key'], encoding='utf-8')
try:
#获取S3对象
response = s3.get_object(Bucket=bucket, Key=key)
#复制对象到目标桶
target_bucket = 'my-target-bucket'
target_key = str(uuid.uuid4())
target_copy_source = {
'Bucket': bucket,
'Key': key
}
s3.copy_object(Bucket=target_bucket, CopySource=target_copy_source, Key=target_key)
return 'Copied video successfully'
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
在lambda控制台上,确保将适当的角色分配给lambda函数以在目标桶中写入。