我在第7行发现s3存储桶出现缩进错误,提示语法错误,我正在尝试运行lambda函数,当将新对象基于sns上传到s3存储桶时,基本上将s3存储桶数据从源复制到目标主题和cloudwatch日志。
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)
答案 0 :(得分:0)
这是因为在定义函数后,您没有缩进下一行。看起来像这样:
def lambda_handler(event, context):
s3 = boto3.client('s3')
sns_message = ast.literal_eval(event['Records'][0]['Sns']['Message'])
target_bucket = context.function_name