Gestione di tag degli oggetti - Amazon Simple Storage Service

Gestione di tag degli oggetti

In questa sezione viene spiegato come gestire i tag oggetto utilizzando gli SDK AWS per Java e .NET o la console di Amazon S3.

Il tagging degli oggetti consente di classificare l’archiviazione in bucket per uso generico. Ciascun tag è una coppia chiave-valore che aderisce alle seguenti regole:

  • È possibile associare fino a un massimo di 10 tag a ciascun oggetto. I tag associati a un oggetto devono avere chiavi di tag univoche.

  • Una chiave di tag può essere composta da un massimo di 128 caratteri Unicode e i valori di tag possono essere composti da un massimo di 256 caratteri Unicode. I tag di oggetti Amazon S3 sono rappresentati internamente in UTF-16. I caratteri in UTF-16 usano 1 o 2 posizioni.

  • La chiave e i valori fanno distinzione tra maiuscole e minuscole.

Per ulteriori informazioni sui tag degli oggetti, consulta Classificazione degli oggetti utilizzando i tag. Per ulteriori informazioni sui limiti dei tag, consulta Restrizioni sui tag definiti dall'utente nella Guida per l'utente Gestione dei costi e fatturazione AWS.

Per aggiungere tag a un oggetto
  1. Accedi alla Console di gestione AWS e apri la console Amazon S3 all’indirizzo https://console.aws.amazon.com/s3/.

  2. Nel riquadro di navigazione sinistro, scegli Bucket per uso generico.

  3. Nell’elenco dei bucket scegli il nome del bucket contenente l’oggetto.

  4. Seleziona la casella di controllo a sinistra dei nomi degli oggetti da modificare.

  5. Dal menu Operazioni, seleziona Modifica tag.

  6. Esamina gli oggetti elencati e seleziona Aggiungi tag.

  7. Ogni tag oggetto è una coppia chiave-valore. Immettere una chiave e un valore. Per aggiungere un altro tag, scegliere Add Tag (Aggiungi tag).

    È possibile immettere fino a un massimo di 10 tag per ciascun oggetto.

  8. Seleziona Salva modifiche.

    Amazon S3 aggiungerà i tag agli oggetti specificati.

Per ulteriori informazioni, vedi anche Visualizzazione delle proprietà di un oggetto nella console di Amazon S3 e Caricamento degli oggetti in questa guida.

Java

Per gestire i tag di oggetto con AWS SDK per Java, è possibile impostare i tag per un nuovo oggetto e recuperare o sostituire i tag per un oggetto esistente. Per ulteriori informazioni sul tagging dell'oggetto, consulta Classificazione degli oggetti utilizzando i tag.

Caricare un oggetto in un bucket e impostare tag mediante un'interfaccia S3Client. Per esempi, consulta Caricamento di un oggetto in un bucket nella Guida di riferimento delle API di Amazon S3.

.NET

Il seguente esempio mostra come utilizzare AWS SDK per .NET per impostare i tag per un nuovo oggetto e recuperare o sostituire i tag per un oggetto esistente. Per ulteriori informazioni sul tagging dell'oggetto, consulta Classificazione degli oggetti utilizzando i tag.

Per informazioni sulla configurazione e l'esecuzione degli esempi di codice, consulta Nozioni di base sull'SDK AWS per .NET nella Guida per gli sviluppatori di SDK AWS per .NET.

using Amazon; using Amazon.S3; using Amazon.S3.Model; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Amazon.DocSamples.S3 { public class ObjectTagsTest { private const string bucketName = "*** bucket name ***"; private const string keyName = "*** key name for the new object ***"; private const string filePath = @"*** file path ***"; // Specify your bucket region (an example region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static IAmazonS3 client; public static void Main() { client = new AmazonS3Client(bucketRegion); PutObjectWithTagsTestAsync().Wait(); } static async Task PutObjectWithTagsTestAsync() { try { // 1. Put an object with tags. var putRequest = new PutObjectRequest { BucketName = bucketName, Key = keyName, FilePath = filePath, TagSet = new List<Tag>{ new Tag { Key = "Keyx1", Value = "Value1"}, new Tag { Key = "Keyx2", Value = "Value2" } } }; PutObjectResponse response = await client.PutObjectAsync(putRequest); // 2. Retrieve the object's tags. GetObjectTaggingRequest getTagsRequest = new GetObjectTaggingRequest { BucketName = bucketName, Key = keyName }; GetObjectTaggingResponse objectTags = await client.GetObjectTaggingAsync(getTagsRequest); for (int i = 0; i < objectTags.Tagging.Count; i++) Console.WriteLine("Key: {0}, Value: {1}", objectTags.Tagging[i].Key, objectTags.Tagging[i].Value); // 3. Replace the tagset. Tagging newTagSet = new Tagging(); newTagSet.TagSet = new List<Tag>{ new Tag { Key = "Key3", Value = "Value3"}, new Tag { Key = "Key4", Value = "Value4" } }; PutObjectTaggingRequest putObjTagsRequest = new PutObjectTaggingRequest() { BucketName = bucketName, Key = keyName, Tagging = newTagSet }; PutObjectTaggingResponse response2 = await client.PutObjectTaggingAsync(putObjTagsRequest); // 4. Retrieve the object's tags. GetObjectTaggingRequest getTagsRequest2 = new GetObjectTaggingRequest(); getTagsRequest2.BucketName = bucketName; getTagsRequest2.Key = keyName; GetObjectTaggingResponse objectTags2 = await client.GetObjectTaggingAsync(getTagsRequest2); for (int i = 0; i < objectTags2.Tagging.Count; i++) Console.WriteLine("Key: {0}, Value: {1}", objectTags2.Tagging[i].Key, objectTags2.Tagging[i].Value); } catch (AmazonS3Exception e) { Console.WriteLine( "Error encountered ***. Message:'{0}' when writing an object" , e.Message); } catch (Exception e) { Console.WriteLine( "Encountered an error. Message:'{0}' when writing an object" , e.Message); } } } }