0

I'm trying to upload a file to google cloud using the below Python code and it throws the list index out of range error. Can someone help me resolve this issue please ?

import sys
from sys import argv


from google.cloud import storage


def upload_blob(bucket_name, source_file_name, destination_blob_name):
    """Uploads a file to the bucket."""
    # The ID of your GCS bucket
    bucket_name = "test1"
    # The path to your file to upload
    source_file_name = "C:/Users/test/p3.csv"
    # The ID of your GCS object
    destination_blob_name = "Test_Upload"
    kms_key_name ="C:/Users/test/df08d120148c00.json"

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(destination_blob_name,kms_key_name=kms_key_name)

    blob.upload_from_filename(source_file_name)

    print(
        f"File {source_file_name} uploaded to {destination_blob_name}."
    )



if __name__ == "__main__":
    
    upload_blob(
        bucket_name=sys.argv[1],
        source_file_name=sys.argv[2],
        destination_blob_name=sys.argv[3],
    )
   
Lakshmi_N
  • 1
  • 1
  • 4
  • Post the complete stacktrace/Traceback – darth baba Jun 27 '22 at 10:14
  • Does printing `sys.argv` show all three arguments? – InsertCheesyLine Jun 27 '22 at 10:15
  • @darthbaba `Traceback (most recent call last): File "c:\Users\test\vscodeadmanager\.venv\UploadFile_GCS.py", line 36, in bucket_name=sys.argv[1], IndexError: list index out of range` – Lakshmi_N Jun 27 '22 at 10:18
  • @Lakshmi_N looks like you're not passing the required number of arguments. eg: `python3 filename.py arg1 arg2 arg3` – InsertCheesyLine Jun 27 '22 at 10:25
  • @InsertCheesyLine No , its not printing those 3 arguments. `Arg list : ['c:/Users/test/vscodeadmanager/.venv/UploadFile_GCS.py']` . This is what it prints when I tried ` print ("Arg list : ",str(sys.argv))` – Lakshmi_N Jun 27 '22 at 10:26
  • Your script expects you to pass 3 arguments [sys.argv](https://www.geeksforgeeks.org/how-to-use-sys-argv-in-python/) – InsertCheesyLine Jun 27 '22 at 10:28
  • @InsertCheesyLine Thank you !! It worked . I have been running the code in VSCode . But then I tried executing the script ` python UploadFile_GCS.py test1 C:/Users/test/p3.csv Test_Upload` – Lakshmi_N Jun 27 '22 at 10:41

0 Answers0