lakshmi
lakshmi

Reputation: 211

Not able to rename the file while storing in GCS bucket

I want to rename a file while storing a file in GCS bucket.I referred this doc:
How to rename a file while storing it to a GCS bucket

It gives the error.I am not sure if I have modified it correfctly.Entry point is new_func.This is the code below:

def new_func(event,context):
    bucket_name: 'poc_bucket_testing'
    blob_name:  'poc_bucket_testing/republic_txttocsv.txt'          
    new_blob_name: 'target_bucket/republic_txttocsv.csv'    
    rename_blob(bucket_name,blob_name,new_blob_name);

def rename_blob(bucket_name,blob_name,new_blob_name):       
    print("inside");
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(blob_name)
    new_blob = bucket.rename_blob(blob,new_blob_name)        
    return new_blob.public_url

Error is given below.I also tried assigning it inside rename_blob and gives the same error.

"Traceback (most recent call last):
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 449, in run_background_function
    _function_handler.invoke_user_function(event_object)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 268, in invoke_user_function
    return call_user_function(request_or_event)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", line 265, in call_user_function
    event_context.Context(**request_or_event.context))
  File "/user_code/main.py", line 38, in new_func
    rename_blob(bucket_name,blob_name,new_blob_name);
UnboundLocalError: local variable 'bucket_name' referenced before assignment

Upvotes: 0

Views: 260

Answers (1)

John Hanley
John Hanley

Reputation: 81424

You are using colons (as in a dictionary) instead of assignment operators:

def new_func(event,context):
    bucket_name = 'poc_bucket_testing'
    blob_name =  'poc_bucket_testing/republic_txttocsv.txt'          
    new_blob_name = 'target_bucket/republic_txttocsv.csv'    
    rename_blob(bucket_name,blob_name,new_blob_name)

Upvotes: 3

Related Questions