0

I am trying to upload multiple images in s3 from react application using aws api gateway. I have tried below approach:

  1. Setup api gateway which target to lambda function.
  2. lambda function code:
    
  3. import json
    import boto3
    
    
    def lambda_handler(event, context):
        print(event)
        s3 = boto3.client('s3', region_name='us-east-1')
        bucket_name = 'testimagesbucketupload'
    
        URL = s3.generate_presigned_post(
                Bucket= bucket_name, 
                Key="${filename}", 
    
                # Conditions=[
                #     ["starts-with", "$success_action_redirect", ""],
                #     ["eq", "$userid", "test"],
                #     ], 
                ExpiresIn=3600)
        data = {"url": URL['url'], "fields": URL['fields']}
        print(type(data))
        # print(data)
        return data
    
    

Using above code i am able to upload single image from web and postman both but now i want to upload multiple image using this url and also want to retrieve image for preview..

If any one worked please help me

Thanks in advance..

I tried presigned_post and presigned-url for achieve this but still i am not able to achieve this

2
  • where are the images that you're trying to upload? Commented Feb 13, 2023 at 14:46
  • we working on one application in which we have functionality to capture and attach images after capture we need to send all images in s3 but for security purpose we are using presigned url Commented Feb 13, 2023 at 18:11

1 Answer 1

1

You'll need to create one url for image, but you can use a loop to create all of them. I think something like this could work for you

import boto3

def lambda_handler(event, context):

    s3 = boto3.client('s3', region_name='us-east-1')
    bucket_name = 'testimagesbucketupload'
    
    image_list = event['image_list'] 

    data = []
    for image in image_list:
        URL = s3.generate_presigned_post(
                Bucket= bucket_name, 
                Key=image, 
                ExpiresIn=3600)
        data.append({"url": URL['url'], "fields": URL['fields']})

    return data

Note that you need to pass a list of images in the event

For the preview, you could use a presigned url to return the image as a public url...

from botocore.client import Config
import boto3

s3 = boto3.client('s3', config=Config(signature_version='s3v4'), region_name = "your_region")

presigned_url = s3.generate_presigned_url('get_object',
                                Params={'Bucket': "your_bucket",
                                        'Key': "your_file_key"},
                                ExpiresIn=3600)
presigned_url
Sign up to request clarification or add additional context in comments.

3 Comments

For multiple image: Now my flow is like this : 1. when i hit my first post api then i will get url in response with fileds 2. In second step i sent image with post url which i got through from 1st step so sending the list of image in first step is the correct approach ? because same thing we also achieve through presigned url to generate different url for each image any other way in which we upload multiple/bulk image
You can modify your code to work with a list of images/url so you need to make fewer requests to your API. You'll get the same results but in a more efficient way
if you have any example can you add it here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.