Asnim P Ansari
Asnim P Ansari

Reputation: 2477

Unable to download files from Google Cloud Storage: ValueError("%r could not be converted to unicode" % (value,))

I am trying to download a file from my Cloud Storage bucket using Python:

GCP_BUCKET_NAME = "my-learning-bucket"
storage_bucket = storage_client.bucket(GCP_BUCKET_NAME)


def download_object(download_obj_name):
    blob = storage_bucket.blob(download_obj_name)
    blob.download_to_file_name(f"downloads/data.db")

But it gives me error:

Traceback (most recent call last):
  File "/Users/asnimansari/Desktop/tradestation/ares/main.py", line 22, in <module>
    root.run_click_commands()
  File "/Users/asnimansari/.pyenv/versions/3.9.4/lib/python3.9/site-packages/click/core.py", line 1137, in __call__
    return self.main(*args, **kwargs)
  File "/Users/asnimansari/.pyenv/versions/3.9.4/lib/python3.9/site-packages/click/core.py", line 1062, in main
    rv = self.invoke(ctx)
  File "/Users/asnimansari/.pyenv/versions/3.9.4/lib/python3.9/site-packages/click/core.py", line 1668, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/asnimansari/.pyenv/versions/3.9.4/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/asnimansari/.pyenv/versions/3.9.4/lib/python3.9/site-packages/click/core.py", line 763, in invoke
    return __callback(*args, **kwargs)
  File "/Users/asnimansari/Desktop/tradestation/ares/commands/import_data.py", line 9, in import_data
    import_latest_data_from_gc()
  File "/Users/asnimansari/Desktop/tradestation/ares/helpers/import_data_from_gc.py", line 8, in import_latest_data_from_gc
    db_names_in_gcp = get_db_names_in_gcp()
  File "/Users/asnimansari/Desktop/tradestation/ares/helpers/gcp.py", line 29, in get_db_names_in_gcp
    donwload_object(bobu)
  File "/Users/asnimansari/Desktop/tradestation/ares/helpers/gcp.py", line 13, in donwload_object
    blob = storage_bucket.blob(download_obj_name)
  File "/Users/asnimansari/.pyenv/versions/3.9.4/lib/python3.9/site-packages/google/cloud/storage/bucket.py", line 688, in blob
    return Blob(
  File "/Users/asnimansari/.pyenv/versions/3.9.4/lib/python3.9/site-packages/google/cloud/storage/blob.py", line 205, in __init__
    name = _bytes_to_unicode(name)
  File "/Users/asnimansari/.pyenv/versions/3.9.4/lib/python3.9/site-packages/google/cloud/_helpers.py", line 389, in _bytes_to_unicode
    raise ValueError("%r could not be converted to unicode" % (value,))
ValueError: <Blob: tradeenginedata, quotes/quotes_2021-06-10.db, 1623310593606516> could not be converted to unicode

The file is a sqlite database.

Upvotes: 0

Views: 3225

Answers (1)

Donnald Cucharo
Donnald Cucharo

Reputation: 4126

I see that you've already opened a GitHub Issue link and there's already a response from the Cloud Storage Team.

As they mentioned, the error comes from the fact that you're passing an invalid String to storage_bucket.blob(). As additional insight, we can see from the error that a Blob Object is actually being passed in, rather than String.

Therefore, please make sure that download_obj_name is a valid String and not a Blob Object.

Also, you must correct your typo in the download method and notice the extra underscore. The correct method is download_to_filename(), not download_to_file_name().

Upvotes: 1

Related Questions