Move/copy data from one folder to another on AWS S3 Move/copy data from one folder to another on AWS S3 python python

Move/copy data from one folder to another on AWS S3


import boto3s3 = boto3.resource('s3')copy_source = {    'Bucket': 'staging',    'Key': 'AwsTesting/research/filename.csv'}s3.meta.client.copy(copy_source, 'staging', 'AwsTesting/')


An alternative to using cp with the CLI is sync - https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html

aws s3 sync s3://mybucket s3://mybucket2

It will essentially do the same thing.


Use the following snippet which is working.

def s3candidateavtarcopy(old,new):try:    response = s3.list_objects_v2(Bucket = s3_candidate_bucket,Prefix=old)    keycount = response['KeyCount']    if(keycount > 0):        for key in response['Contents']:            file = key['Key']            try:                output = file.split(old)                newfile = new + output[1]                input_source = {'Bucket': s3_candidate_bucket,'Key' : file }                s3_resource.Object(s3_candidate_bucket,newfile).copy_from(CopySource=input_source)            except ClientError as e:                print(e.response['Error']['Message'])            else:                print('Success')    else:        print('No matching records')except ClientError as e:    print(e.response['Error']['Message'])else:    print('Operatio completed')