Python 当我试图运行lambda函数时,我遇到了以下错误,错误是第7行的缩进错误

Python 当我试图运行lambda函数时,我遇到了以下错误,错误是第7行的缩进错误,python,python-2.7,amazon-web-services,aws-lambda,boto3,Python,Python 2.7,Amazon Web Services,Aws Lambda,Boto3,我在第7行得到了s3 bucket的缩进错误,表示语法错误,我正在尝试运行lambda函数,当根据sns主题和cloudwatch日志将新对象上传到s3 bucket时,该函数基本上将s3 bucket数据从源复制到目标 import urllib import boto3 import ast import json print('Loading function') def lambda_handler(event, context): s3 = boto3.client('s3') sns

我在第7行得到了s3 bucket的缩进错误,表示语法错误,我正在尝试运行lambda函数,当根据sns主题和cloudwatch日志将新对象上传到s3 bucket时,该函数基本上将s3 bucket数据从源复制到目标

import urllib
import boto3
import ast
import json
print('Loading function')
def lambda_handler(event, context):
s3 = boto3.client('s3')
sns_message = ast.literal_eval(event['Records'][0]['Sns']['Message'])
target_bucket = context.function_name
source_bucket = str(sns_message['Records'][0]['s3']['bucket']['name'])
key = str(urllib.unquote_plus(sns_message['Records'][0]['s3']['object']['key']).decode('utf8'))
copy_source = {'Bucket':source_bucket, 'Key':key}
print ("Copying %s from bucket %s to bucket %s ..." % (key, source_bucket, target_bucket))
sts_client = boto3.client('sts')
assumedRoleObject = sts_client.assume_role(
    RoleArn="arn:aws:iam::lambdadestd3s:role/lambdasource",
    RoleSessionName="AssumeRoleSession1"
)
credentials = assumedRoleObject['Credentials']
#use of temporary credentials for sts role
s3 = boto3.client(
    's3',
    aws_access_key_id = credentials['AccessKeyId'],
    aws_secret_access_key = credentials['SecretAccessKey'],
    aws_session_token = credentials['SessionToken'],
)
s3.copy_object(Bucket=target_bucket, Key=key, CopySource=copy_source)

这是因为在定义函数后没有缩进下一行。它看起来像这样:

def lambda_handler(event, context):
        s3 = boto3.client('s3')
        sns_message = ast.literal_eval(event['Records'][0]['Sns']['Message'])
        target_bucket = context.function_name

这是因为第7行需要4个空格,请阅读工作原理。