@@ -24,27 +24,41 @@ def lambda_handler(event, context):
2424
2525 # Get as many files from S3 as we can move this time (minimum of queueSpace and dripRate)
2626 s3Client = boto3 .client ('s3' )
27+ s3 = boto3 .resource ('s3' )
2728 maxKeys = min (dripRate , queueSpace ) + 10 # list a few additional keys to allow for some folder objects that won't be moved
2829 response = s3Client .list_objects_v2 (Bucket = sourceBucket , MaxKeys = maxKeys )
2930 if "Contents" in response :
3031 # We now have a list of objects that we can use
31- sourcePrefix = "/" + sourceBucket + "/"
3232 keyPrefix = targetAudioKey
3333 if keyPrefix != "" :
3434 keyPrefix += "/"
35- files = [f for f in response ["Contents" ] if not f ["Key" ].endswith ("/" )][:dripRate ] # ignore folder objects
35+ files = [f for f in response ["Contents" ] if not f ["Key" ].endswith ("/" )][:dripRate ]
36+ folders = [f for f in response ["Contents" ] if f ["Key" ].endswith ("/" )]
3637 for audioFile in files :
3738 try :
3839 # Copy and delete file
39- print (f'Copying: Bucket={ targetBucket } , CopySource={ (sourcePrefix + audioFile ["Key" ])} , Key={ (keyPrefix + audioFile ["Key" ])} ' )
40- copyResponse = s3Client .copy_object (Bucket = targetBucket ,
41- CopySource = (sourcePrefix + audioFile ["Key" ]),
42- Key = (keyPrefix + audioFile ["Key" ]))
40+ copyDestnKey = keyPrefix + audioFile ["Key" ]
41+ copy_source = {
42+ 'Bucket' : sourceBucket ,
43+ 'Key' : audioFile ["Key" ]
44+ }
45+ print (f'Copying: copy_source={ copy_source } , targetBucket={ targetBucket } , copyDestnKey={ copyDestnKey } ' )
46+ s3 .meta .client .copy (copy_source , targetBucket , copyDestnKey )
4347 deleteResponse = s3Client .delete_object (Bucket = sourceBucket , Key = audioFile ["Key" ])
4448 movedFiles += 1
4549 except Exception as e :
4650 print ("Failed to move audio file {}" .format (audioFile ["Key" ]))
4751 print (e )
52+ raise
53+ # Delete any folder objects so they do not appear in subsequent object lists - usually these are created in S3 console only
54+ for folder in folders :
55+ try :
56+ print (f'Deleting folder object: Bucket={ sourceBucket } , Key={ folder ["Key" ]} ' )
57+ deleteResponse = s3Client .delete_object (Bucket = sourceBucket , Key = folder ["Key" ])
58+ except Exception as e :
59+ print ("Failed to delete folder file {}" .format (folder ["Key" ]))
60+ print (e )
61+ raise
4862
4963 # Increase our counter, remove the queue value and return
5064 sfData ["filesProcessed" ] += movedFiles
0 commit comments