如何使用cli命令,而不是手动使用gcloud init,将GCP(谷歌云平台)账户下当前正在运行的项目修改为其他项目?

Gcloud项目列表将列出在我的帐户上运行的项目。我想使用cli命令将当前项目更改为列表中的任何其他项目。


当前回答

此外,如果您正在使用多个项目,并且不想每次都设置全局项目,您可以使用选择项目标志。

例如:连接谷歌云平台my_project项目下名为my_vm的虚拟机:

Gcloud——project my_project compute SSH my_vm

通过这种方式,您可以使用多个项目,并通过添加项目标志轻松地在它们之间进行更改。你可以在这里找到更多关于其他GCP标志的信息。

其他回答

可以使用gcloud命令更改项目:

gcloud config set project <your_project_name>

做以下几点:

1 - gcloud认证列表 2 - gcloud项目列表 3 - gcloud配置项目*投影* (将项目id替换为实际项目id)

gcloud config set project <PROJECT_ID>

我确实更喜欢别名,对于可能需要多个命令的东西,根据您的项目需求,我更喜欢函数……

例子

function switchGCPProject() {
        gcloud config set project [Project Name]
        // if you are using GKE use the following
        gcloud config set container/cluster [Cluster Name]
        // if you are using GCE use the following
        gcloud config set compute/zone [Zone]
        gcloud config set compute/region [region]
        // if you are using GKE use the following
        gcloud container clusters get-credentials [cluster name] --zone [Zone] --project [project name]
        export GOOGLE_APPLICATION_CREDENTIALS=path-to-credentials.json
}

只需使用gcloud项目列表来获取您拥有的项目。获取要使用的对象的PROJECT_ID 之后使用gcloud set project——project=PROJECT_ID来设置项目。