首页 > 解决方案 > AWS Lambda,使用 python 在 s3 存储桶之间复制

问题描述

所以我写了一个 lambda 函数,女巫是由 S3 PUT 触发的,

import datetime
import boto3
import botocore

#boto3.set_stream_logger('botocore', level='DEBUG')

def lambda_handler(event, context):

    src_bucket_name=event['Records'][0]['s3']['bucket']['name']
    print src_bucket_name
    file = event['Records'][0]['s3']['object']['key']
    split_string = file.split('/')
    file_string = split_string[-1].split('_')
    fecha_str = event['Records'][0]['eventTime']
    fecha_real=datetime.datetime.strptime(fecha_str, '%Y-%m-%dT%H:%M:%S.%fZ')+ datetime.timedelta(hours=-6)

    new_path='PATH/'+file_string[0].lower()+'/'+str(fecha_real.year)+'/'+str(fecha_real.month)+'/'+split_string[-1]


    s3 = boto3.resource('s3')
    s3_client = boto3.client('s3')


    copy_source = {
        'Bucket': src_bucket_name,
        'Key': file
    }

    s3.meta.client.copy(copy_source, DST_BUCKET_NAME, new_path)

当我运行代码时,我得到 ClientError:调用 HeadObject 操作时发生错误(404):未找到

该文件确实存在 source_bucket中的文件

你能告诉我我做错了什么吗?

编辑:

我授予了我正在使用的角色的管理员权限,但仍然有同样的错误。

更新关闭:

我删除了角色,创建了一个新角色,将复制部分的代码更改为:

copy_source = {
    'Bucket': src_bucket_name,
    'Key': file
}

r = s3_client.copy_object(
    Bucket=[DST_BUCKET_NAME],
    CopySource=copy_source,
    Key=new_path
)

它奏效了!

标签: amazon-web-servicesamazon-s3aws-lambdaboto3

解决方案


推荐阅读