Move files between two AWS S3 buckets using boto3

后端 未结 7 1798
难免孤独
难免孤独 2020-12-01 07:52

I have to move files between one bucket to another with Python Boto API. (I need it to \"Cut\" the file from the first Bucket and \"Paste\" it in the second one). What is th

7条回答
  •  悲&欢浪女
    2020-12-01 08:05

    If you have 2 different buckets with different access credentials. Store the credentials accordingly in credentials and config files under ~/.aws folder.

    you can use the following to copy object from one bucket with different credentials and then save the object in the other bucket with different credentials:

    import boto3
    
    
    session_src = boto3.session.Session(profile_name=)
    source_s3_r = session_src.resource('s3')
    
    session_dest = boto3.session.Session(profile_name=)
    dest_s3_r = session_dest.resource('s3')
    
    # create a reference to source image
    old_obj = source_s3_r.Object(,  + )
    
    # create a reference for destination image
    new_obj = dest_s3_r.Object(, old_obj.key)
    
    # upload the image to destination S3 object
    new_obj.put(Body=old_obj.get()['Body'].read())
    

    Both bucket do not need to have accessibility from each other in the ACL or the bucket policies.

提交回复
热议问题