skip to Main Content

I currently have a lambda that extracts a zip file to an s3. I would like to be able to go into the folder created by the extracted zip and copy the contents (all files and folders of the extracted zip folder) to another s3 bucket

Example:

from
src_bucket/extract/*

to
dest_bucket/*

Thanks

2

Answers


  1. Chosen as BEST ANSWER

    I can do something like this but it takes the entire folder and not the folder contents

    import boto3
    
    def lambda_handler(event, context):
        # Create an S3 client
        s3_client = boto3.client('s3')
    
        # Specify the source and destination bucket names
        source_bucket_name = 'mybucket'
        destination_bucket_name = 'mybucket2'
    
        # List objects in the source bucket
        response = s3_client.list_objects_v2(Bucket=source_bucket_name)
    
        # Iterate through the objects in the source bucket
        for obj in response['Contents']:
            key = obj['Key']
            
            # Check if the object starts with "myfolder"
            if key.startswith('myfolder'):
                # Copy the object to the destination bucket
                copy_source = {'Bucket': source_bucket_name, 'Key': key}
                s3_client.copy_object(Bucket=destination_bucket_name, Key=key, CopySource=copy_source)
    

  2. I managed to get is working with the following code. I had to use the split, to then delete the first folder name and rejoin.

    # Create an S3 client
    s3_client = boto3.client('s3')
    
    # Specify the source and destination bucket names
    source_bucket_name = 'mybucket'
    destination_bucket_name = 'mybucket2'
    
    # List objects in the source bucket
    response = s3_client.list_objects_v2(Bucket=source_bucket_name)
    
    # Find the folder containing the word "myfolder" in its name
    folder_name = None
    for obj in response['Contents']:
        key = os.path.splitext(obj['Key'])[0]
        print(key)
        
        # Check if the object is a folder containing the word "simply" in its name
        if 'myfolder' in key:
            folder_name = key
            break
    
    if folder_name is not None:
        # List objects in the folder
        response = s3_client.list_objects_v2(Bucket=source_bucket_name, Prefix=folder_name + '/')
    
        # Iterate through the objects in the folder
        for obj in response['Contents']:
            key = obj['Key']
    
            # Skip the folder itself
            if key == folder_name + '/':
                continue
    
            # Extract the filename and folders by removing the root folder
            filename = key.split('/')
            del filename[0]
            filename = "/".join(filename)
    
            # Copy the object to the root folder of the destination bucket
            new_key = filename if len(filename) > 0 else 'default_filename'
            copy_source = {'Bucket': source_bucket_name, 'Key': key}
            s3_client.copy_object(Bucket=destination_bucket_name, Key=new_key, CopySource=copy_source)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search