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 the best way to do that?
** Note: Is that matter if I have two different ACCESS KEYS and SECRET KEYS?
import boto3 s3 = boto3. resource('s3') copy_source = { 'Bucket': 'mybucket', 'Key': 'mykey' } bucket = s3. Bucket('otherbucket') bucket. copy(copy_source, 'otherkey') # This is a managed transfer that will perform a multipart copy in # multiple threads if necessary.
If the object that you can't copy between buckets is owned by another account, then the object owner can do one of the following: The object owner can grant the bucket owner full control of the object. After the bucket owner owns the object, the bucket policy applies to the object.
If you are using boto3 (the newer boto version) this is quite simple
import boto3 s3 = boto3.resource('s3') copy_source = { 'Bucket': 'mybucket', 'Key': 'mykey' } s3.meta.client.copy(copy_source, 'otherbucket', 'otherkey')
(Docs)
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?
awscli does the job 30 times faster for me than boto coping and deleting each key. Probably due to multithreading in awscli. If you still want to run it from your python script without calling shell commands from it, you may try something like this:
Install awscli python package:
sudo pip install awscli
And then it is as simple as this:
import os
if os.environ.get('LC_CTYPE', '') == 'UTF-8':
os.environ['LC_CTYPE'] = 'en_US.UTF-8'
from awscli.clidriver import create_clidriver
driver = create_clidriver()
driver.main('s3 mv source_bucket target_bucket --recursive'.split())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With