我有一个字典,键为 key1、key2、key3,值为 s3 路径,其中包含一些文件,我正在循环 dict 以检查路径中是否存在文件。
在序列中,如果文件存在于所有路径中 - 脚本工作正常
在序列中,如果最后一个文件不存在 - 脚本工作正常
在序列中,如果路径中的任何文件不存在(介于两者之间) - 脚本跳转到异常块并退出,我希望在失败情况下继续执行(文件不存在)
我尝试根据我的要求使用 break、continue 语句来控制执行,但仍然没有实现我想要的。
import boto3
import botocore, os, datetime, csv
from io import StringIO
import time, json
from datetime import timedelta
def lambda_handler(event, context):
client = boto3.resource('s3')
s3 = boto3.client('s3')
TS = datetime.datetime.today().strftime('%Y%m%d')
st = datetime.datetime.now()+ timedelta(hours = 5.5)
st = st.strftime('%Y-%m-%d %H:%M:%S')
Buck = 'mybuck'
Feed = {"key1": "test/Disk_space1_"+TS+"_0001"+".PNG",
"key2": "EC2/EC2_InstanceID_Input_File.csv",
"key3": "EC2/test2/AWSError.PNG"}
try:
for key, value in Feed.items():
print(value)
obj = client.Bucket(Buck).Object(value).load()
#print(obj)
if obj is None:
print(obj)
contents = st +' '+ key+ ' '+'File-exists!'
target_bucket = 'mybuck'
target_file = 'EC2/hello.csv'
open('/tmp/test.txt', 'a+').write(contents)
open('/tmp/test.txt', 'a+').write('\r\n')
s3.upload_file('/tmp/test.txt', Buck, target_file)
except botocore.exceptions.ClientError as error:
contents1 = st +' '+ key+ ' '+'File-doesnot-exists!'
print('File does not exists in path:',value+' '+'ErrMsg:',error)
open('/tmp/test.txt', 'a+').write(contents1)
open('/tmp/test.txt', 'a+').write('\r\n')
s3.upload_file('/tmp/test.txt', Buck, target_file)
回首忆惘然
慕虎7371278
相关分类