DataGuy
DataGuy

Reputation: 1725

copy one file from one S3 bucket to a key within another S3 bucket

I am using boto3 and have two s3 buckets:

source s3 bucket: 'final-files'
destination s3 bucket: 'exchange-files'
prefix for destination s3 bucket: 'timed/0001'
key: final_file[-1] #this is assigned earlier and is a file name

I need to copy a single file from the source bucket to a folder within the destination bucket and Im unsure how to add the prefix to the destination; here's my code:

#create a source dictionary that specifies bucket name and key name of the object to be copied
copy_source = {
'Bucket': 'final-files',
'Key': final_file[-1] 
}
bucket = s3.Bucket('exchange-files')
prefix="timed/0001"
bucket.copy(copy_source, prefix + key)

# Printing the Information That the File Is Copied.
print('Single File is copied')

This is the error Im getting:

"errorMessage": "expected string or bytes-like object, got 's3.Bucket'"

What am I missing?

Upvotes: 0

Views: 60

Answers (1)

Vlam
Vlam

Reputation: 1798

Please try the following code instead and see if it works. Code is based on the example at https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/copy.html.

import boto3

s3 = boto3.resource('s3')
copy_source = {
    'Bucket': 'final-files',
    'Key': final_file[-1]
}
prefix="timed/0001"
s3.meta.client.copy(copy_source, 'exchange-files', prefix + final_file[-1])

Upvotes: 0

Related Questions