Blob could not be converted to unicode in my Cloud Function

Hello

I have this Pyhton Cloud Function :

 

from google.cloud import storage

def fileUpload(event, context):
    
    file_data = event
    bucket_name = file_data['bucket']
    file_name = file_data['name']
    
    print(type(file_name))
    print(f"Bucket name: {bucket_name}")
    print(f"File name: {file_name}")
    
    client = storage.Client()
    source_bucket = client.bucket(bucket_name)
    destination_bucket = client.bucket(bucket_name)
    source_blob = source_bucket.blob(file_name)
    destination_blob = source_bucket.blob("copy")
    
    destination_generation_match_precondition = 0
    
    blob_copy = source_bucket.copy_blob(
        source_blob, destination_bucket, destination_blob, if_generation_match=destination_generation_match_precondition,
    )

 

And I got this error :

 

gcloud functions logs read clound-function-ingestion-study1 --region europe-west1
LEVEL  NAME                              EXECUTION_ID  TIME_UTC                 LOG
       clound-function-ingestion-study1                2024-03-06 14:31:22.840  ValueError: <Blob: my-bucket-study1, copy, None> could not be converted to unicode
       clound-function-ingestion-study1                2024-03-06 14:31:21.802      raise ValueError("%r could not be converted to unicode" % (value,))
       clound-function-ingestion-study1                2024-03-06 14:31:21.802    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/google/cloud/_helpers/__init__.py", line 352, in _bytes_to_unicode
       clound-function-ingestion-study1                2024-03-06 14:31:21.802             ^^^^^^^^^^^^^^^^^^^^^^^
       clound-function-ingestion-study1                2024-03-06 14:31:21.802      name = _bytes_to_unicode(name)
       clound-function-ingestion-study1                2024-03-06 14:31:21.802    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/google/cloud/storage/blob.py", line 219, in __init__
       clound-function-ingestion-study1                2024-03-06 14:31:21.802                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       clound-function-ingestion-study1                2024-03-06 14:31:21.802      new_blob = Blob(bucket=destination_bucket, name=new_name)
       clound-function-ingestion-study1                2024-03-06 14:31:21.802    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/google/cloud/storage/bucket.py", line 1909, in copy_blob
       clound-function-ingestion-study1                2024-03-06 14:31:21.802                  ^^^^^^^^^^^^^^^^^^^^^^^^
       clound-function-ingestion-study1                2024-03-06 14:31:21.802      blob_copy = source_bucket.copy_blob(
       clound-function-ingestion-study1                2024-03-06 14:31:21.802    File "/workspace/main.py", line 22, in fileUpload
       clound-function-ingestion-study1                2024-03-06 14:31:21.802      function(data, context)
       clound-function-ingestion-study1                2024-03-06 14:31:21.802    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/functions_framework/__init__.py", line 150, in view_func
       clound-function-ingestion-study1                2024-03-06 14:31:21.802             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       clound-function-ingestion-study1                2024-03-06 14:31:21.802      return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
       clound-function-ingestion-study1                2024-03-06 14:31:21.802    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 1799, in dispatch_request
       clound-function-ingestion-study1                2024-03-06 14:31:21.802           ^^^^^^^^^^^^^^^^^^^^^^^
       clound-function-ingestion-study1                2024-03-06 14:31:21.802      rv = self.dispatch_request()
       clound-function-ingestion-study1                2024-03-06 14:31:21.802    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 1823, in full_dispatch_request

 

 The prints displayed this :

<class 'str'>

Bucket name: my-bucket-study1

File name: test.jpeg

 

Can you help me please ?

1 1 271
1 REPLY 1

Hello @aalshikhley,

Take a look at this documentation.

Try changing your destination_blob to a string as stated in the documentation. Also, take a look at this Stackoverflow Post as you might have the same problem.

If the above option doesn't work, you can contact Google Cloud Support to further look into your case. Hope it helps, thanks!