将 DescribeDBClusterSnapshots 与 Amazon SDK 或 CLI 配合使用 - Amazon Aurora
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

DescribeDBClusterSnapshots 与 Amazon SDK 或 CLI 配合使用

以下代码示例演示如何使用 DescribeDBClusterSnapshots

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
Amazon SDK for .NET
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

/// <summary> /// Return a list of DB snapshots for a particular DB cluster. /// </summary> /// <param name="dbClusterIdentifier">DB cluster identifier.</param> /// <returns>List of DB snapshots.</returns> public async Task<List<DBClusterSnapshot>> DescribeDBClusterSnapshotsByIdentifierAsync(string dbClusterIdentifier) { var results = new List<DBClusterSnapshot>(); DescribeDBClusterSnapshotsResponse response; DescribeDBClusterSnapshotsRequest request = new DescribeDBClusterSnapshotsRequest { DBClusterIdentifier = dbClusterIdentifier }; // Get the full list if there are multiple pages. do { response = await _amazonRDS.DescribeDBClusterSnapshotsAsync(request); results.AddRange(response.DBClusterSnapshots); request.Marker = response.Marker; } while (response.Marker is not null); return results; }
C++
适用于 C++ 的 SDK
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); Aws::RDS::Model::DescribeDBClusterSnapshotsRequest request; request.SetDBClusterSnapshotIdentifier(snapshotID); Aws::RDS::Model::DescribeDBClusterSnapshotsOutcome outcome = client.DescribeDBClusterSnapshots(request); if (outcome.IsSuccess()) { snapshot = outcome.GetResult().GetDBClusterSnapshots()[0]; } else { std::cerr << "Error with Aurora::DescribeDBClusterSnapshots. " << outcome.GetError().GetMessage() << std::endl; cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, DB_CLUSTER_IDENTIFIER, DB_INSTANCE_IDENTIFIER, client); return false; }
Go
适用于 Go V2 的 SDK
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

type DbClusters struct { AuroraClient *rds.Client } // GetClusterSnapshot gets a DB cluster snapshot. func (clusters *DbClusters) GetClusterSnapshot(snapshotName string) (*types.DBClusterSnapshot, error) { output, err := clusters.AuroraClient.DescribeDBClusterSnapshots(context.TODO(), &rds.DescribeDBClusterSnapshotsInput{ DBClusterSnapshotIdentifier: aws.String(snapshotName), }) if err != nil { log.Printf("Couldn't get snapshot %v: %v\n", snapshotName, err) return nil, err } else { return &output.DBClusterSnapshots[0], nil } }
Java
SDK for Java 2.x
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

public static void waitForSnapshotReady(RdsClient rdsClient, String dbSnapshotIdentifier, String dbInstanceClusterIdentifier) { try { boolean snapshotReady = false; String snapshotReadyStr; System.out.println("Waiting for the snapshot to become available."); DescribeDbClusterSnapshotsRequest snapshotsRequest = DescribeDbClusterSnapshotsRequest.builder() .dbClusterSnapshotIdentifier(dbSnapshotIdentifier) .dbClusterIdentifier(dbInstanceClusterIdentifier) .build(); while (!snapshotReady) { DescribeDbClusterSnapshotsResponse response = rdsClient.describeDBClusterSnapshots(snapshotsRequest); List<DBClusterSnapshot> snapshotList = response.dbClusterSnapshots(); for (DBClusterSnapshot snapshot : snapshotList) { snapshotReadyStr = snapshot.status(); if (snapshotReadyStr.contains("available")) { snapshotReady = true; } else { System.out.println("."); Thread.sleep(sleepTime * 5000); } } } System.out.println("The Snapshot is available!"); } catch (RdsException | InterruptedException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
Kotlin
适用于 Kotlin 的 SDK
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

suspend fun waitSnapshotReady( dbSnapshotIdentifier: String?, dbInstanceClusterIdentifier: String?, ) { var snapshotReady = false var snapshotReadyStr: String println("Waiting for the snapshot to become available.") val snapshotsRequest = DescribeDbClusterSnapshotsRequest { dbClusterSnapshotIdentifier = dbSnapshotIdentifier dbClusterIdentifier = dbInstanceClusterIdentifier } RdsClient { region = "us-west-2" }.use { rdsClient -> while (!snapshotReady) { val response = rdsClient.describeDbClusterSnapshots(snapshotsRequest) val snapshotList = response.dbClusterSnapshots if (snapshotList != null) { for (snapshot in snapshotList) { snapshotReadyStr = snapshot.status.toString() if (snapshotReadyStr.contains("available")) { snapshotReady = true } else { println(".") delay(slTime * 5000) } } } } } println("The Snapshot is available!") }
Python
SDK for Python(Boto3)
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

class AuroraWrapper: """Encapsulates Aurora DB cluster actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon Relational Database Service (Amazon RDS) client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_cluster_snapshot(self, snapshot_id): """ Gets a DB cluster snapshot. :param snapshot_id: The ID of the snapshot to retrieve. :return: The retrieved snapshot. """ try: response = self.rds_client.describe_db_cluster_snapshots( DBClusterSnapshotIdentifier=snapshot_id ) snapshot = response["DBClusterSnapshots"][0] except ClientError as err: logger.error( "Couldn't get DB cluster snapshot %s. Here's why: %s: %s", snapshot_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return snapshot
  • 有关 API 详细信息,请参阅《适用于 Python 的 Amazon SDK(Boto3)API 参考》中的 DescribeDBClusterSnapshots

有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将此服务与 Amazon SDK 结合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。