RDSContoh Amazon menggunakan AWS SDK for .NET - AWS SDK for .NET

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

RDSContoh Amazon menggunakan AWS SDK for .NET

Contoh kode berikut menunjukkan kepada Anda cara melakukan tindakan dan mengimplementasikan skenario umum dengan menggunakan AWS SDK for .NET With AmazonRDS.

Dasar-dasar adalah contoh kode yang menunjukkan kepada Anda bagaimana melakukan operasi penting dalam suatu layanan.

Tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Sementara tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks dalam skenario terkait.

Skenario adalah contoh kode yang menunjukkan kepada Anda bagaimana menyelesaikan tugas tertentu dengan memanggil beberapa fungsi dalam layanan atau dikombinasikan dengan yang lain layanan AWS.

Setiap contoh menyertakan tautan ke kode sumber lengkap, di mana Anda dapat menemukan instruksi tentang cara mengatur dan menjalankan kode dalam konteks.

Memulai

Contoh kode berikut menunjukkan cara memulai menggunakan AmazonRDS.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

using System; using System.Threading.Tasks; using Amazon.RDS; using Amazon.RDS.Model; namespace RDSActions; public static class HelloRds { static async Task Main(string[] args) { var rdsClient = new AmazonRDSClient(); Console.WriteLine($"Hello Amazon RDS! Following are some of your DB instances:"); Console.WriteLine(); // You can use await and any of the async methods to get a response. // Let's get the first twenty DB instances. var response = await rdsClient.DescribeDBInstancesAsync( new DescribeDBInstancesRequest() { MaxRecords = 20 // Must be between 20 and 100. }); foreach (var instance in response.DBInstances) { Console.WriteLine($"\tDB name: {instance.DBName}"); Console.WriteLine($"\tArn: {instance.DBInstanceArn}"); Console.WriteLine($"\tIdentifier: {instance.DBInstanceIdentifier}"); Console.WriteLine(); } } }

Hal-hal mendasar

Contoh kode berikut ini menunjukkan cara:

  • Membuat grup parameter basis data kustom dan mengatur nilai parameter.

  • Membuat instans basis data yang dikonfigurasikan untuk menggunakan grup parameter. Instans basis data juga berisi basis data.

  • Mengambil cuplikan instans.

  • Menghapus instans dan grup parameter.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Jalankan skenario interaktif di penggugah/prompt perintah.

/// <summary> /// Scenario for RDS DB instance example. /// </summary> public class RDSInstanceScenario { /* Before running this .NET code example, set up your development environment, including your credentials. This .NET example performs the following tasks: 1. Returns a list of the available DB engine families using the DescribeDBEngineVersionsAsync method. 2. Selects an engine family and creates a custom DB parameter group using the CreateDBParameterGroupAsync method. 3. Gets the parameter groups using the DescribeDBParameterGroupsAsync method. 4. Gets parameters in the group using the DescribeDBParameters method. 5. Parses and displays parameters in the group. 6. Modifies both the auto_increment_offset and auto_increment_increment parameters using the ModifyDBParameterGroupAsync method. 7. Gets and displays the updated parameters using the DescribeDBParameters method with a source of "user". 8. Gets a list of allowed engine versions using the DescribeDBEngineVersionsAsync method. 9. Displays and selects from a list of micro instance classes available for the selected engine and version. 10. Creates an RDS DB instance that contains a MySql database and uses the parameter group using the CreateDBInstanceAsync method. 11. Waits for DB instance to be ready using the DescribeDBInstancesAsync method. 12. Prints out the connection endpoint string for the new DB instance. 13. Creates a snapshot of the DB instance using the CreateDBSnapshotAsync method. 14. Waits for DB snapshot to be ready using the DescribeDBSnapshots method. 15. Deletes the DB instance using the DeleteDBInstanceAsync method. 16. Waits for DB instance to be deleted using the DescribeDbInstances method. 17. Deletes the parameter group using the DeleteDBParameterGroupAsync. */ private static readonly string sepBar = new('-', 80); private static RDSWrapper rdsWrapper = null!; private static ILogger logger = null!; private static readonly string engine = "mysql"; static async Task Main(string[] args) { // Set up dependency injection for the Amazon RDS service. using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .ConfigureServices((_, services) => services.AddAWSService<IAmazonRDS>() .AddTransient<RDSWrapper>() ) .Build(); logger = LoggerFactory.Create(builder => { builder.AddConsole(); }).CreateLogger<RDSInstanceScenario>(); rdsWrapper = host.Services.GetRequiredService<RDSWrapper>(); Console.WriteLine(sepBar); Console.WriteLine( "Welcome to the Amazon Relational Database Service (Amazon RDS) DB instance scenario example."); Console.WriteLine(sepBar); try { var parameterGroupFamily = await ChooseParameterGroupFamily(); var parameterGroup = await CreateDbParameterGroup(parameterGroupFamily); var parameters = await DescribeParametersInGroup(parameterGroup.DBParameterGroupName, new List<string> { "auto_increment_offset", "auto_increment_increment" }); await ModifyParameters(parameterGroup.DBParameterGroupName, parameters); await DescribeUserSourceParameters(parameterGroup.DBParameterGroupName); var engineVersionChoice = await ChooseDbEngineVersion(parameterGroupFamily); var instanceChoice = await ChooseDbInstanceClass(engine, engineVersionChoice.EngineVersion); var newInstanceIdentifier = "Example-Instance-" + DateTime.Now.Ticks; var newInstance = await CreateRdsNewInstance(parameterGroup, engine, engineVersionChoice.EngineVersion, instanceChoice.DBInstanceClass, newInstanceIdentifier); if (newInstance != null) { DisplayConnectionString(newInstance); await CreateSnapshot(newInstance); await DeleteRdsInstance(newInstance); } await DeleteParameterGroup(parameterGroup); Console.WriteLine("Scenario complete."); Console.WriteLine(sepBar); } catch (Exception ex) { logger.LogError(ex, "There was a problem executing the scenario."); } } /// <summary> /// Choose the RDS DB parameter group family from a list of available options. /// </summary> /// <returns>The selected parameter group family.</returns> public static async Task<string> ChooseParameterGroupFamily() { Console.WriteLine(sepBar); // 1. Get a list of available engines. var engines = await rdsWrapper.DescribeDBEngineVersions(engine); Console.WriteLine("1. The following is a list of available DB parameter group families:"); int i = 1; var parameterGroupFamilies = engines.GroupBy(e => e.DBParameterGroupFamily).ToList(); foreach (var parameterGroupFamily in parameterGroupFamilies) { // List the available parameter group families. Console.WriteLine( $"\t{i}. Family: {parameterGroupFamily.Key}"); i++; } var choiceNumber = 0; while (choiceNumber < 1 || choiceNumber > parameterGroupFamilies.Count) { Console.WriteLine("Select an available DB parameter group family by entering a number from the list above:"); var choice = Console.ReadLine(); Int32.TryParse(choice, out choiceNumber); } var parameterGroupFamilyChoice = parameterGroupFamilies[choiceNumber - 1]; Console.WriteLine(sepBar); return parameterGroupFamilyChoice.Key; } /// <summary> /// Create and get information on a DB parameter group. /// </summary> /// <param name="dbParameterGroupFamily">The DBParameterGroupFamily for the new DB parameter group.</param> /// <returns>The new DBParameterGroup.</returns> public static async Task<DBParameterGroup> CreateDbParameterGroup(string dbParameterGroupFamily) { Console.WriteLine(sepBar); Console.WriteLine($"2. Create new DB parameter group with family {dbParameterGroupFamily}:"); var parameterGroup = await rdsWrapper.CreateDBParameterGroup( "ExampleParameterGroup-" + DateTime.Now.Ticks, dbParameterGroupFamily, "New example parameter group"); var groupInfo = await rdsWrapper.DescribeDBParameterGroups(parameterGroup .DBParameterGroupName); Console.WriteLine( $"3. New DB parameter group: \n\t{groupInfo[0].Description}, \n\tARN {groupInfo[0].DBParameterGroupArn}"); Console.WriteLine(sepBar); return parameterGroup; } /// <summary> /// Get and describe parameters from a DBParameterGroup. /// </summary> /// <param name="parameterGroupName">Name of the DBParameterGroup.</param> /// <param name="parameterNames">Optional specific names of parameters to describe.</param> /// <returns>The list of requested parameters.</returns> public static async Task<List<Parameter>> DescribeParametersInGroup(string parameterGroupName, List<string>? parameterNames = null) { Console.WriteLine(sepBar); Console.WriteLine("4. Get some parameters from the group."); Console.WriteLine(sepBar); var parameters = await rdsWrapper.DescribeDBParameters(parameterGroupName); var matchingParameters = parameters.Where(p => parameterNames == null || parameterNames.Contains(p.ParameterName)).ToList(); Console.WriteLine("5. Parameter information:"); matchingParameters.ForEach(p => Console.WriteLine( $"\n\tParameter: {p.ParameterName}." + $"\n\tDescription: {p.Description}." + $"\n\tAllowed Values: {p.AllowedValues}." + $"\n\tValue: {p.ParameterValue}.")); Console.WriteLine(sepBar); return matchingParameters; } /// <summary> /// Modify a parameter from a DBParameterGroup. /// </summary> /// <param name="parameterGroupName">Name of the DBParameterGroup.</param> /// <param name="parameters">The parameters to modify.</param> /// <returns>Async task.</returns> public static async Task ModifyParameters(string parameterGroupName, List<Parameter> parameters) { Console.WriteLine(sepBar); Console.WriteLine("6. Modify some parameters in the group."); foreach (var p in parameters) { if (p.IsModifiable && p.DataType == "integer") { int newValue = 0; while (newValue == 0) { Console.WriteLine( $"Enter a new value for {p.ParameterName} from the allowed values {p.AllowedValues} "); var choice = Console.ReadLine(); Int32.TryParse(choice, out newValue); } p.ParameterValue = newValue.ToString(); } } await rdsWrapper.ModifyDBParameterGroup(parameterGroupName, parameters); Console.WriteLine(sepBar); } /// <summary> /// Describe the user source parameters in the group. /// </summary> /// <param name="parameterGroupName">Name of the DBParameterGroup.</param> /// <returns>Async task.</returns> public static async Task DescribeUserSourceParameters(string parameterGroupName) { Console.WriteLine(sepBar); Console.WriteLine("7. Describe user source parameters in the group."); var parameters = await rdsWrapper.DescribeDBParameters(parameterGroupName, "user"); parameters.ForEach(p => Console.WriteLine( $"\n\tParameter: {p.ParameterName}." + $"\n\tDescription: {p.Description}." + $"\n\tAllowed Values: {p.AllowedValues}." + $"\n\tValue: {p.ParameterValue}.")); Console.WriteLine(sepBar); } /// <summary> /// Choose a DB engine version. /// </summary> /// <param name="dbParameterGroupFamily">DB parameter group family for engine choice.</param> /// <returns>The selected engine version.</returns> public static async Task<DBEngineVersion> ChooseDbEngineVersion(string dbParameterGroupFamily) { Console.WriteLine(sepBar); // Get a list of allowed engines. var allowedEngines = await rdsWrapper.DescribeDBEngineVersions(engine, dbParameterGroupFamily); Console.WriteLine($"Available DB engine versions for parameter group family {dbParameterGroupFamily}:"); int i = 1; foreach (var version in allowedEngines) { Console.WriteLine( $"\t{i}. Engine: {version.Engine} Version {version.EngineVersion}."); i++; } var choiceNumber = 0; while (choiceNumber < 1 || choiceNumber > allowedEngines.Count) { Console.WriteLine("8. Select an available DB engine version by entering a number from the list above:"); var choice = Console.ReadLine(); Int32.TryParse(choice, out choiceNumber); } var engineChoice = allowedEngines[choiceNumber - 1]; Console.WriteLine(sepBar); return engineChoice; } /// <summary> /// Choose a DB instance class for a particular engine and engine version. /// </summary> /// <param name="engine">DB engine for DB instance choice.</param> /// <param name="engineVersion">DB engine version for DB instance choice.</param> /// <returns>The selected orderable DB instance option.</returns> public static async Task<OrderableDBInstanceOption> ChooseDbInstanceClass(string engine, string engineVersion) { Console.WriteLine(sepBar); // Get a list of allowed DB instance classes. var allowedInstances = await rdsWrapper.DescribeOrderableDBInstanceOptions(engine, engineVersion); Console.WriteLine($"8. Available micro DB instance classes for engine {engine} and version {engineVersion}:"); int i = 1; // Filter to micro instances for this example. allowedInstances = allowedInstances .Where(i => i.DBInstanceClass.Contains("micro")).ToList(); foreach (var instance in allowedInstances) { Console.WriteLine( $"\t{i}. Instance class: {instance.DBInstanceClass} (storage type {instance.StorageType})"); i++; } var choiceNumber = 0; while (choiceNumber < 1 || choiceNumber > allowedInstances.Count) { Console.WriteLine("9. Select an available DB instance class by entering a number from the list above:"); var choice = Console.ReadLine(); Int32.TryParse(choice, out choiceNumber); } var instanceChoice = allowedInstances[choiceNumber - 1]; Console.WriteLine(sepBar); return instanceChoice; } /// <summary> /// Create a new RDS DB instance. /// </summary> /// <param name="parameterGroup">Parameter group to use for the DB instance.</param> /// <param name="engineName">Engine to use for the DB instance.</param> /// <param name="engineVersion">Engine version to use for the DB instance.</param> /// <param name="instanceClass">Instance class to use for the DB instance.</param> /// <param name="instanceIdentifier">Instance identifier to use for the DB instance.</param> /// <returns>The new DB instance.</returns> public static async Task<DBInstance?> CreateRdsNewInstance(DBParameterGroup parameterGroup, string engineName, string engineVersion, string instanceClass, string instanceIdentifier) { Console.WriteLine(sepBar); Console.WriteLine($"10. Create a new DB instance with identifier {instanceIdentifier}."); bool isInstanceReady = false; DBInstance newInstance; var instances = await rdsWrapper.DescribeDBInstances(); isInstanceReady = instances.FirstOrDefault(i => i.DBInstanceIdentifier == instanceIdentifier)?.DBInstanceStatus == "available"; if (isInstanceReady) { Console.WriteLine("Instance already created."); newInstance = instances.First(i => i.DBInstanceIdentifier == instanceIdentifier); } else { Console.WriteLine("Please enter an admin user name:"); var username = Console.ReadLine(); Console.WriteLine("Please enter an admin password:"); var password = Console.ReadLine(); newInstance = await rdsWrapper.CreateDBInstance( "ExampleInstance", instanceIdentifier, parameterGroup.DBParameterGroupName, engineName, engineVersion, instanceClass, 20, username, password ); // 11. Wait for the DB instance to be ready. Console.WriteLine("11. Waiting for DB instance to be ready..."); while (!isInstanceReady) { instances = await rdsWrapper.DescribeDBInstances(instanceIdentifier); isInstanceReady = instances.FirstOrDefault()?.DBInstanceStatus == "available"; newInstance = instances.First(); Thread.Sleep(30000); } } Console.WriteLine(sepBar); return newInstance; } /// <summary> /// Display a connection string for an RDS DB instance. /// </summary> /// <param name="instance">The DB instance to use to get a connection string.</param> public static void DisplayConnectionString(DBInstance instance) { Console.WriteLine(sepBar); // Display the connection string. Console.WriteLine("12. New DB instance connection string: "); Console.WriteLine( $"\n{engine} -h {instance.Endpoint.Address} -P {instance.Endpoint.Port} " + $"-u {instance.MasterUsername} -p [YOUR PASSWORD]\n"); Console.WriteLine(sepBar); } /// <summary> /// Create a snapshot from an RDS DB instance. /// </summary> /// <param name="instance">DB instance to use when creating a snapshot.</param> /// <returns>The snapshot object.</returns> public static async Task<DBSnapshot> CreateSnapshot(DBInstance instance) { Console.WriteLine(sepBar); // Create a snapshot. Console.WriteLine($"13. Creating snapshot from DB instance {instance.DBInstanceIdentifier}."); var snapshot = await rdsWrapper.CreateDBSnapshot(instance.DBInstanceIdentifier, "ExampleSnapshot-" + DateTime.Now.Ticks); // Wait for the snapshot to be available bool isSnapshotReady = false; Console.WriteLine($"14. Waiting for snapshot to be ready..."); while (!isSnapshotReady) { var snapshots = await rdsWrapper.DescribeDBSnapshots(instance.DBInstanceIdentifier); isSnapshotReady = snapshots.FirstOrDefault()?.Status == "available"; snapshot = snapshots.First(); Thread.Sleep(30000); } Console.WriteLine( $"Snapshot {snapshot.DBSnapshotIdentifier} status is {snapshot.Status}."); Console.WriteLine(sepBar); return snapshot; } /// <summary> /// Delete an RDS DB instance. /// </summary> /// <param name="instance">The DB instance to delete.</param> /// <returns>Async task.</returns> public static async Task DeleteRdsInstance(DBInstance newInstance) { Console.WriteLine(sepBar); // Delete the DB instance. Console.WriteLine($"15. Delete the DB instance {newInstance.DBInstanceIdentifier}."); await rdsWrapper.DeleteDBInstance(newInstance.DBInstanceIdentifier); // Wait for the DB instance to delete. Console.WriteLine($"16. Waiting for the DB instance to delete..."); bool isInstanceDeleted = false; while (!isInstanceDeleted) { var instance = await rdsWrapper.DescribeDBInstances(); isInstanceDeleted = instance.All(i => i.DBInstanceIdentifier != newInstance.DBInstanceIdentifier); Thread.Sleep(30000); } Console.WriteLine("DB instance deleted."); Console.WriteLine(sepBar); } /// <summary> /// Delete a DB parameter group. /// </summary> /// <param name="parameterGroup">The parameter group to delete.</param> /// <returns>Async task.</returns> public static async Task DeleteParameterGroup(DBParameterGroup parameterGroup) { Console.WriteLine(sepBar); // Delete the parameter group. Console.WriteLine($"17. Delete the DB parameter group {parameterGroup.DBParameterGroupName}."); await rdsWrapper.DeleteDBParameterGroup(parameterGroup.DBParameterGroupName); Console.WriteLine(sepBar); }

Metode pembungkus yang digunakan oleh skenario untuk tindakan instans basis data.

/// <summary> /// Wrapper methods to use Amazon Relational Database Service (Amazon RDS) with DB instance operations. /// </summary> public partial class RDSWrapper { private readonly IAmazonRDS _amazonRDS; public RDSWrapper(IAmazonRDS amazonRDS) { _amazonRDS = amazonRDS; } /// <summary> /// Get a list of DB engine versions for a particular DB engine. /// </summary> /// <param name="engine">Name of the engine.</param> /// <param name="dbParameterGroupFamily">Optional parameter group family name.</param> /// <returns>List of DBEngineVersions.</returns> public async Task<List<DBEngineVersion>> DescribeDBEngineVersions(string engine, string dbParameterGroupFamily = null) { var response = await _amazonRDS.DescribeDBEngineVersionsAsync( new DescribeDBEngineVersionsRequest() { Engine = engine, DBParameterGroupFamily = dbParameterGroupFamily }); return response.DBEngineVersions; } /// <summary> /// Get a list of orderable DB instance options for a specific /// engine and engine version. /// </summary> /// <param name="engine">Name of the engine.</param> /// <param name="engineVersion">Version of the engine.</param> /// <returns>List of OrderableDBInstanceOptions.</returns> public async Task<List<OrderableDBInstanceOption>> DescribeOrderableDBInstanceOptions(string engine, string engineVersion) { // Use a paginator to get a list of DB instance options. var results = new List<OrderableDBInstanceOption>(); var paginateInstanceOptions = _amazonRDS.Paginators.DescribeOrderableDBInstanceOptions( new DescribeOrderableDBInstanceOptionsRequest() { Engine = engine, EngineVersion = engineVersion, }); // Get the entire list using the paginator. await foreach (var instanceOptions in paginateInstanceOptions.OrderableDBInstanceOptions) { results.Add(instanceOptions); } return results; } /// <summary> /// Returns a list of DB instances. /// </summary> /// <param name="dbInstanceIdentifier">Optional name of a specific DB instance.</param> /// <returns>List of DB instances.</returns> public async Task<List<DBInstance>> DescribeDBInstances(string dbInstanceIdentifier = null) { var results = new List<DBInstance>(); var instancesPaginator = _amazonRDS.Paginators.DescribeDBInstances( new DescribeDBInstancesRequest { DBInstanceIdentifier = dbInstanceIdentifier }); // Get the entire list using the paginator. await foreach (var instances in instancesPaginator.DBInstances) { results.Add(instances); } return results; } /// <summary> /// Create an RDS DB instance with a particular set of properties. Use the action DescribeDBInstancesAsync /// to determine when the DB instance is ready to use. /// </summary> /// <param name="dbName">Name for the DB instance.</param> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <param name="parameterGroupName">DB parameter group to associate with the instance.</param> /// <param name="dbEngine">The engine for the DB instance.</param> /// <param name="dbEngineVersion">Version for the DB instance.</param> /// <param name="instanceClass">Class for the DB instance.</param> /// <param name="allocatedStorage">The amount of storage in gibibytes (GiB) to allocate to the DB instance.</param> /// <param name="adminName">Admin user name.</param> /// <param name="adminPassword">Admin user password.</param> /// <returns>DB instance object.</returns> public async Task<DBInstance> CreateDBInstance(string dbName, string dbInstanceIdentifier, string parameterGroupName, string dbEngine, string dbEngineVersion, string instanceClass, int allocatedStorage, string adminName, string adminPassword) { var response = await _amazonRDS.CreateDBInstanceAsync( new CreateDBInstanceRequest() { DBName = dbName, DBInstanceIdentifier = dbInstanceIdentifier, DBParameterGroupName = parameterGroupName, Engine = dbEngine, EngineVersion = dbEngineVersion, DBInstanceClass = instanceClass, AllocatedStorage = allocatedStorage, MasterUsername = adminName, MasterUserPassword = adminPassword }); return response.DBInstance; } /// <summary> /// Delete a particular DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <returns>DB instance object.</returns> public async Task<DBInstance> DeleteDBInstance(string dbInstanceIdentifier) { var response = await _amazonRDS.DeleteDBInstanceAsync( new DeleteDBInstanceRequest() { DBInstanceIdentifier = dbInstanceIdentifier, SkipFinalSnapshot = true, DeleteAutomatedBackups = true }); return response.DBInstance; }

Metode pembungkus yang digunakan oleh skenario untuk grup parameter basis data.

/// <summary> /// Wrapper methods to use Amazon Relational Database Service (Amazon RDS) with parameter groups. /// </summary> public partial class RDSWrapper { /// <summary> /// Get descriptions of DB parameter groups. /// </summary> /// <param name="name">Optional name of the DB parameter group to describe.</param> /// <returns>The list of DB parameter group descriptions.</returns> public async Task<List<DBParameterGroup>> DescribeDBParameterGroups(string name = null) { var response = await _amazonRDS.DescribeDBParameterGroupsAsync( new DescribeDBParameterGroupsRequest() { DBParameterGroupName = name }); return response.DBParameterGroups; } /// <summary> /// Create a new DB parameter group. Use the action DescribeDBParameterGroupsAsync /// to determine when the DB parameter group is ready to use. /// </summary> /// <param name="name">Name of the DB parameter group.</param> /// <param name="family">Family of the DB parameter group.</param> /// <param name="description">Description of the DB parameter group.</param> /// <returns>The new DB parameter group.</returns> public async Task<DBParameterGroup> CreateDBParameterGroup( string name, string family, string description) { var response = await _amazonRDS.CreateDBParameterGroupAsync( new CreateDBParameterGroupRequest() { DBParameterGroupName = name, DBParameterGroupFamily = family, Description = description }); return response.DBParameterGroup; } /// <summary> /// Update a DB parameter group. Use the action DescribeDBParameterGroupsAsync /// to determine when the DB parameter group is ready to use. /// </summary> /// <param name="name">Name of the DB parameter group.</param> /// <param name="parameters">List of parameters. Maximum of 20 per request.</param> /// <returns>The updated DB parameter group name.</returns> public async Task<string> ModifyDBParameterGroup( string name, List<Parameter> parameters) { var response = await _amazonRDS.ModifyDBParameterGroupAsync( new ModifyDBParameterGroupRequest() { DBParameterGroupName = name, Parameters = parameters, }); return response.DBParameterGroupName; } /// <summary> /// Delete a DB parameter group. The group cannot be a default DB parameter group /// or be associated with any DB instances. /// </summary> /// <param name="name">Name of the DB parameter group.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteDBParameterGroup(string name) { var response = await _amazonRDS.DeleteDBParameterGroupAsync( new DeleteDBParameterGroupRequest() { DBParameterGroupName = name, }); return response.HttpStatusCode == HttpStatusCode.OK; } /// <summary> /// Get a list of DB parameters from a specific parameter group. /// </summary> /// <param name="dbParameterGroupName">Name of a specific DB parameter group.</param> /// <param name="source">Optional source for selecting parameters.</param> /// <returns>List of parameter values.</returns> public async Task<List<Parameter>> DescribeDBParameters(string dbParameterGroupName, string source = null) { var results = new List<Parameter>(); var paginateParameters = _amazonRDS.Paginators.DescribeDBParameters( new DescribeDBParametersRequest() { DBParameterGroupName = dbParameterGroupName, Source = source }); // Get the entire list using the paginator. await foreach (var parameters in paginateParameters.Parameters) { results.Add(parameters); } return results; }

Metode pembungkus yang digunakan oleh skenario untuk tindakan cuplikan basis data.

/// <summary> /// Wrapper methods to use Amazon Relational Database Service (Amazon RDS) with snapshots. /// </summary> public partial class RDSWrapper { /// <summary> /// Create a snapshot of a DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <param name="snapshotIdentifier">Identifier for the snapshot.</param> /// <returns>DB snapshot object.</returns> public async Task<DBSnapshot> CreateDBSnapshot(string dbInstanceIdentifier, string snapshotIdentifier) { var response = await _amazonRDS.CreateDBSnapshotAsync( new CreateDBSnapshotRequest() { DBSnapshotIdentifier = snapshotIdentifier, DBInstanceIdentifier = dbInstanceIdentifier }); return response.DBSnapshot; } /// <summary> /// Return a list of DB snapshots for a particular DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <returns>List of DB snapshots.</returns> public async Task<List<DBSnapshot>> DescribeDBSnapshots(string dbInstanceIdentifier) { var results = new List<DBSnapshot>(); var snapshotsPaginator = _amazonRDS.Paginators.DescribeDBSnapshots( new DescribeDBSnapshotsRequest() { DBInstanceIdentifier = dbInstanceIdentifier }); // Get the entire list using the paginator. await foreach (var snapshots in snapshotsPaginator.DBSnapshots) { results.Add(snapshots); } return results; }

Tindakan

Contoh kode berikut menunjukkan cara menggunakanCreateDBInstance.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

/// <summary> /// Create an RDS DB instance with a particular set of properties. Use the action DescribeDBInstancesAsync /// to determine when the DB instance is ready to use. /// </summary> /// <param name="dbName">Name for the DB instance.</param> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <param name="parameterGroupName">DB parameter group to associate with the instance.</param> /// <param name="dbEngine">The engine for the DB instance.</param> /// <param name="dbEngineVersion">Version for the DB instance.</param> /// <param name="instanceClass">Class for the DB instance.</param> /// <param name="allocatedStorage">The amount of storage in gibibytes (GiB) to allocate to the DB instance.</param> /// <param name="adminName">Admin user name.</param> /// <param name="adminPassword">Admin user password.</param> /// <returns>DB instance object.</returns> public async Task<DBInstance> CreateDBInstance(string dbName, string dbInstanceIdentifier, string parameterGroupName, string dbEngine, string dbEngineVersion, string instanceClass, int allocatedStorage, string adminName, string adminPassword) { var response = await _amazonRDS.CreateDBInstanceAsync( new CreateDBInstanceRequest() { DBName = dbName, DBInstanceIdentifier = dbInstanceIdentifier, DBParameterGroupName = parameterGroupName, Engine = dbEngine, EngineVersion = dbEngineVersion, DBInstanceClass = instanceClass, AllocatedStorage = allocatedStorage, MasterUsername = adminName, MasterUserPassword = adminPassword }); return response.DBInstance; }

Contoh kode berikut menunjukkan cara menggunakanCreateDBParameterGroup.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

/// <summary> /// Create a new DB parameter group. Use the action DescribeDBParameterGroupsAsync /// to determine when the DB parameter group is ready to use. /// </summary> /// <param name="name">Name of the DB parameter group.</param> /// <param name="family">Family of the DB parameter group.</param> /// <param name="description">Description of the DB parameter group.</param> /// <returns>The new DB parameter group.</returns> public async Task<DBParameterGroup> CreateDBParameterGroup( string name, string family, string description) { var response = await _amazonRDS.CreateDBParameterGroupAsync( new CreateDBParameterGroupRequest() { DBParameterGroupName = name, DBParameterGroupFamily = family, Description = description }); return response.DBParameterGroup; }

Contoh kode berikut menunjukkan cara menggunakanCreateDBSnapshot.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

/// <summary> /// Create a snapshot of a DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <param name="snapshotIdentifier">Identifier for the snapshot.</param> /// <returns>DB snapshot object.</returns> public async Task<DBSnapshot> CreateDBSnapshot(string dbInstanceIdentifier, string snapshotIdentifier) { var response = await _amazonRDS.CreateDBSnapshotAsync( new CreateDBSnapshotRequest() { DBSnapshotIdentifier = snapshotIdentifier, DBInstanceIdentifier = dbInstanceIdentifier }); return response.DBSnapshot; }

Contoh kode berikut menunjukkan cara menggunakanDeleteDBInstance.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

/// <summary> /// Delete a particular DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <returns>DB instance object.</returns> public async Task<DBInstance> DeleteDBInstance(string dbInstanceIdentifier) { var response = await _amazonRDS.DeleteDBInstanceAsync( new DeleteDBInstanceRequest() { DBInstanceIdentifier = dbInstanceIdentifier, SkipFinalSnapshot = true, DeleteAutomatedBackups = true }); return response.DBInstance; }

Contoh kode berikut menunjukkan cara menggunakanDeleteDBParameterGroup.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

/// <summary> /// Delete a DB parameter group. The group cannot be a default DB parameter group /// or be associated with any DB instances. /// </summary> /// <param name="name">Name of the DB parameter group.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteDBParameterGroup(string name) { var response = await _amazonRDS.DeleteDBParameterGroupAsync( new DeleteDBParameterGroupRequest() { DBParameterGroupName = name, }); return response.HttpStatusCode == HttpStatusCode.OK; }

Contoh kode berikut menunjukkan cara menggunakanDescribeDBEngineVersions.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

/// <summary> /// Get a list of DB engine versions for a particular DB engine. /// </summary> /// <param name="engine">Name of the engine.</param> /// <param name="dbParameterGroupFamily">Optional parameter group family name.</param> /// <returns>List of DBEngineVersions.</returns> public async Task<List<DBEngineVersion>> DescribeDBEngineVersions(string engine, string dbParameterGroupFamily = null) { var response = await _amazonRDS.DescribeDBEngineVersionsAsync( new DescribeDBEngineVersionsRequest() { Engine = engine, DBParameterGroupFamily = dbParameterGroupFamily }); return response.DBEngineVersions; }

Contoh kode berikut menunjukkan cara menggunakanDescribeDBInstances.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

/// <summary> /// Returns a list of DB instances. /// </summary> /// <param name="dbInstanceIdentifier">Optional name of a specific DB instance.</param> /// <returns>List of DB instances.</returns> public async Task<List<DBInstance>> DescribeDBInstances(string dbInstanceIdentifier = null) { var results = new List<DBInstance>(); var instancesPaginator = _amazonRDS.Paginators.DescribeDBInstances( new DescribeDBInstancesRequest { DBInstanceIdentifier = dbInstanceIdentifier }); // Get the entire list using the paginator. await foreach (var instances in instancesPaginator.DBInstances) { results.Add(instances); } return results; }

Contoh kode berikut menunjukkan cara menggunakanDescribeDBParameterGroups.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

/// <summary> /// Get descriptions of DB parameter groups. /// </summary> /// <param name="name">Optional name of the DB parameter group to describe.</param> /// <returns>The list of DB parameter group descriptions.</returns> public async Task<List<DBParameterGroup>> DescribeDBParameterGroups(string name = null) { var response = await _amazonRDS.DescribeDBParameterGroupsAsync( new DescribeDBParameterGroupsRequest() { DBParameterGroupName = name }); return response.DBParameterGroups; }

Contoh kode berikut menunjukkan cara menggunakanDescribeDBParameters.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

/// <summary> /// Get a list of DB parameters from a specific parameter group. /// </summary> /// <param name="dbParameterGroupName">Name of a specific DB parameter group.</param> /// <param name="source">Optional source for selecting parameters.</param> /// <returns>List of parameter values.</returns> public async Task<List<Parameter>> DescribeDBParameters(string dbParameterGroupName, string source = null) { var results = new List<Parameter>(); var paginateParameters = _amazonRDS.Paginators.DescribeDBParameters( new DescribeDBParametersRequest() { DBParameterGroupName = dbParameterGroupName, Source = source }); // Get the entire list using the paginator. await foreach (var parameters in paginateParameters.Parameters) { results.Add(parameters); } return results; }

Contoh kode berikut menunjukkan cara menggunakanDescribeDBSnapshots.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

/// <summary> /// Return a list of DB snapshots for a particular DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <returns>List of DB snapshots.</returns> public async Task<List<DBSnapshot>> DescribeDBSnapshots(string dbInstanceIdentifier) { var results = new List<DBSnapshot>(); var snapshotsPaginator = _amazonRDS.Paginators.DescribeDBSnapshots( new DescribeDBSnapshotsRequest() { DBInstanceIdentifier = dbInstanceIdentifier }); // Get the entire list using the paginator. await foreach (var snapshots in snapshotsPaginator.DBSnapshots) { results.Add(snapshots); } return results; }

Contoh kode berikut menunjukkan cara menggunakanDescribeOrderableDBInstanceOptions.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

/// <summary> /// Get a list of orderable DB instance options for a specific /// engine and engine version. /// </summary> /// <param name="engine">Name of the engine.</param> /// <param name="engineVersion">Version of the engine.</param> /// <returns>List of OrderableDBInstanceOptions.</returns> public async Task<List<OrderableDBInstanceOption>> DescribeOrderableDBInstanceOptions(string engine, string engineVersion) { // Use a paginator to get a list of DB instance options. var results = new List<OrderableDBInstanceOption>(); var paginateInstanceOptions = _amazonRDS.Paginators.DescribeOrderableDBInstanceOptions( new DescribeOrderableDBInstanceOptionsRequest() { Engine = engine, EngineVersion = engineVersion, }); // Get the entire list using the paginator. await foreach (var instanceOptions in paginateInstanceOptions.OrderableDBInstanceOptions) { results.Add(instanceOptions); } return results; }

Contoh kode berikut menunjukkan cara menggunakanModifyDBParameterGroup.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

/// <summary> /// Update a DB parameter group. Use the action DescribeDBParameterGroupsAsync /// to determine when the DB parameter group is ready to use. /// </summary> /// <param name="name">Name of the DB parameter group.</param> /// <param name="parameters">List of parameters. Maximum of 20 per request.</param> /// <returns>The updated DB parameter group name.</returns> public async Task<string> ModifyDBParameterGroup( string name, List<Parameter> parameters) { var response = await _amazonRDS.ModifyDBParameterGroupAsync( new ModifyDBParameterGroupRequest() { DBParameterGroupName = name, Parameters = parameters, }); return response.DBParameterGroupName; }

Skenario

Contoh kode berikut menunjukkan cara membuat aplikasi web yang melacak item pekerjaan dalam database Amazon Aurora Tanpa Server dan menggunakan Amazon Simple Email Service (AmazonSES) untuk mengirim laporan.

AWS SDK for .NET

Menunjukkan cara menggunakan AWS SDK for .NET untuk membuat aplikasi web yang melacak item pekerjaan dalam database Amazon Aurora dan laporan email dengan menggunakan Amazon Simple Email Service (AmazonSES). Contoh ini menggunakan front end yang dibangun dengan React.js untuk berinteraksi dengan fileRESTful. NETbackend.

  • Integrasikan aplikasi web React dengan AWS layanan.

  • Cantumkan, tambahkan, perbarui, dan hapus butir di tabel Aurora.

  • Kirim laporan email item pekerjaan yang difilter menggunakan AmazonSES.

  • Menyebarkan dan mengelola sumber daya contoh dengan AWS CloudFormation skrip yang disertakan.

Untuk kode sumber lengkap dan instruksi tentang cara mengatur dan menjalankan, lihat contoh lengkapnya di GitHub.

Layanan yang digunakan dalam contoh ini
  • Aurora

  • Amazon RDS

  • Layanan RDS Data Amazon

  • Amazon SES