为了账号安全,请及时绑定邮箱和手机立即绑定

AWS lambda 中的 Python 代码 - 第一次出现失败案例后执行中断

AWS lambda 中的 Python 代码 - 第一次出现失败案例后执行中断

Helenr 2021-12-17 16:32:26
我有一个字典,键为 key1、key2、key3,值为 s3 路径,其中包含一些文件,我正在循环 dict 以检查路径中是否存在文件。在序列中,如果文件存在于所有路径中 - 脚本工作正常在序列中,如果最后一个文件不存在 - 脚本工作正常在序列中,如果路径中的任何文件不存在(介于两者之间) - 脚本跳转到异常块并退出,我希望在失败情况下继续执行(文件不存在)我尝试根据我的要求使用 break、continue 语句来控制执行,但仍然没有实现我想要的。import boto3import botocore, os, datetime, csvfrom io import StringIOimport time, jsonfrom datetime import timedeltadef 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)
查看完整描述

2 回答

?
回首忆惘然

TA贡献1847条经验 获得超11个赞

您需要将抛出异常的代码包装在更接近的 try/catch 中。这很可能意味着换client.Bucket(...)行。如果您在循环中捕获异常,则可以使用continue跳过该迭代。


查看完整回答
反对 回复 2021-12-17
?
慕虎7371278

TA贡献1802条经验 获得超4个赞

我的 for 循环在 try..except 块下,我尝试将 try..except 块放在 for 循环下,这解决了问题。


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"}



    for key, value in Feed.items():

        print(value)

        try:

            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)


查看完整回答
反对 回复 2021-12-17
  • 2 回答
  • 0 关注
  • 184 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号