AWS Lambda-S3 put_object参数主体的无效类型

时间:2018-11-11 00:50:33

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

我在路线53中有一个托管区域,并且希望将 hostzone 对象的内容存储在S3中,但出现错误。我以为Body是正确的参数,但这也许是因为对象是JSON格式?

import boto3
import json

def allwork():
    client = boto3.client('route53')
    hostzone = client.list_hosted_zones()
    bucket_name = "testlambda"
    file_name = "r53data.txt"
    lambda_path = "/tmp/" + file_name
    s3_path = "10102018/" + file_name 

    s3 = boto3.resource("s3")
    s3.Bucket(bucket_name).put_object(Key=s3_path, Body=hostzone)

allwork()

这是错误:

module initialization error: Parameter validation failed:
Invalid type for parameter Body, value: {u'HostedZones': 
[{u'ResourceRecordSetCount': 7, u'CallerReference': '814E3.........

1 个答案:

答案 0 :(得分:1)

tkausl在评论中回答了这个问题:

Looks like it returns a dict, so you need to json encode it manually before passing it to put_object 

更新:

import boto3
import json

def allwork():
client = boto3.client('route53')
hostzone = client.list_hosted_zones()
bucket_name = "testlambda"
file_name = "r53data.txt"
lambda_path = "/tmp/" + file_name
s3_path = "10102018/" + file_name 

hostzone2=json.dumps(hostzone, ensure_ascii=False)

s3 = boto3.resource("s3")
s3.Bucket(bucket_name).put_object(Key=s3_path, Body=hostzone2)

allwork()