是否可以清除 AWS Lambda 中的临时文件?



>我创建了一个 lambda,它将扫描上传的文件并搜索另一个 s3 存储桶中列出的特定短语。如果短语在原始上传文件中匹配,它将打印成绩单的行以及响应。

如果我们单独上传每个成绩单,这个 lambda 有效,但是如果我们上传超过 1 个,它会存储原始输出并将其添加到开头。

我觉得这个问题可能是由于 lambda 函数结束时未清除/tmp/文件引起的。

有没有办法在每次完成作业时清除/tmp/文件?

输出如下所示:

ch_0 :  Okay.  And then,  um,  how do you guys typically allocate funding for a project like this? 
-------------------------------------------------------------
ch_1 :  Yeah,  we do have capital projects and we've allocated money 3 place,  which is and stuff,  Um,  every year. 
ch_0 :  Okay.  And then,  um,  how do you guys typically allocate funding for a project like this? 
-------------------------------------------------------------
ch_1 :  Yeah,  we do have capital projects and we've allocated money 3 place,  which is and stuff,  Um,  every year. 
ch_0 :  Okay.  And then,  um,  how do you guys typically allocate funding for a project like this? 
-------------------------------------------------------------
ch_1 :  Yeah,  we do have capital projects and we've allocated money 3 place,  which is and stuff,  Um,  every year.

但是,它应如下所示:

ch_0 :  Okay.  And then,  um,  how do you guys typically allocate funding for a project like this? 
-------------------------------------------------------------
ch_1 :  Yeah,  we do have capital projects and we've allocated money 3 place,  which is and stuff,  Um,  every year. 

我的 lambda 代码如下:

import boto3
def lambda_handler(event, context):
s3 = boto3.client("s3")
if event:
file_obj = event["Records"][0]
bucketname = str(file_obj['s3']['bucket']['name'])
filename = str(file_obj['s3']['object']['key'])
job_name = filename
print("Filename: ", filename)
fileObj = s3.get_object(Bucket=bucketname, Key=filename)
file_content = fileObj["Body"].read().decode('utf-8')
budget_file = s3.get_object(Bucket= "bantp-phrases", Key="B.txt")
budget_content = budget_file["Body"].read().decode('utf-8')
authority_file = s3.get_object(Bucket= "bantp-phrases", Key="A.txt")
authority_content = authority_file["Body"].read().decode('utf-8')
need_file = s3.get_object(Bucket= "bantp-phrases", Key="N.txt")
need_content = need_file["Body"].read().decode('utf-8')
timeline_file = s3.get_object(Bucket= "bantp-phrases", Key="T.txt")
timeline_content = timeline_file["Body"].read().decode('utf-8')
partner_file = s3.get_object(Bucket= "bantp-phrases", Key="P.txt")
partner_content = partner_file["Body"].read().decode('utf-8')
# Converts all to a list
budgets = budget_content.split("n")
authorities = authority_content.split("n")
needs = need_content.split("n")
timelines = timeline_content.split("n")
partners = partner_content.split("n")
lines = file_content.split("n")
directory_name = filename
mylist = lines
#Budget Phrase Analysis
for b in budgets:
with open("/tmp/budget.txt", "a") as x:
try:
output = None
for index, line in enumerate(lines):
if b.strip() in line:
output = index
break
if output:
x.write("n" + lines[output] + "n")
x.write("-------------------------------------------------------------")
x.write("n" + lines[output +1] + "n")
print ("It worked!")
break
except (ValueError):
x.write("Nothing found")
print ("It didn't work :(")
break
s3.upload_file(Filename = "/tmp/budget.txt" , Bucket="bantp-analysis",  Key = ((directory_name)+'/'+"Budget_" + (filename)))
#Authority Phrase Analysis
for a in authorities:
with open("/tmp/authority.txt", "a") as c:
try:
output = None
for index, line in enumerate(lines):
if a.strip() in line:
output = index
if output:
c.write("n" + lines[output] + "n")
c.write("-------------------------------------------------------------")
c.write("n" + lines[output +1] + "n")
print ("It worked!")
except (ValueError):
c.write("Nothing found")
print ("It didn't work :(")
s3.upload_file(Filename = "/tmp/authority.txt" , Bucket="bantp-analysis",  Key = ((directory_name)+'/'+"Authority_") + (filename))

#Need Phrase Analysis
for n in needs:
with open("/tmp/need.txt", "a") as v:
try:
output = None
for index, line in enumerate(lines):
if n.strip() in line:
output = index
break
if output:
v.write("n" + lines[output] + "n")
v.write("-------------------------------------------------------------")
v.write("n" + lines[output +1] + "n")
print ("It worked!")
break
except (ValueError):
v.write("Nothing found")
print ("It didn't work :(")
break
s3.upload_file(Filename = "/tmp/need.txt" , Bucket="bantp-analysis",  Key = ((directory_name)+'/'+"Need_") + (filename))

#Timeline Phrase Analysis
for t in timelines:
with open("/tmp/timeline.txt", "a") as z:
try:
output = None
for index, line in enumerate(lines):
if t.strip() in line:
output = index
break
if output:
z.write("n" + lines[output] + "n")
z.write("-------------------------------------------------------------")
z.write("n" + lines[output +1] + "n")
print ("It worked!")
break
except (ValueError):
z.write("Nothing found")
print ("It didn't work :(")
break
s3.upload_file(Filename = "/tmp/timeline.txt" , Bucket="bantp-analysis",  Key = ((directory_name)+'/'+"Timeline_") + (filename))

#Partner Phrase Analysis
for p in partners:
with open("/tmp/partner.txt", "a") as q:
try:
output = None
for index, line in enumerate(lines):
if p.strip() in line:
output = index
break
if output:
q.write("n" + lines[output] + "n")
q.write("-------------------------------------------------------------")
q.write("n" + lines[output +1] + "n")
print ("It worked!")
except (ValueError):
q.write("Nothing found")
print ("It didn't work :(")
s3.upload_file(Filename = "/tmp/partner.txt" , Bucket="bantp-analysis",  Key = ((directory_name)+'/'+"Partner_") + (filename))

欢迎来到堆栈溢出!

您可以尝试以下解决方案并评论结果吗

在所有open操作中,将打开文件的模式从a更改为w

with open("/tmp/timeline.txt", "a") as z:

with open("/tmp/timeline.txt", "w") as z:

此更改适用于所有打开的操作,以覆盖现有图元文件。还要注意缩进。

最新更新