Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AWS lambda中的Python代码-第一次出现失败案例后执行中断_Python_Amazon Web Services_Aws Lambda - Fatal编程技术网

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

AWS lambda中的Python代码-第一次出现失败案例后执行中断,python,amazon-web-services,aws-lambda,Python,Amazon Web Services,Aws Lambda,我有一个字典,其中键为key1、key2、key3,值为s3路径,其中包含一些文件,我正在循环dict以检查路径中是否存在文件 在序列中,如果所有路径中都存在文件-脚本工作正常 在序列中,如果最后一个文件不存在-脚本工作正常 在序列中,如果路径中不存在任何一个文件(介于两者之间),脚本将跳转到异常块并退出,我希望在出现故障后继续执行(文件不存在) 我试着在中间使用break、continue语句来按照我的要求控制执行,但仍然没有达到我想要的效果 import boto3 import botoc

我有一个字典,其中键为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)
    

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

    我的for循环处于try..except block下,我尝试将try..except block置于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)
    

    谢谢你的回复!我试图引入try,但for循环下的块帮助了我,它将给出我的更新代码作为答案。你应该将此问题标记为已接受,而不是使用@Daniel的解决方案创建自己的答案!我没有用Daniel的解决方案创建答案。。请仔细看看,我的答案与他的解决方案不同。你的答案是将try/except移动到for循环中,这正是我的答案建议你做的。为什么我的问题被否决了,我已经自己努力解决这个问题,我已经展示了我所做的尝试以及对我有效的方法。据我所知,我已经遵循了指导方针,请不要通过否决投票来降低我们的积极性。