[英]Moving file based on filename with Amazon S3
需要根据名称将 Amazon S3 文件移动到名称相同的相应文件夹中。 这适用于 AWS 上 python 中的自动化脚本。 文件名递增 1。
例如,一个将被称为my_file_section_a
,然后下一个将是my_file_section_b1
,然后下一个将是my_file_section_b2
等等。 这些文件夹将被称为my_file_section_a
、 my_file_section_b1
等等。
这是代码:
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
如何将基于名称的文件移动到名称相同的文件夹中?
新文件夹是不带扩展名的文件名。 根据 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'
这里是一个AWS Lambda function,它将一个object复制到一个同名目录,然后删除原来的ZA8CFDE6331BD54B6F666C。
它只会从bucket的根目录复制对象,避免复制的object再次触发Lambda function,造成死循环。 (Amazon S3 很大,但不是无限的)如果您的目标是不同的存储桶。 这将没有必要。
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
)
function 需要一个 IAM 角色,该角色具有对存储桶的GetObject
和PutObject
(可能更多?)权限。 创建此 AWS Lambda function 后,在 Amazon S3 存储桶上创建一个事件以在创建 ZA8CFDE63311C4BEB66 时触发 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.