Home > OS >  Python script for GCS - calling in terminal with argument - json.loads() stoped working
Python script for GCS - calling in terminal with argument - json.loads() stoped working

Time:01-13

I'm Uploading files to GCS (Google Cloud Storage). My script was working. But now I try to run it in Terminal by python uploadtogcs.py 'C:/Users/AS/Documents/GCSUploadParameters.json'

I receive error:

    Traceback (most recent call last):
  File "C:\Users\AS\uploadtogcs.py", line 43, in <module>
    upload_files(sys.argv[0])
  File "C:\Users\AS\uploadtogcs.py", line 11, in upload_files
    contents = json.loads(data)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I tried writing json.load(), but it doesn't help. I also tried removing file.read(). Then I have another error. I want to run from terminal and give argument there.

Here is my code:

from google.cloud import storage
import os
import glob
import json
import sys

def upload_files(config_file):
    # Reading 3 Parameters for upload from JSON file
    with open(config_file, "r") as file:
        data = file.read()
        contents = json.loads(data)

    # Setting up login credentials
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = contents['login_credentials']
    # The ID of GCS bucket
    bucket_name = contents['bucket_name']
    # Setting path to files
    LOCAL_PATH = contents['local_path']

    for source_file_name in glob.glob(LOCAL_PATH   '/**'):

    # For multiple files upload
    # Setting destination folder according to file name 
        if os.path.isfile(source_file_name):
            partitioned_file_name = os.path.split(source_file_name)[-1].partition("-")
            file_type_name = partitioned_file_name[0]

            # Setting folder where files will be uploaded
            destination_blob_name = file_type_name   "/"   os.path.split(source_file_name)[-1]

            # Setting up required variables for GCS 
            storage_client = storage.Client()
            bucket = storage_client.bucket(bucket_name)
            blob = bucket.blob(destination_blob_name)

            # Running upload and printing confirmation message
            blob.upload_from_filename(source_file_name)
            print("File from {} uploaded to {} in bucket {}.".format(
                source_file_name, destination_blob_name, bucket_name
            ))


upload_files(sys.argv[0])

Can you please help me?

Kind regards, Anna

CodePudding user response:

Instead of reading the file by yourself, you should give it to the json.load function like this:

import json 
def upload_files(config_file):
    # Reading 3 Parameters for upload from JSON file
    with open(config_file, "r") as file:
        contents = json.load(file)

CodePudding user response:

My friend helped me.

It had to be: upload_files(sys.argv[1])

positional argument 1, not 0.

All works now. Hope it will be helpful to someone.

  •  Tags:  
  • Related