My question is relevant to the previous one copy files from one AWS/S3 bucket to another bucket on databricks. I created a new thread because this question is different from the previous one.
This post AWS S3 copy files and folders between two buckets does not help me.
I need to copy some files from one AWS/S3 bucket/folder to another AWS/S3 bucket folder by python on databricks.
My source S3 bucket/folder is like :
source_s3_bucket
folder_name1
folder_name2
folder_name3
folder_name4
deepest_folder_name
file1
file2
....
file11500
The destination s3 bucket/folder:
destination_s3_bucket
dest_folder_name1
dest_folder_name2
dest_folder_name3
deepest_folder_name (this folder name must be exactly same as the source one "deepest_folder_name")
file1
file2
....
file11500
Also, the "dest_folder_nameX" are all different from the sources ones and also the depth of the source and destination folders are also different. But, the deepest folder name in source bucket must be kept in destination bucket.
All files must be exactly copied and keep the same names.
I have tried to do the python3 coding:
import boto3
s3 = boto3.client('s3')
s3_resource = boto3.resource('s3')
for key in s3.list_objects(Bucket=source_bucket, Prefix=source_prefix)['Contents']:
files = key['Key']
copy_source = {'Bucket': source_bucket,'Key': files}
s3_resource.meta.client.copy(CopySource=copy_source, Bucket=dest_bucket, Key=dest_prefix)
But, no files are copied to the destination folder and also how I can keep the "deepest_folder_name" ?
UPDATE "The deepest folder" means that I have to keep that layer's sub-folders' names and copy them and the files located in them to the destination.
for example, in source bucket:
folder_name_abc
folder_name_dfr
folder_name_typ # this folder names must be kept
file1
file2
In destination bucket:
folder_name_typ # this folder names must be exactly same as the source
file1
file2
thanks