Move files between two AWS S3 buckets using boto3

后端 未结 7 1758
难免孤独
难免孤独 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:23

    I think the boto S3 documentation answers your question.

    https://github.com/boto/boto/blob/develop/docs/source/s3_tut.rst

    Moving files from one bucket to another via boto is effectively a copy of the keys from source to destination and then removing the key from source.

    You can get access to the buckets:

    import boto
    
    c = boto.connect_s3()
    src = c.get_bucket('my_source_bucket')
    dst = c.get_bucket('my_destination_bucket')
    

    and iterate the keys:

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

    See also: Is it possible to copy all files from one S3 bucket to another with s3cmd?

    0 讨论(0)
提交回复
热议问题