Api gw S3 put file without name on the path

0

Need to upload file on S3 by API gw, but without key (file name) on path on API gw. I need create automatically file name with timestamp.

Luca_S
asked 3 months ago206 views
2 Answers
0

To upload a file to S3 via API Gateway without specifying the file name in the path, you can use a Lambda function to generate the file name with a timestamp.

Steps:

  1. Create a POST endpoint in API Gateway that accepts file uploads.
  2. Configure API Gateway to trigger a Lambda function when a POST request is received.
  3. Lambda Function:
    • Generates a unique file name using a timestamp.
    • Saves the file to S3 with the generated name.

Simple Lambda Function Example (Python):

import boto3
from datetime import datetime

s3 = boto3.client('s3')

def lambda_handler(event, context):
    file_content = event['body'].encode('utf-8')
    timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
    file_name = f"uploaded_file_{timestamp}.txt"
    s3.put_object(Bucket='your-s3-bucket', Key=file_name, Body=file_content)
    
    return {
        'statusCode': 200,
        'body': f'File uploaded successfully with name {file_name}'
    }

API Gateway Configuration:

  1. Create a POST method on your API resource.
  2. Set up the integration with the created Lambda function.
  3. Enable binary support if uploading non-text files.

Test:

  • Use Postman or cURL to send a POST request to the API Gateway endpoint with the file data.

This way, you can upload a file to S3 through API Gateway without needing to specify the file name in the path.

profile picture
Vava
answered 3 months ago
  • It's possibile without lambda? API GW will have a input file in gzip. Maybe I can read file name and override "key" for S3 PutObject?

0

It's possible without using Lambda. You can configure the API Gateway to directly put the object in S3 with a custom integration.

Steps to achieve this:

  1. Create a PUT method in API Gateway:

    • In the API Gateway console, create a PUT method for the desired resource.
  2. Configure integration for S3:

    • Choose "AWS Service" for the integration type.
    • Set the service to "S3".
    • Specify the action as "PutObject".
    • Provide the S3 bucket name and the key for the object.
  3. Add mapping templates:

    • Configure the mapping templates to include the file name in the S3 key. You can use integration variables to obtain the file name from the request header or path.
  4. Set up IAM permissions:

    • Create an IAM policy that allows API Gateway to perform the s3:PutObject action on the specified bucket.
    • Attach this policy to the API Gateway execution role.

Example Mapping Template for the key:

  • In the API Gateway console, for the PUT method, set up a mapping template for the S3 key.
  • Use the variable {filename} to represent the file name you want to use.
  - Set "bucket" to your S3 bucket name.
  - Set "key" to `{filename}`.

Example IAM Policy:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "s3:PutObject",
      "Resource": "arn:aws:s3:::your-bucket-name/*"
    }
  ]
}

Testing the Setup:

  1. Use tools like Postman or cURL to send a PUT request to the API Gateway endpoint with the gzip file and the header containing the file name.
  2. Make sure to include the correct Content-Type header for gzip (e.g., application/gzip).

This way, you can upload a file directly to S3 through API Gateway without needing Lambda, as long as the file name is correctly provided in the PUT request.

profile picture
Vava
answered 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions