模块'lambda_function'中的语法错误:unindent与任何外部缩进级别都不匹配(lambda_function.py,第26行)

时间:2018-04-04 06:02:37

标签: python aws-lambda

  1. 来自 future import print_function import json import boto3
    导入时间导入urllib打印('加载功能')        s3 = boto3.client('s3')

       def lambda_handler(event, context):
      bucket = event['Records'][0]['s3']['bucket']['name']
      key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'])
      try:
          print("Using waiter to waiting for object to persist thru s3 service")
          waiter = s3.get_waiter('object_exists')
          waiter.wait(Bucket+bucket, Key=key)
          response = s3.head_object(Bucket=bucket, Key=key)
          print("CONTENT TYPE: " +response['ContentType'])
          print("Etag: " + response['ETag'])
          print("Content-Length: ", response['ContentLength'])
          print("Keyname: "+key)
          print("Deleting object" + key)
          s3.delete_object(Bucket=bucket, Key=key)
          return response['ContentType']
       except Exception as e
           print(e)
           print('Error getting object{} from bucket {} . Make sure they exist'
           'bucket is in same region as this is'
           'function.' ,format(key, bucket))
           raise e
    

2 个答案:

答案 0 :(得分:2)

您的缩进不正确,就像异常所说的那样。

def lambda_handler(event, context):
   # indented by 3 spaces
   try:
       ...
    # indented by 4 spaces ... 
    except Exception as e  # NOTE: Missing `:`
        ...

您的代码示例在异常处理程序的末尾也缺少:

答案 1 :(得分:-2)

就在这里:

           print("Content-Length: ", response['ContentLength'])
           print("Keyname: "+key)
           print("Deleting object" + key)
           s3.delete_object(Bucket=bucket, Key=key)
           return response['ContentType']
        except Exception as e: #<---also add me, the colon :)
            print(e)
            print('Error getting object{} from bucket {} . Make sure they exist'
            'bucket is in same region as this is'
            'function.' ,format(key, bucket))
            raise e

除了需要移回一个空格之外的东西的缩进。