0

I was doing this and worked perfectly fine:

def b64_image(filename):
    with open("static/img/"+filename, 'rb') as f:
        b64 = base64.b64encode(f.read())
        return b64.decode('utf-8')

But now I uploaded the images to an S3 Bucket so I edited the code:

def b64_image(filename):
    with open("https://mybucket.s3-sa-east-1.amazonaws.com/" + filename, 'rb') as f:
        b64 = base64.b64encode(f.read())
        return b64.decode('utf-8')

And I'm getting the fllowing error:

FileNotFoundError: [Errno 2] No such file or directory:

How can I make this code work? How can I make the code to look for the actual URL image? Thanks in advance.

1
  • 1
    You need to use boto3 to access files on S3. Have you looked at it?
    – Roy2012
    Commented May 26, 2020 at 19:55

1 Answer 1

0

In order to use open on files in S3 you will need to use a 3rd party library. In particular, this maintained and active library, smart-open does just that.

1
  • I haven`t used that, but it's definitely an option, thank you!
    – user13527999
    Commented May 26, 2020 at 20:13