LoginSignup
4
0

More than 3 years have passed since last update.

[備忘録] Google Cloud Dataflow APIでLocationを指定する

Last updated at Posted at 2019-08-19

なにこれ

自作Dataflow TemplateをAPIでlaunchする際、Location(Region)を指定していたら地味なところでハマったので備忘としてのメモ。同じ境遇の方がいればその助けになれば幸いです。

問題

公式ドキュメントに沿って自作Dataflow Templateの実行をしようとした際、Locationの指定があると下記のようにエラーとなった。

The workflow could not be created, since it was sent to an invalid regional endpoint (asia-northeast1). Please resubmit to a valid Cloud Dataflow regional endpoint.

※社内のめんどくさいポリシー上、asia-northeast1を利用する必要があります。

対処法

これも公式ドキュメントには書いてありました。公式最強。
https://cloud.google.com/dataflow/docs/reference/rest/

projects.templates.launchではなく、projects.locations.templates.launch を利用すれば良かったみたいです。

なんでLocationの有無でAPIが分かれてるのか。。。

実装

公式にある実装例に、.locations()locationパラメータを追加するだけでOK。

example.py
from oauth2client.client import GoogleCredentials
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build

PROJECTID = "{{YOURPROJECTID}}"
LOCATION = "asia-northeast1" ## 顧客が本当に指定したいRegion
GCSPATH = "gs://{{YOURBUCKETPATH}}/{{YOURTEMPLATE}}" ## template

credentials = GoogleCredentials.get_application_default()
service = build("dataflow", "v1b3", credentials=credentials)

## RequestBody
body = {
    "environment": {
      ...
    },
    "parameters": {
      ...
    },
    "jobName": "{{YOURJOBNAME}}",
}
## REST:: v1b3.projects.locations.templates
req = service.projects().locations().templates().launch(
          projectId=PROJECTID, location=LOCATION,
          gcsPath=GCSPATH, body=body
      )
res = req.execute()
...
4
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0