0

I'm trying to copy a file from one gcs bucket to another bucket using python . I'm not able to copy the file, it throws the below error.

Error:

An error was encountered:
<Blob:[[BUCKET]], [[PATH]], None> could not be converted to unicode
Traceback (most recent call last):
  File "/hadoop/yarn/nm-local-dir/usercache/root/appcache/application_1679321656981_0001/container_e01_1679321656981_0001_01_000001/mllibs/lib/python3.7/site-packages/google/cloud/storage/bucket.py", line 1901, in copy_blob
    new_blob = Blob(bucket=destination_bucket, name=new_name)
  File "/hadoop/yarn/nm-local-dir/usercache/root/appcache/application_1679321656981_0001/container_e01_1679321656981_0001_01_000001/mllibs/lib/python3.7/site-packages/google/cloud/storage/blob.py", line 216, in __init__
    name = _bytes_to_unicode(name)
  File "/hadoop/yarn/nm-local-dir/usercache/root/appcache/application_1679321656981_0001/container_e01_1679321656981_0001_01_000001/mllibs/lib/python3.7/site-packages/google/cloud/_helpers/__init__.py", line 352, in _bytes_to_unicode
    raise ValueError("%r could not be converted to unicode" % (value,))
ValueError: <Blob: [[BUCKET]], [[PATH]], None> could not be converted to unicode

The code:

from google.cloud import storage
from google.oauth2 import service_account
from google.cloud import storage

credentials = service_account.Credentials.from_service_account_file("FILE.json")
client = storage.Client(project='PROJECT', credentials=credentials)

print(client)

source_bucket = client.bucket(str(source_bucket))
source_blob = source_bucket.blob(str(source_blob))
destination_bucket = client.bucket(str(destination_bucket))
destination_blob_name=source_bucket.blob(str(destination_blob_name))

blob_copy = source_bucket.copy_blob(source_blob, destination_bucket, destination_blob_name)

My google package versions for python :

google-api-core          2.11.0
google-auth              2.16.0
google-cloud-bigquery    3.4.1
google-cloud-core        2.3.2
google-cloud-storage     2.7.0
google-crc32c            1.5.0
google-resumable-media   2.4.0
googleapis-common-protos 1.58.0

I have no idea what is wrong. Why the ValueError is coming. I followed the documentation from google.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
tempUser
  • 49
  • 6
  • 2
    Reading the [docs](https://cloud.google.com/storage/docs/samples/storage-copy-file#storage_copy_file-python), it looks like `destination_blob_name` is supposed to be a string. Can you try avoiding the conversion to blob and just passing it as a string? – Nick ODell Mar 20 '23 at 17:00
  • It worked. The official documentation did not give any type for dest_blob_name . So I copied from the source and used it. https://cloud.google.com/storage/docs/copying-renaming-moving-objects#client-libraries. – tempUser Mar 20 '23 at 17:07

1 Answers1

2

The argument destination_blob_name is supposed to be a string.

So you need to change

destination_blob_name=source_bucket.blob(str(destination_blob_name))

to

destination_blob_name = str(destination_blob_name)
Nick ODell
  • 15,465
  • 3
  • 32
  • 66