0

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

AnnaSh
  • 11
  • 1
  • 4
  • Welcome to stackoverflow. What's the content of your file (don't copy-paste secrets if there are some in your file)? – ndclt Jan 12 '22 at 13:41
  • @ndclt I generated csv on one website. I have 100 rows of following: ID FisrtName LastName OrderNumber/n ID FisrtName LastName OrderNumber/n ID FisrtName LastName OrderNumber/n – AnnaSh Jan 12 '22 at 14:15
  • That's the matter, you try to read a csv with a json reader. A json file should contain some brackets, keys, values. You should see the [csv reader included in Python library](https://docs.python.org/3/library/csv.html). – ndclt Jan 12 '22 at 14:44
  • @ndclt I meant I upload csv file. But I read config file that is json `{ "login_credentials": "C:/Users/AS/secret.json", "bucket_name": "bucket_name.appspot.com", "local_path": "C:/Users/AS/Documents/Test/" }` – AnnaSh Jan 12 '22 at 15:38
  • sorry. I misunderstood. – ndclt Jan 12 '22 at 16:17

2 Answers2

0

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)

ndclt
  • 2,590
  • 2
  • 12
  • 26
0

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.

AnnaSh
  • 11
  • 1
  • 4