gcp client libraryチートシート
google-api-python-client
というpackage- pythonでGCPのAPIを叩く際に必要
- 様々なAPIがgoogleからも開発されているが、これが一番根幹のAPIになる
- ドキュメントが網羅的である反面、複雑度が高い
インストール
$ python3 -m pip install google-api-python-client
$ python3 -m pip install google-auth
ドキュメント
- https://googleapis.github.io/google-api-python-client/docs/
gcloudコマンドとの対応
compute instance
$ gcloud compute instances list
from oauth2client.client import GoogleCredentials
from googleapiclient import discovery
from googleapiclient import errors
project = "dena-ai-training-16-gcp"
project_id = 'projects/{}'.format(project)
zone = "us-central1-a"
compute = discovery.build('compute', 'v1')
ret = compute.instances().list(project=project, zone=zone).execute()
print(ret)
ai platform
$ gcloud ai-platform jobs submit training job --master-image-uri=... --region=... --scale-tier=... ...
from oauth2client.client import GoogleCredentials
from googleapiclient import discovery
from googleapiclient import errors
project = "dena-ai-training-16-gcp"
project_id = 'projects/{}'.format(project)
zone = "us-central1-a"
training_inputs = {
"masterConfig": {
'imageUri': "gcr.io/dena-ai-training-16-gcp/hello-world",
},
"region": "us-central1",
'scaleTier': 'BASIC',
'packageUris': [],
'args': [],
'pythonVersion': '3.7',
'scheduling': {
'maxRunningTime': '3600s'
},
}
job_spec = {'jobId': 'my_job_name', 'trainingInput': training_inputs}
ml = discovery.build('ml','v1')
resource = ml.projects().jobs().create(parent='projects/dena-ai-training-16-gcp', body=job_spec)
print(resource.execute())
- このように厳密に対応しないことも多い
- 参考