5

The Google documentation is a little generic on this topic and I find it hard to get around the different APIs and terms they're using, so I'm wondering if someone could point me to the right direction.

I'm looking for a way to call the gcloud command directly from Python. I've installed gcloud in my Python environment and as an example to follow, I'd like to know how to do the following from Python:

gcloud compute copy-files [Source directory or file name] [destination directory of file name]
bossylobster
  • 9,993
  • 1
  • 42
  • 61
orange
  • 7,755
  • 14
  • 75
  • 139

4 Answers4

4

You should check out gcloud: https://pypi.python.org/pypi/gcloud

bossylobster
  • 9,993
  • 1
  • 42
  • 61
1

There's nothing magic about uploading files to a Computer Engine VM. I ended up using paramiko to upload files.

orange
  • 7,755
  • 14
  • 75
  • 139
1

You can of course call gcloud from python directly and not care about the implementation details, or you can try to see what gcloud does:

Try running gcloud compute copy-files with the --dry-run flag. That will expose the scp command it uses underneath and with what arguments. Knowing what scp params you need, you can recreate them programmatically using paramiko_scp in python. More information on this here: How to scp in python?

Community
  • 1
  • 1
Valentin
  • 917
  • 14
  • 19
  • This is what I ended up doing, but I answered that already yesterday. How about other commands that are not scp related? How can these be called from gloud-python? – orange Jun 24 '15 at 23:05
  • Don't run the `gcloud` command as a subprocess from Python. Instead, use [`google-api-client-library`](https://github.com/google/google-api-python-client) and see the relevant docs for how to use it, e.g., with [Google Compute Engine](https://cloud.google.com/compute/docs/tutorials/python-guide). If you have a specific question about a particular command or API, please clarify in a separate question. – Misha Brukman Jun 26 '15 at 00:15
  • 1
    I found the google api client library in the meantime. I was wrongly assuming that `gcloud` is also a Python library which one could access from Python, but many commands just seem to map to other commands (like `scp` for instance). – orange Jun 26 '15 at 01:03
  • @orange once you've installed `gcloud` (aka `google-cloud-sdk`), since it's Python, you can see the source so you can find out what the implementation is for any given command. – Misha Brukman Jun 26 '15 at 14:07
0

You can use the subprocess.run function in python to execute commands from your terminal/shell/bash. That is what I have done to execute gcloud commands from python, rather than using the Python SDK.