亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號