Bill Noble
Bill Noble

Reputation: 6734

Python open says file doesn't exist when it does

I am trying to work out why my Python open call says a file doesn't exist when it does. If I enter the exact same file url in a browser the photo appears.

The error message I get is:

No such file or directory: 'https://yhistory.s3.amazonaws.com/media/userphotos/1_1471378042183_cdv_photo_033.jpg'

The Python code is:

full_path_filename = 'https://' + settings.AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/' + file_name
fd_img = open(full_path_filename, 'r')

I was wondering if this problem is something to do with the file being in an AWS S3 bucket but I am able to connect to the bucket and list its contents.

Upvotes: -1

Views: 1220

Answers (1)

TheClonerx
TheClonerx

Reputation: 343

If you are trying to open a file over internet you should do something like this (assuming that you are using python 3):

import urllib.request
full_path_filename = 'https://' + settings.AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/' + file_name

file = urllib.request.urlopen(full_path_filename)

This will download the actual file. You can use file as an another file like object.

Upvotes: 3

Related Questions