F128115
F128115

Reputation: 81

How I can upload batch images with name on Amazon S3 using boto?

I am uploading images to a folder currently on local . like in site/uploads. And After searching I got that for uploading images to Amazon S3, I have to do like this

import boto3

s3 = boto3.resource('s3')

# Get list of objects for indexing
images=[('image01.jpeg','Albert Einstein'),
      ('image02.jpeg','Candy'),
      ('image03.jpeg','Armstrong'),
      ('image04.jpeg','Ram'),
      ('image05.jpeg','Peter'),
      ('image06.jpeg','Shashank')
      ]

# Iterate through list to upload objects to S3   
for image in images:
    file = open(image[0],'rb')
    object = s3.Object('rekognition-pictures','index/'+ image[0])
    ret = object.put(Body=file,
                    Metadata={'FullName':image[1]}
                    )

Clarification

Its my code to send images and name to S3 . But I dont know how to get image in this line of code images=[('image01.jpeg','Albert Einstein'), like how can I get this image in this code from /upload/image01.jpeg . and 2ndly how can I get images from s3 and show in my website image page ?

Upvotes: 3

Views: 9550

Answers (3)

Mausam Sharma
Mausam Sharma

Reputation: 892

The very first thing, the code snippet you are showing as a reference is not for your use case as I had written that code snippet for batch uploads from boto3 where you have to provide image paths in your script along with metadata for image, so the names in your code snippet are metadata.So upto what i get to understand from your question, you want files in a local folder to be uploaded and want to provide custom names before uploading , so this is how you will do that.

import os
import boto3

s3 = boto3.resource('s3')

directory_in_str="E:\\streethack\\hold"

directory = os.fsencode(directory_in_str)

for file in os.listdir(directory):
    filename = os.fsdecode(file)
    if filename.endswith(".jpeg") or filename.endswith(".jpg") or filename.endswith(".png"):

        strg=directory_in_str+'\\'+filename
        print(strg)
        print("Enter name for your image : ")
        inp_val = input()

        strg2=inp_val+'.jpeg'
        file = open(strg,'rb')
        object = s3.Object('mausamrest','test/'+ strg2)     #mausamrest is bucket
        object.put(Body=file,ContentType='image/jpeg',ACL='public-read')



    else:
        continue

programmatically , you have to provide path of folder which is hard-coded in this example in directory_in_str variable. then , this code will iterate over each file searching for image , then it will ask for input for custom name and then it will upload your file.

Moreover, you want to show these images on your website , so public_read for images have been turned on using ACL , so you can directly use s3 links to embedd images in your webpages like this one.

https://s3.amazonaws.com/mausamrest/test/jkl.jpeg

This above file is the one i used to test this code snippet. your images will be availbale like this. Make sure you change bucket name. :)

Upvotes: 2

OpenBSDNinja
OpenBSDNinja

Reputation: 1069

I know your question is specific to boto3 so you might not like my answer, but it will achieve the same outcome as what you would like to achieve and the aws-cli also makes use of boto3.

See here: http://bigdatums.net/2016/09/17/copy-local-files-to-s3-aws-cli/

This example is from the site and could easily be used in a script:

#!/bin/bash
#copy all files in my-data-dir into the "data" directory located in my-s3-bucket 
aws s3 cp my-data-dir/ s3://my-s3-bucket/data/ --recursive

Upvotes: 3

John Rotenstein
John Rotenstein

Reputation: 269370

Using the Resource method:

# Iterate through list to upload objects to S3
bucket = s3.Bucket('rekognition-pictures')

for image in images:
    bucket.upload_file(Filename='/upload/' + image[0],
                       Key='index/' + image[0],
                       ExtraArgs={'FullName': image[1]}
                      )

Using the client method:

import boto3

client = boto3.client('s3')

...

# Iterate through list to upload objects to S3
for image in images:
    client.upload_file(Filename='/upload/' + image[0],
                       Bucket='rekognition-pictures',
                       Key='index/' + image[0],
                       ExtraArgs={'FullName': image[1]}
                      )

Upvotes: 1

Related Questions