0

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

1
  • Besides what @jellycsv said, unless I am missing something or the code snippet is incomplete, you are also missing code to actually download the image and store it in temp.jpg Commented Feb 1, 2022 at 15:58

1 Answer 1

2

s3.upload_file expects a filename in the first param. You should use upload_fileobj instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.