4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Linux (Ubuntu) 環境でサービスアカウントからgsutilを利用する

Posted at

Dockerfileなどのスクリプトで gsutil を実行したい時があったので。
環境変数 GOOGLE_APPLICATION_CREDENTIALS にサービスアカウントキーファイルをセットすればいいかと思いきや、それだけでは

ServiceException: 401 Anonymous caller
does not havestorage.objects.list access to bucket

のようなエラーが出てしまう。

結論

https://cloud.google.com/storage/docs/gsutil_install?hl=ja#deb を参考にする。

$ export CLOUD_SDK_REPO="cloud-sdk-$(lsb_release -c -s)" && \
    echo "deb http://packages.cloud.google.com/apt $CLOUD_SDK_REPO main" | \
    sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
    curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | \
    sudo apt-key add - && \
    sudo apt-get -y update && \
    sudo apt-get -y install google-cloud-sdk && \
    gcloud auth activate-service-account \
    --key-file=/path/to/service_account/key_file.json

最後の

gcloud auth activate-service-account \
--key-file=/path/to/service_account/key_file.json

がミソ。 ドキュメントの通りに gcloud init とすると、ログイン操作が必要になってしまいスクリプトでは実行できない。

4
5
0

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
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?