【发布时间】:2019-01-15 22:38:37
【问题描述】:
我在 s3 存储桶的第 7 行收到缩进错误,说语法错误,我正在尝试运行 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)
【问题讨论】:
-
那是因为第 7 行需要 4 个空格
-
请阅读Python Indentation 的工作原理。
标签: python python-2.7 amazon-web-services aws-lambda boto3