2017-06-29 160 views
1

我有兩個Google Cloud服務帳戶;一個用於我的兩個項目。如何使用gcloud使用多個服務帳戶?

# ACCOUNTS 
[email protected] 
[email protected] 

我可以告訴gcloud佔我需要執行命令前使用:

gcloud set account [ACCOUNT] 

問:有沒有什麼辦法可以配置gcloudgsutil,這樣他們就會使用對於在他們各自的項目中執行的操作,無需我一直手動切換這些帳戶?


我在一個項目中管理實例,並從另一個項目中的桶中上傳/下載文件。在命令之間必須始終執行gcloud set_account [ACCOUNT]變得非常繁瑣。

我需要在這使我想我會掉在坑裏,如果我激活/同時運行這兩個項目長期運行的命令去激活使用這些命令的帳戶。

也許我唯一的選擇是從兩個不同的Docker容器運行google-cloud-sdk?

回答

5

您有幾種選擇在這裏:

  • 雲SDK尊重環境變量指定的屬性。 gcloud config set accountgcloud config set core/account的簡寫,所以相應的屬性是CLOUDSDK_CORE_ACCOUNT

    你可以這樣做:

    $ [email protected] gcloud ... 
    $ [email protected] gcloud ... 
    

    這應該給你你感興趣的結果

  • 如果你需要不止一個屬性改變,雲SDK提供了一個抽象named configuration 。請參閱有關完整信息,文檔,但你可以運行:

    $ gcloud config configurations create my-project1-config 
    $ gcloud config configurations activate my-project1-config 
    $ gcloud auth login # or activate-service-account 
    $ gcloud config set project project1 # and any other configuration you need to do 
    $ 
    $ gcloud config configurations create my-project2-config 
    $ gcloud config configurations activate my-project2-config 
    $ gcloud auth login # or activate-service-account 
    $ gcloud config set project project2 # and any other configuration you need to do 
    $ 
    $ CLOUDSDK_ACTIVE_CONFIG_NAME=my-project1-config gcloud ... 
    $ CLOUDSDK_ACTIVE_CONFIG_NAME=my-project2-config gcloud ... 
    
  • 在最極端的情況下,可以保持獨立的雲SDK配置目錄。默認情況下(在* nix)爲~/.config/gcloud

    $ CLOUDSDK_CONFIG=/tmp/tmpconfig1 gcloud auth login 
    $ CLOUDSDK_CONFIG=/tmp/tmpconfig2 gcloud auth login 
    
+0

要添加到這個答案,gcloud支持--account和--configuration標誌。 --account的可能值可以通過'gcloud auth list'列出,配置的可能值可以通過'gcloud config configurations list'列出。但是這兩個標誌不能與gsutil或bq一起使用。 – cherba

相關問題