Home > Mobile >  Using a service account and JSON key which is sent to you to upload data into google cloud storage
Using a service account and JSON key which is sent to you to upload data into google cloud storage

Time:11-22

I wrote a python script that uploads files from a local folder into Google cloud storage.

I also created a service account with sufficient permission and tested it on my computer using that service account JSON key and it worked.

Now I send the code and JSON key to someone else to run but the authentication fails on her side.

Are we missing any authentication through GCP UI?

def config_gcloud():
    subprocess.run(
        [
            shutil.which("gcloud"),
            "auth",
            "activate-service-account",
            "--key-file",
            CREDENTIALS_LOCATION,
        ]
    )
    storage_client = storage.Client.from_service_account_json(CREDENTIALS_LOCATION)
    return storage_client


def file_upload(bucket, source, destination):
    storage_client = config_gcloud()
    ...

The error happens in the config_cloud and it says it is expecting str, path, ... but gets NonType. As I said, the code is fine and works on my computer. How anotehr person can use it using JSON key which I sent her?She stored Json locally and path to Json is in the code.

CodePudding user response:

CREDENTIALS_LOCATION is None instead of the correct path, hence it complaining about it being NoneType instead of str|Path.

Also you don't need that gcloud call, that would only matter for gcloud/gsutil commands, not python client stuff.

And please post the actual stacktrace of the error next time, not just a misspelled interpretation of it.

  • Related