登录到Google Console并打开云外壳时,我有一个(Bash)外壳,然后使用“三个点”菜单将文件上传到该实例:
如何使用本地命令行(例如,从笔记本电脑上运行的操作系统)执行此操作? 我想我需要正在运行的实例的名称,如何找到它?
答案 0 :(得分:0)
要获取当前Cloud Shell实例的名称,您可以调用Cloud Shell REST API。该端点将返回描述实例的JSON:
https://cloud.google.com/shell/docs/reference/rest/v1alpha1/users.environments/get
返回的JSON看起来像这样:
{
"name": "users/john.smith@example.com/environments/default",
"id": "default",
"dockerImage": "gcr.io/cloudshell-images/cloudshell:latest",
"state": "RUNNING",
"sshUsername": "john_smith,
"sshHost": "devshell-vm-5ee6f5d4-3c46-4fee-8865-dc0978b5af98.cloudshell.dev",
"sshPort": 6000,
"publicKeys": [
{
"name": "users/me/environments/default/publicKeys/8b4afb8d-5fd9-1234-85d9-9527393ea7ca",
"format": "SSH_RSA",
"key": "<removed>"
},
{
"name": "users/me/environments/default/publicKeys/0a580367-fd88-56ae-dc4e-5295ee29784e",
"format": "SSH_RSA",
"key": "<removed>"
}
]
}
我已经发布了用Go语言编写的完整程序,该程序可与Google Cloud Shell交互。这包括执行远程命令,上传文件,下载文件以及通过SSH连接启动Putty。
以下示例显示了如何调用Cloud Shell REST API以获取实例信息:
package main
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/kirinlabs/HttpRequest"
)
//******************************************************************************************
// Cloud Shell State
//
// https://cloud.google.com/shell/docs/reference/rest/Shared.Types/State
//
// STATE_UNSPECIFIED The environment's states is unknown.
// DISABLED The environment is not running and can't be connected to.
// Starting the environment will transition it to the STARTING state.
// STARTING The environment is being started but is not yet ready to accept
// connections.
// RUNNING The environment is running and ready to accept connections. It
// will automatically transition back to DISABLED after a period of
// inactivity or if another environment is started.
//******************************************************************************************
//******************************************************************************************
// https://cloud.google.com/shell/docs/reference/rest/Shared.Types/Environment
//******************************************************************************************
type CloudShellEnv struct {
Name string `json:"name"`
Id string `json:"id"`
DockerImage string `json:"dockerImage"`
State string `json:"state"`
SshUsername string `json:"sshUsername"`
SshHost string `json:"sshHost"`
SshPort int32 `json:"sshPort"`
Error struct {
Code int32 `json:"code"`
Message string `json:"message"`
Status string `json:"status"`
} `json:"error"`
}
//******************************************************************************************
// Method: users.environments.get
// https://cloud.google.com/shell/docs/reference/rest/v1alpha1/users.environments/get
//******************************************************************************************
func cloud_shell_get_environment(accessToken string, flag_info bool) (CloudShellEnv, error) {
//************************************************************
//
//************************************************************
var params CloudShellEnv
endpoint := "https://cloudshell.googleapis.com/v1alpha1/users/me/environments/default"
endpoint += "?alt=json"
req := HttpRequest.NewRequest()
req.SetHeaders(map[string]string{
"Authorization": "Bearer " + accessToken,
"X-Goog-User-Project": config.ProjectId})
//************************************************************
//
//************************************************************
res, err := req.Get(endpoint)
if err != nil {
fmt.Println("Error: ", err)
return params, err
}
body, err := res.Body()
if err != nil {
fmt.Println("Error: ", err)
return params, err
}
if flag_info == true {
fmt.Println("")
fmt.Println("************************************************************")
fmt.Println("Cloud Shell Info:")
fmt.Println(string(body))
fmt.Println("************************************************************")
}
err = json.Unmarshal(body, ¶ms)
if err != nil {
fmt.Println("Error: Cannot unmarshal JSON: ", err)
return params, err
}
if params.Error.Code != 0 {
fmt.Println("")
fmt.Println(params.Error.Message)
}
return params, nil
}