【发布时间】:2022-02-01 23:54:19
【问题描述】:
我正在尝试从 url 获取图像并将该图像上传到我的 AWS S3 存储桶。我现在收到ValueError('Filename must be a string') 错误。
代码:
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 (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
【问题讨论】:
-
除了@jellycsv 所说的,除非我遗漏了什么或者代码 sn-p 不完整,否则您也缺少实际下载图像并将其存储在 temp.jpg 中的代码
标签: python amazon-web-services amazon-s3 boto3