Reputation: 10632
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)
file.write(response.content)
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:
key.send_file(f)
Upvotes: 0
Views: 611
Reputation: 32484
You must open the file in binary mode:
with open(cropped_img, 'rb') as f:
key.send_file(f)
Alternatively you can use the boto.s3.key.Key.set_contents_from_filename()
method:
key.set_contents_from_filename(cropped_img)
Upvotes: 1