2

I wrote a lambda function to download the csv file to s3 and created API Gateway

import os
import uuid
import boto3
from botocore.exceptions import ClientError

SECRET_KEY = os.environ["SECRET_KEY"]
ACCESS_KEY = os.environ["ACCESS_KEY"]

def lambda_handler(event, context):
    # TODO implement
    bucket = event['bucket']
    object_name = '{}.csv'.format(uuid.uuid1())
    file_name = event['filename']

    s3_client = boto3.client('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
    try:
        response = s3_client.upload_file(file_name, bucket, object_name)
    except ClientError as e:
        logging.error(e)
        return e
return True

When I run this code using the test event directly through the aws interface

test case

{
  "bucket": "testcsvupload-drv-cvbx",
  "filename": "file.csv",
  "key3": "value3"
}

I got successful result. But then I trying using curl request fron linux console

curl -X POST https://tvomerrrcfa.execute-api.us-east-1.amazonaws.com/file_to_s3  -H "Content-Type: multipart/form-data" -F 'filename=@/home/y700/projects/start/file.csv'

I get an error

{"message": "Could not parse request body into json: Unexpected character (\'-\' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value\n at [Source: (byte[])\"--------------------------e6b7a9fa3c5b61d0\r\nContent-Disposition: form-data; name=\"filename\"; filename=\"file.csv\"\r\nContent-Type: application\/octet-stream\r\n\r\nname,surname,age\nTim,Bert,19\nHelen,Glad,23\n\r\n--------------------------e6b7a9fa3c5b61d0--\r\n\"; line: 1, column: 3]"}

file.csv

name,surname,age
Tim,Bert,19
Helen,Glad,23

What am I doing wrong? And why is json referenced in the error?

Jekson
  • 2,892
  • 8
  • 44
  • 79
  • 1
    Not sure how your api gateway is set up? you may need to configure this to handle multipart, similar to https://stackoverflow.com/questions/41756190/api-gateway-post-multipart-form-data – LostJon Nov 25 '19 at 19:06
  • 2
    Side-note: There should be no need to extract an Access Key/Secret Key from environment variables within an AWS Lambda function. You can simply assign an IAM Role to the Lambda function and it will obtain the credentials itself. – John Rotenstein Nov 26 '19 at 04:52
  • @JohnRotenstein Thank you for your advice! Will it also work when using the Gataway API? – Jekson Nov 26 '19 at 05:54
  • @JohnRotenstein What do you think is the cause of the problem? Is my code written correctly? Does this look like an AWS Gataway configuration problem? – Jekson Nov 26 '19 at 05:56

0 Answers0