获取有关某个游戏服务器部署的详细信息。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
C#
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库。
using Google.Cloud.Gaming.V1;
public class GetDeploymentSample
{
public GameServerDeployment GetDeployment(
string projectId, string deploymentId)
{
// Create the client.
GameServerDeploymentsServiceClient client = GameServerDeploymentsServiceClient.Create();
GetGameServerDeploymentRequest request = new GetGameServerDeploymentRequest
{
GameServerDeploymentName = GameServerDeploymentName.FromProjectLocationDeployment(projectId, "global", deploymentId)
};
// Make the request.
GameServerDeployment response = client.GetGameServerDeployment(request);
return response;
}
}
Go
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库。
import (
"context"
"fmt"
"io"
gaming "cloud.google.com/go/gaming/apiv1"
gamingpb "google.golang.org/genproto/googleapis/cloud/gaming/v1"
)
// getGameServerDeployment retrieves info on a game server deployment.
func getGameServerDeployment(w io.Writer, projectID, deploymentID string) error {
// projectID := "my-project"
// deploymentID := "mydeployment"
ctx := context.Background()
client, err := gaming.NewGameServerDeploymentsClient(ctx)
if err != nil {
return fmt.Errorf("NewGameServerDeploymentsClient: %v", err)
}
defer client.Close()
req := &gamingpb.GetGameServerDeploymentRequest{
Name: fmt.Sprintf("projects/%s/locations/global/gameServerDeployments/%s", projectID, deploymentID),
}
resp, err := client.GetGameServerDeployment(ctx, req)
if err != nil {
return fmt.Errorf("GetGameServerDeployment: %v", err)
}
fmt.Fprintf(w, "Deployment retrieved: %v", resp.Name)
return nil
}
Java
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库。
import com.google.cloud.gaming.v1.GameServerDeployment;
import com.google.cloud.gaming.v1.GameServerDeploymentsServiceClient;
import java.io.IOException;
public class GetDeployment {
public static void getGameServerDeployment(String projectId, String deploymentId)
throws IOException {
// String projectId = "your-project-id";
// String deploymentId = "your-game-server-deployment-id";
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (GameServerDeploymentsServiceClient client = GameServerDeploymentsServiceClient.create()) {
String deploymentName =
String.format(
"projects/%s/locations/global/gameServerDeployments/%s", projectId, deploymentId);
GameServerDeployment deployment = client.getGameServerDeployment(deploymentName);
System.out.println("Game Server Deployment found: " + deployment.getName());
}
}
}
Node.js
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库。
const {
GameServerDeploymentsServiceClient,
} = require('@google-cloud/game-servers');
const client = new GameServerDeploymentsServiceClient();
async function getGameServerDeployment() {
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'Your Google Cloud Project ID';
// const deploymentId = 'A unique ID for the Game Server Deployment';
const request = {
// The full resource name
name: client.gameServerDeploymentPath(projectId, 'global', deploymentId),
};
const [deployment] = await client.getGameServerDeployment(request);
console.log(`Deployment name: ${deployment.name}`);
console.log(`Deployment description: ${deployment.description}`);
const createTime = deployment.createTime;
const createDate = new Date(createTime.seconds * 1000);
console.log(`Deployment created on: ${createDate.toLocaleDateString()}\n`);
}
getGameServerDeployment();
Python
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库。
def get_deployment(project_id, deployment_id):
"""Gets a game server deployment."""
client = gaming.GameServerDeploymentsServiceClient()
# Location is hard coded as global, as game server deployments can
# only be created in global. This is done for all operations on
# game server deployments, as well as for its child resource types.
request = game_server_deployments.GetGameServerDeploymentRequest(
name=f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}",
)
response = client.get_game_server_deployment(request)
print(f"Get deployment response:\n{response}")
return response
后续步骤
如需搜索并过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。