rix rix - 1 year ago 74
Python Question

UnicodeDecodeError issue when downloading image and uploading

When trying to upload a downloaded file to s3 I'm getting this error:

# *** UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

My understanding is my file is in bytes, I'm not sure what the final open is doing. How can I get this working please?

def download(url, file_name):
with open(file_name, "wb") as file:
response = requests.get(url)

def upload(cropped_img):
s3_connection = boto.connect_s3()
bucket = s3_connection.get_bucket(settings.AWS_S3_BUCKET_NAME)
key = boto.s3.key.Key(bucket, 'th/' + cropped_img)
with open(cropped_img) as f:

Answer Source

You must open the file in binary mode:

with open(cropped_img, 'rb') as f:

Alternatively you can use the boto.s3.key.Key.set_contents_from_filename() method:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download