Semplice applicazione multipiattaforma con AWS SDK for .NET - AWS SDK for .NET

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Semplice applicazione multipiattaforma con AWS SDK for .NET

Questo tutorial utilizza e.NET Core per lo sviluppo multipiattaforma. AWS SDK for .NET Il tutorial mostra come utilizzare l'SDK per elencare i bucket Amazon S3 di cui sei proprietario e, facoltativamente, creare un bucket.

Eseguirai questo tutorial utilizzando strumenti multipiattaforma come l'interfaccia a riga di comando (CLI) .NET. Per altri modi di configurare il tuo ambiente di sviluppo, consulta. Installa e configura la tua toolchain

Necessario per lo sviluppo multipiattaforma in Windows, Linux o macOS:

  • Microsoft .NET Core SDK, versione 2.1, 3.1 o versioni successive, che include l'interfaccia a riga di comando (CLI) .NET (dotnet) e il .NET Core Runtime.

Fasi

Creazione del progetto

  1. Apri il prompt dei comandi o il terminale. Trovare o creare una cartella del sistema operativo in cui è possibile creare un progetto.NET.

  2. In tale cartella, eseguire il seguente comando per creare il progetto.NET.

    dotnet new console --name S3CreateAndList
  3. Vai alla S3CreateAndList cartella appena creata ed esegui i seguenti comandi:

    dotnet add package AWSSDK.S3 dotnet add package AWSSDK.SecurityToken dotnet add package AWSSDK.SSO dotnet add package AWSSDK.SSOOIDC

    I comandi precedenti installano i NuGet pacchetti dal gestore di NuGet pacchetti. Poiché sappiamo esattamente di quali NuGet pacchetti abbiamo bisogno per questo tutorial, ora possiamo eseguire questo passaggio. È anche comune che i pacchetti richiesti vengano resi noti durante lo sviluppo. Quando ciò accade, un comando simile può essere eseguito in quel momento.

Creazione del codice

  1. Nella cartella S3CreateAndList, trovare e aprire Program.cs nell'editor del codice.

  2. Sostituire il contenuto con il seguente codice e salvare il file.

    using System; using System.Threading.Tasks; // NuGet packages: AWSSDK.S3, AWSSDK.SecurityToken, AWSSDK.SSO, AWSSDK.SSOOIDC using Amazon.Runtime; using Amazon.Runtime.CredentialManagement; using Amazon.S3; using Amazon.S3.Model; using Amazon.SecurityToken; using Amazon.SecurityToken.Model; namespace S3CreateAndList { class Program { // This code is part of the quick tour in the developer guide. // See https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/quick-start.html // for complete steps. // Requirements: // - An SSO profile in the SSO user's shared config file with sufficient privileges for // STS and S3 buckets. // - An active SSO Token. // If an active SSO token isn't available, the SSO user should do the following: // In a terminal, the SSO user must call "aws sso login". // Class members. static async Task Main(string[] args) { // Get SSO credentials from the information in the shared config file. // For this tutorial, the information is in the [default] profile. var ssoCreds = LoadSsoCredentials("default"); // Display the caller's identity. var ssoProfileClient = new AmazonSecurityTokenServiceClient(ssoCreds); Console.WriteLine($"\nSSO Profile:\n {await ssoProfileClient.GetCallerIdentityArn()}"); // Create the S3 client is by using the SSO credentials obtained earlier. var s3Client = new AmazonS3Client(ssoCreds); // Parse the command line arguments for the bucket name. if (GetBucketName(args, out String bucketName)) { // If a bucket name was supplied, create the bucket. // Call the API method directly try { Console.WriteLine($"\nCreating bucket {bucketName}..."); var createResponse = await s3Client.PutBucketAsync(bucketName); Console.WriteLine($"Result: {createResponse.HttpStatusCode.ToString()}"); } catch (Exception e) { Console.WriteLine("Caught exception when creating a bucket:"); Console.WriteLine(e.Message); } } // Display a list of the account's S3 buckets. Console.WriteLine("\nGetting a list of your buckets..."); var listResponse = await s3Client.ListBucketsAsync(); Console.WriteLine($"Number of buckets: {listResponse.Buckets.Count}"); foreach (S3Bucket b in listResponse.Buckets) { Console.WriteLine(b.BucketName); } Console.WriteLine(); } // // Method to parse the command line. private static Boolean GetBucketName(string[] args, out String bucketName) { Boolean retval = false; bucketName = String.Empty; if (args.Length == 0) { Console.WriteLine("\nNo arguments specified. Will simply list your Amazon S3 buckets." + "\nIf you wish to create a bucket, supply a valid, globally unique bucket name."); bucketName = String.Empty; retval = false; } else if (args.Length == 1) { bucketName = args[0]; retval = true; } else { Console.WriteLine("\nToo many arguments specified." + "\n\ndotnet_tutorials - A utility to list your Amazon S3 buckets and optionally create a new one." + "\n\nUsage: S3CreateAndList [bucket_name]" + "\n - bucket_name: A valid, globally unique bucket name." + "\n - If bucket_name isn't supplied, this utility simply lists your buckets."); Environment.Exit(1); } return retval; } // // Method to get SSO credentials from the information in the shared config file. static AWSCredentials LoadSsoCredentials(string profile) { var chain = new CredentialProfileStoreChain(); if (!chain.TryGetAWSCredentials(profile, out var credentials)) throw new Exception($"Failed to find the {profile} profile"); return credentials; } } // Class to read the caller's identity. public static class Extensions { public static async Task<string> GetCallerIdentityArn(this IAmazonSecurityTokenService stsClient) { var response = await stsClient.GetCallerIdentityAsync(new GetCallerIdentityRequest()); return response.Arn; } } }

Esecuzione dell'applicazione.

  1. Esegui il comando seguente.

    dotnet run
  2. Esamina l'output per vedere il numero di bucket Amazon S3 che possiedi, se ce ne sono, e i relativi nomi.

  3. Scegli un nome per un nuovo bucket Amazon S3. Usa "dotnet-quicktour-s3-1-cross-» come base e aggiungi qualcosa di unico, come un GUID o il tuo nome. Assicurati di seguire le regole per i nomi dei bucket, come descritto in Regole per la denominazione dei bucket nella Amazon S3 User Guide.

  4. Eseguire il comando seguente, sostituendo BUCKET_NAME con il nome del bucket scelto.

    dotnet run BUCKET-NAME
  5. Esaminare l'output per visualizzare il nuovo bucket creato.

Rimozione

Durante l'esecuzione di questo tutorial, hai creato alcune risorse che puoi scegliere di pulire in questo momento.

Fasi successive

Torna al menu del tour rapido o vai direttamente alla fine di questo tour rapido.