Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

upload a simple json file to google storage from a simple python script

I have a .json local file and I want to upload it to google cloud storage using Python.

First note that my python script is not on app-engine, it's a simple script so I don't want to use the appengine API. Secondly, I read this tutorial and I think it is extremely unclear.

Hence, I am looking for an alternative API, maybe wrote by someone outside from Google, to help me upload my json file using my json credentials.

like image 359
sweeeeeet Avatar asked Apr 08 '26 06:04

sweeeeeet


1 Answers

If you want to interact with google cloud storage services outside of the App Engine environment, you may use Gcloud (https://googlecloudplatform.github.io/gcloud-python/stable/) to do so.

You need a service account on your application as well as download the JSON credentials file. With those, once you install the proper packages, you'll be able to make queries as well as write data.

Here is an example of authenticating yourself to use the library:

from gcloud import datastore

# the location of the JSON file on your local machine
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/location/client_secret.json"


# project ID from the Developers Console
projectID = "THE_ID_OF_YOUR_PROJECT"

os.environ["GCLOUD_TESTS_PROJECT_ID"] = projectID
os.environ["GCLOUD_TESTS_DATASET_ID"] = projectID
client = datastore.Client(dataset_id=projectID)

Edit:

This library is for the following as per the docs here(https://github.com/GoogleCloudPlatform/gcloud-python):

This client supports the following Google Cloud Platform services:

Google Cloud Datastore
Google Cloud Storage
Google Cloud Pub/Sub
Google BigQuery
Google Cloud Resource Manager
like image 125
Kris Avatar answered Apr 10 '26 18:04

Kris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!