Reputation: 2417
I am trying to get an image from a url and upload that image to my AWS S3 bucket. I am currectly getting a ValueError('Filename must be a string')
error.
code:
ACCESS_KEY = ''
SECRET_KEY = ''
def upload_to_aws(local_file, bucket, s3_file):
s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
try:
s3.upload_file(local_file, bucket, s3_file)
print("Upload Successful")
return True
except FileNotFoundError:
print("The file was not found")
return False
except NoCredentialsError:
print("Credentials not available")
return False
url = "https://product-images.tcgplayer.com/{}.jpg".format(sealed_product['identifiers'].get('tcgplayerProductId'))
with urllib.request.urlopen(url) as url:
with open('temp.jpg', 'wb') as f:
uploaded = upload_to_aws(f, 'cardcompanion-s3-bucket', "{}.jpg".format(sealed_product['identifiers'].get('tcgplayerProductId')))
Traceback:
Traceback (most recent call last):
File "C:\Users\rossw\Documents\Projects\card_companion_v2\card_companion_310_venv\lib\site-packages\background_task\tasks.py", line 43, in bg_runner
func(*args, **kwargs)
File "C:\Users\rossw\Documents\Projects\card_companion_v2\administration\views.py", line 97, in set_update_bg_task
set_update_magic_sets_sealed_products(set_json, options) if options.get('sealed_products') == 'true' else None
File "C:\Users\rossw\Documents\Projects\card_companion_v2\administration\views.py", line 189, in set_update_magic_sets_sealed_products
uploaded = upload_to_aws(f, 'cardcompanion-s3-bucket', "{}.jpg".format(sealed_product['identifiers'].get('tcgplayerProductId')))
File "C:\Users\rossw\Documents\Projects\card_companion_v2\administration\views.py", line 174, in upload_to_aws
s3.upload_file(local_file, bucket, s3_file)
File "C:\Users\rossw\Documents\Projects\card_companion_v2\card_companion_310_venv\lib\site-packages\boto3\s3\inject.py", line 130, in upload_file
return transfer.upload_file(
File "C:\Users\rossw\Documents\Projects\card_companion_v2\card_companion_310_venv\lib\site-packages\boto3\s3\transfer.py", line 281, in upload_file
raise ValueError('Filename must be a string')
ValueError: Filename must be a string
Upvotes: 0
Views: 859
Reputation: 12359
s3.upload_file
expects a filename in the first param. You should use upload_fileobj
instead.
Upvotes: 2