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

我有一个字典,键为 key1、key2、key3,值为 s3 路径,其中包含一些文件,我正在循环 dict 以检查路径中是否存在文件。

  1. 在序列中,如果文件存在于所有路径中 - 脚本工作正常

  2. 在序列中,如果最后一个文件不存在 - 脚本工作正常

  3. 在序列中,如果路径中的任何文件不存在(介于两者之间) - 脚本跳转到异常块并退出,我希望在失败情况下继续执行(文件不存在)

我尝试根据我的要求使用 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)


Helenr
浏览 185回答 2
2回答

回首忆惘然

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

慕虎7371278

我的 for 循环在 try..except 块下,我尝试将 try..except 块放在 for 循环下,这解决了问题。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"}    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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python