KcH
KcH

Reputation: 3502

How to copy an object from one bucket to another

I have been trying to copy an object from one bucket to another

for e.g. bucket1/sample.jpg to bucket2/some_dynamic_folder/copied_sample.jpg

here some_dynamic_folder is that , if it exists already it should copy the object under it , if not it should create and then copy

I have tried as below :

def copy_to_bucket(bucket_from_name, img_url, conventional_folder_name):
    try:
        file_name = img_url.split('/')[-1]  # to get name of image
        response = S3.copy_object(      # S3 has s3 client config
            Bucket= 'sample_bucket1' ,
            CopySource='/' + sample_bucket2 + '/' + file_name,
            Key= conventional_folder_name + '/' + file_name ,  # create a folder and copy there 
        )
        return response
    except Exception as error:
        return None

I have been getting the err as : Boto3 copy_object "No Such Key".................

Few of the images have spaces , read it might cause issue so replaced spaces using urlparse and here in the GIT hub issue mentioned this is not possible

Any guiding links or any other workaround or an alternative to make this work is highly appreciated ... TIA

Upvotes: 0

Views: 155

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269101

The CopySource should not have a slash (/) at the front.

The format is: bucket-name/path/object-name

Also, the Bucket and Key are the Destination. The source bucket and key are provided in CopySource.

Upvotes: 1

Related Questions