简体   繁体   中英

AWS Lambda Python - copying keys to a "folder" creates nested "folders". How do I exclude a "folder" from being monitored

This is my first post so I apologize for any inconstancies. What I am trying to do is, copy an uploaded file to the destination bucket, copy that file to a processed folder within the source bucket and delete the original file. I need to exclude the processed folder because it is being copied over from the source folder and creating nested processed folders. But I am having problems figuring out how to exclude that folder.

import json
import boto3

s3 = boto3.resource('s3')

def lambda_handler(event, context):
    bucket = s3.Bucket('source-test-bucket-007')
    dest_bucket = s3.Bucket('destination-test-bucket-008')

    # Variables for processed key into the processed folder
    new_bucket_name = 'source-test-bucket-007'

    
    for obj in bucket.objects.all():
        dest_key = obj.key
        print('Keys :', obj.key)
        s3.Object(dest_bucket.name, dest_key).copy_from(CopySource = {'Bucket': obj.bucket_name, 'Key': obj.key})
        s3.Object(obj.bucket_name, obj.key).delete()
        copy_source_back = {'Bucket': new_bucket_name, 'Key': obj.key}
        s3.meta.client.copy(copy_source_back, new_bucket_name, 'processed/' + obj.key)
        
    return {
        'Status': 200,
        'body': ('Test with no errors.')
    }

If I assume you are saying is you want to copy something like:

key = 'processed/copythisfile.txt'

to a new a bucket and it's showing up as:

newkey = 'processed/processed/copythisfile.xt'

Depending on prefixes (folders), if process is the only folder being duplicated, can you just leave that out of this line:

s3.meta.client.copy(copy_source_back, new_bucket_name, 'processed/' + obj.key)

But if you really only want the file name you can split the key and grab the last element with [-1]

In [58]: key = 'processed/copythisfile.txt'
    ...:

In [59]: key.split('/')
    ...:
Out[59]: ['processed', 'copythisfile.txt']

In [60]: key.split('/')[-1]
    ...: 
    ...:
Out[60]: 'copythisfile.txt'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM