我正在编写一个Lambda函数来创建一个文本文件并上传到一个特定的S3目录。在
import boto3
import os
def lambda_handler(event, context):
src_bucket = event['Records'][0]['s3']['bucket']['name']
filepath = event['Records'][0]['s3']['object']['key']
head, tail = os.path.split(filepath)
new_head = head.replace("/", "").upper()
new_filename = "_".join((new_head, tail))
s3_client = boto3.client('s3')
s3 = boto3.resource('s3')
string = "dfghj"
encoded_string = string.encode("utf-8")
file_name = "_".join(('ErrorLog', os.path.splitext(new_filename)[0]+'.txt'))
print(file_name)
s3_path = (head + '/errorlog/') + file_name
print(s3_path)
s3.Bucket(src_bucket).put_object(Key=s3_path, Body=encoded_string)
执行时没有任何错误。但是,它会进入一个无限循环,并继续使用errorlog/filename创建子文件夹。在
例如,如果文件_name=“ErrorLog_test1.txt”和s3_path=“folder1/ErrorLog/ErrorLog_test1.txt”,它会在其内部用文件名创建子文件夹“ErrorLog”。比如“folder1/errorlog/errorlog/errorlog/errorlog_test1.txt”
如何阻止它创建递归文件夹?我认为我在设置s3u路径对象时出错了。在
似乎您已经在amazons3bucket上配置了一个事件,以便在创建对象时触发Lambda函数。在
创建对象时,将触发Lambda函数。Lambda函数创建一个S3对象。这个触发器和事件,它触发Lambda函数,它创建一个对象。。。是的,它是一个无限循环,直到达到某个极限。在
我不确定您正在对这些对象执行什么操作,但最安全的方法是将事件配置为仅针对给定路径的触发器(子文件夹)。然后,配置Lambda函数以在不同的路径中创建新文件,这样它就不会再次触发事件。在
相关问题 更多 >
编程相关推荐