简体   繁体   English

使用 Amazon S3 根据文件名移动文件

[英]Moving file based on filename with Amazon S3

Need to move Amazon S3 files based on their names into appropriate folders, that are named the same .需要根据名称将 Amazon S3 文件移动到名称相同的相应文件夹中 This is for an automation script in python on AWS.这适用于 AWS 上 python 中的自动化脚本。 The file names increment by 1.文件名递增 1。

For instance, one would be called my_file_section_a , then the next would be my_file_section_b1 , then the next would be my_file_section_b2 and so forth.例如,一个将被称为my_file_section_a ,然后下一个将是my_file_section_b1 ,然后下一个将是my_file_section_b2等等。 The folders would be called my_file_section_a , my_file_section_b1 and so forth.这些文件夹将被称为my_file_section_amy_file_section_b1等等。

Here is the code:这是代码:

from __future__import print_function
import boto3
import time, urllib
import json

print("*"*80)
print("Initializing...")
print("*"*80)

s3 = boto3.client('s3')

def lambda_handler(event, context):

    source_bucket = event['Records'[0]['s3']['bucket']['name']
    object_key = event['Records'][0]['s3']['object']['key']
    target_bucket = 'myfilelambdadestination'
    copy_source = {'Bucket': source_bucket, 'Key': object_key}
    print("Source bucket: ", source_bucket)
    print("Target bucket: ", target_bucket)
    print("Log Stream name: ",context.log_stream_name)
    print("Log Group name: ",context.log_group_name)
    print("Request ID: ",context.aws_request_id)
    print("Mem. limits(MB) ", context.memory_limit_in_mb)
    try:
        print("Using waiter to waiting for object to persist through s3 service")
        waiter = s3.get_waiter('object_exists')
        waiter.wait(Bucket=source_bucket, Key = object_key)
        s3.copy_object(Bucket=target_bucket, Key = object_key, CopySource = copy_source)
        return response['ContentType']
    except Exception as err:
        print("Error -" +str(err))
        return e

How do I have it that the files based on their names are moved to folders which are also named the same?如何将基于名称的文件移动到名称相同的文件夹中?

new folder is file name without the extension.新文件夹是不带扩展名的文件名。 depending on what object_key looks like you may have tweak this code根据 object_key 的外观,您可能已经调整了此代码

object_key = 'my_file_section_b2.txt'
new_object_prefix = object_key.rsplit('/', 1)[-1].split('.')[0]
new_object_key = new_object_prefix + '/' + object_key
new_object_key
'my_file_section_b2/my_file_section_b2.txt'

Here is an AWS Lambda function that will copy an object to a directory of the same name, then delete the original object.这里是一个AWS Lambda function,它将一个object复制到一个同名目录,然后删除原来的ZA8CFDE6331BD54B6F666C。

It will only copy objects from the root of the bucket, to avoid a situation where the copied object triggers the Lambda function again, causing an infinite loop.它只会从bucket的根目录复制对象,避免复制的object再次触发Lambda function,造成死循环。 (Amazon S3 is big, but not infinite,) If your target is a different bucket. (Amazon S3 很大,但不是无限的)如果您的目标是不同的存储桶。 this will not be necessary.这将没有必要。

import boto3
import urllib

def lambda_handler(event, context):
    
    # Get the bucket and object key from the Event
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'])
    
    # Only copy objects that were uploaded to the bucket root (to avoid an infinite loop)
    if '/' not in key:
        
        # Copy object
        s3_client = boto3.client('s3')
        s3_client.copy_object(
            Bucket = bucket,
            Key = f"{key}/{key}",
            CopySource= {'Bucket': bucket, 'Key': key}
        )
        
        # Delete source object
        s3_client.delete_object(
            Bucket = bucket,
            Key = key
        )

The function requires an IAM Role with permission for GetObject and PutObject (maybe more?) on the bucket. function 需要一个 IAM 角色,该角色具有对存储桶的GetObjectPutObject (可能更多?)权限。 After creating this AWS Lambda function, create an Event on the Amazon S3 bucket to trigger the function when an object is created.创建此 AWS Lambda function 后,在 Amazon S3 存储桶上创建一个事件以在创建 ZA8CFDE63311C4BEB66 时触发 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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