【问题标题】:i am getting following error when i am trying to run lambda function,error is indentation error at line no7当我尝试运行 lambda 函数时出现以下错误,错误是第 7 行的缩进错误
【发布时间】: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)

【问题讨论】:

标签: python python-2.7 amazon-web-services aws-lambda boto3


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2023-02-04
    • 2019-01-26
    • 2021-12-21
    • 2011-05-29
    • 2022-01-15
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多