The Python Oracle

How to run Google gsutil using Python

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Sunrise at the Stream

--

Chapters
00:00 Question
01:13 Accepted answer (Score 11)
02:05 Answer 2 (Score 0)
02:25 Thank you

--

Full question
https://stackoverflow.com/questions/4986...

Question links:
[image]: https://i.stack.imgur.com/rheCS.png

Accepted answer links:
[Overview]: https://googlecloudplatform.github.io/go.../
[Documentation]: https://googlecloudplatform.github.io/go.../
[here]: https://github.com/GoogleCloudPlatform/g...

Answer 2 links:
[shutil.which]: https://docs.python.org/3/library/shutil...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #googlecloudplatform #gcloud #gsutil

#avk47



ACCEPTED ANSWER

Score 13


Note that the proper and official way to interact with Google Cloud Storage is to make use of the Google Cloud Client Library for Python and not running the gsutil command through subprocess.Popen. If you are not setting up merely some tests I would suggest you to follow from the beginning this way if there is not any technological constrain that makes this way impracticable.

You can check at the following links the relative Overview and Documentation. A small example taken from the Documentation can be the following:

from google.cloud import storage

client = storage.Client()
bucket = client.get_bucket('<your-bucket-name>')
blob = bucket.blob('my-test-file.txt')
blob.upload_from_string('this is test content!')

You can find a further example here using google-cloud-python with the Datastore and Cloud Storage to manage expenses.




ANSWER 2

Score 1


Use shutil.which to get the full path to gsutil in a cross-platform manner:

import shutil

# If gsutil is in PATH:
path = shutil.which('gsutil')

# Or if gsutil isn't in PATH but you know where it is:
path = shutil.which('gsutil', path="C:/Program Files (x86)/Google/Cloud SDK/google-cloud-sdk/bin")

# Then you can use that path to run it.
import subprocess
cmd = [path, "version"]
p = subprocess.Popen(cmd)