AWS Lambda将所有xml从存储桶上移至另一个存储桶-Python

时间:2020-02-25 04:55:51

标签: boto3

我有一个要求,其中有两个存储桶,例如Bucket1和bucket2。 Bucket1包含所有XML文件。我想将所有xmls文件从bucketA移到BucketB(所有文件)。 然后从BucketA中删除所有文件。

我正在将Boto3与Python lambda函数一起使用-一旦在BucketA中更新了任何文件,这将触发基于事件的事件

1 个答案:

答案 0 :(得分:1)

您可以尝试以下操作:

import boto

c = boto.connect_s3()
src = c.get_bucket('source_bucket')
dst = c.get_bucket('destination_bucket')

for k in src.list():
    # copy files to your destination bucket
    dst.copy_key(k.key.name, src.name, k.key.name)
    # then delete the source key
    k.delete()

我执行的另一种方法如下:

def get_object(s3, bucketName, file):

    try:
        s3_file = s3.get_object(
        Bucket = bucketName,
        Key = file
        )


        return s3_file

    except Exception as e:

        logger.info('file not found')



#Decription: This method enables to put file long with its conetent from one location to another
def move_objects(s3, bucketName, file, newFolder):

    #s3_object = s3.get_object(Bucket=bucketName, Key=file)

    #serializedObject = file['Body'].read().decode('utf-8')

    response = s3.put_object(
        Bucket = bucketName,
        Key = newFolder,
        Body = file.read().decode('utf-8')
        )


#Decription: This method enables to delete file from its original location
def delete_objects(s3, bucketName, file):

    response = s3.delete_object(
        Bucket = bucketName,
        Key = file

        )

另外,请参考以下视频: https://youtu.be/7gqvV4tUxmY