简体   繁体   中英

Write out Boto3 Response in JSON Object and Upload to S3 in AWS Lambda Function

I have a Python 3.6 AWS Lambda Function I am building out to query Cost Explorer, and a few other services. I want to write out the string response I am returning into a JSON object I can either upload to S3 or into DynamoDB.

A working example of the Function is below

import boto3

def handler(event,context):
    client = boto3.client('ce')
    response = client.get_cost_forecast(
    TimePeriod={
        'Start': '2019-06-01', ## Start must be one day after creation date of function
        'End': '2020-07-01'
    },
    Metric='UNBLENDED_COST',
    Granularity='MONTHLY',
    PredictionIntervalLevel=90 ## 51 - 99 Range
    )
    return str (response)

That returns just fine in the console, now I tried this to put the Response into the Body of an object I wanted to write into S3

import boto3

def handler(event,context):
    client = boto3.client('ce')
    s3 = boto3.resource('s3')
    object = s3.Object('my-bucket', 'my/path/object.json')
    response = client.get_cost_forecast(
    ---ce python stuff---
    object.put(Body=response)
    return str (response)

That is not working as expected, and I am getting this error from CloudWatch

Parameter validation failed:
Invalid type for parameter Body, value: {'Total': {'Amount': '...RESPONSE...'}, 'RetryAttempts': 0}}, type: <class 'dict'>, valid types: <class 'bytes'>, <class 'bytearray'>, file-like object: ParamValidationError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 17, in handler
object.put(Body=response)
File "/var/task/boto3/resources/factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "/var/task/boto3/resources/action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
File "/var/task/botocore/client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/var/task/botocore/client.py", line 634, in _make_api_call
api_params, operation_model, context=request_context)
File "/var/task/botocore/client.py", line 682, in _convert_to_request_dict
api_params, operation_model)
File "/var/task/botocore/validate.py", line 297, in serialize_to_request
raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter Body, value: {'Total': {'Amount': '...RESPONSE...'}, 'RetryAttempts': 0}}, type: <class 'dict'>, valid types: <class 'bytes'>, <class 'bytearray'>, file-like object

I thought I could just stuff that into the object and write it out. Is there a better way to do what I am trying to do?

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Object.put

Body (bytes or seekable file-like object) -- Object data.

So try sth like Body=json.dumps(response).encode()

Your response is a dict object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM