Branco François
Branco François

Reputation: 73

Change directory of xlsx file in s3 bucket using AWS Lambda

The goal of my code is to change the directory of a file every 24 hours (because every day a new one is created with another lambda function). I want to get the current file from my s3 bucket and write it to another directory in the same s3 bucket. Currently, this line of the code does not work: s3.put_object(Body=response, Bucket=bucket, Key=fileout) and I get this error: "errorMessage": "Parameter validation failed:\nInvalid type for parameter Body, "errorType": "ParamValidationError" What does the error mean and what is needed in order to be able to store the response in the history directory?

import boto3
import json
s3 = boto3.client('s3')
bucket = "some-bucket"
    
def lambda_handler(event, context):
    file='latest/some_file.xlsx'
    response = s3.get_object(Bucket=bucket, Key=file)
    fileout = 'history/some_file.xlsx'
    s3.put_object(Body=response, Bucket=bucket, Key=fileout)   
    
    return {
        'statusCode': 200,
        'body': json.dumps(data),
    }

Upvotes: 0

Views: 321

Answers (1)

GabrielBoehme
GabrielBoehme

Reputation: 322

The response variable in your code stores more than just the actual xlsx file. You should get the body from the response and pass it to the put object method.

response = s3.get_object(Bucket=bucket, Key=file)['Body']

Upvotes: 1

Related Questions