第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

AWS lambda 中的 Python 代碼 - 第一次出現(xiàn)失敗案例后執(zhí)行中斷

AWS lambda 中的 Python 代碼 - 第一次出現(xiàn)失敗案例后執(zhí)行中斷

Helenr 2021-12-17 16:32:26
我有一個字典,鍵為 key1、key2、key3,值為 s3 路徑,其中包含一些文件,我正在循環(huán) dict 以檢查路徑中是否存在文件。在序列中,如果文件存在于所有路徑中 - 腳本工作正常在序列中,如果最后一個文件不存在 - 腳本工作正常在序列中,如果路徑中的任何文件不存在(介于兩者之間) - 腳本跳轉(zhuǎn)到異常塊并退出,我希望在失敗情況下繼續(xù)執(zhí)行(文件不存在)我嘗試根據(jù)我的要求使用 break、continue 語句來控制執(zhí)行,但仍然沒有實現(xiàn)我想要的。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條經(jīng)驗 獲得超11個贊

您需要將拋出異常的代碼包裝在更接近的 try/catch 中。這很可能意味著換client.Bucket(...)行。如果您在循環(huán)中捕獲異常,則可以使用continue跳過該迭代。


查看完整回答
反對 回復 2021-12-17
?
慕虎7371278

TA貢獻1802條經(jīng)驗 獲得超4個贊

我的 for 循環(huán)在 try..except 塊下,我嘗試將 try..except 塊放在 for 循環(huán)下,這解決了問題。


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

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號