Skip to content
Advertisement

How to access an item from S3 using boto3 and read() its contents

I have a method that fetches a file from a URL and converts it to OpenCV image

def my_method(self, imgurl):
   req = urllib.urlopen(imgurl)
   r = req.read()
   arr = np.asarray(bytearray(r), dtype=np.uint8)
   image = cv2.imdecode(arr,-1) # 'load it as it is'
   return image

I would like to use boto3 to access an object from s3 bucket and convert it to an image just like above method does. However, I’m not sure how to access an item from a bucket using boto3 and then further how to read() contents of that item.

Below is what I’ve tried

>>> import botocore
>>> import boto3
>>> client = boto3.client('s3',aws_access_key_id="myaccsskey",aws_secret_access_key="secretkey")
>>> bucketname = "mybucket"
>>> itemname = "demo.png"

Questions

  1. How can I access a particular item from a bucket using boto3?
  2. Is there a way to read the contents of the accessed item similar to what I’m doing in my_method using req.read()?

Advertisement

Answer

I would do 1 this way:

import boto3
s3 = boto3.resource('s3',
                     use_ssl=False,
                     endpoint_url="http://localhost:4567",
                     aws_access_key_id="",
                     aws_secret_access_key="",
)
obj = s3.Object(bucketname, itemname)

For 2, I have never tried by this SO answer suggest:

body = obj.get()['Body'].read()

using the high-level ressource proposed by boto3.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement