S3 Tags Instructions

Introduction

When using the Bucket to store files, you can categorize the stored content using tags. Each tag is a key-value pair that you can add to a file when uploading it or add to already uploaded files. You can also query tags using the bucket and file name (Keyname). This method is compatible with AWS S3. For more detailed information about tags, please refer to AWS S3 Tag.

Example

Uploading a file and adding tags

func ExampleS3_PutObject() {
 svc := s3.New(session.New())
 input := &s3.PutObjectInput{
  Body:                 aws.ReadSeekCloser(strings.NewReader("filetoupload")),
  Bucket:               aws.String("examplebucket"),
  Key:                  aws.String("exampleobject"),
  Tagging:              aws.String("key1=value1&key2=value2"),
 }

 result, err := svc.PutObject(input)
 if err != nil {
  if aerr, ok := err.(awserr.Error); ok {
   switch aerr.Code() {
   default:
    fmt.Println(aerr.Error())
   }
  } else {
   // Print the error, cast err to awserr.Error to get the Code and
   // Message from an error.
   fmt.Println(err.Error())
  }
  return
 }

 fmt.Println(result)
}

Adding tags to existing files

func ExampleS3_PutObjectTagging() {
    svc := s3.New(session.New())
    input := &s3.PutObjectTaggingInput{
       Bucket: aws.String("examplebucket"),
       Key:    aws.String("exampleobject"),
       Tagging: &s3.Tagging{
          TagSet: []*s3.Tag{
             {
                Key:   aws.String("Key3"),
                Value: aws.String("Value3"),
             },
             {
                Key:   aws.String("Key4"),
                Value: aws.String("Value4"),
             },
          },
       },
    }

    result, err := svc.PutObjectTagging(input)
    if err != nil {
       if aerr, ok := err.(awserr.Error); ok {
          switch aerr.Code() {
          default:
             fmt.Println(aerr.Error())
          }
       } else {
          // Print the error, cast err to awserr.Error to get the Code and
          // Message from an error.
          fmt.Println(err.Error())
       }
       return
    }

    fmt.Println(result)
}

Requesting the corresponding tag through the bucket and filename

func ExampleS3_GetObjectTagging() {
    svc := s3.New(session.New())
    input := &s3.GetObjectTaggingInput{
       Bucket: aws.String("examplebucket"),
       Key:    aws.String("exampleobject"),
    }

    result, err := svc.GetObjectTagging(input)
    if err != nil {
       if aerr, ok := err.(awserr.Error); ok {
          switch aerr.Code() {
          default:
             fmt.Println(aerr.Error())
          }
       } else {
          // Print the error, cast err to awserr.Error to get the Code and
          // Message from an error.
          fmt.Println(err.Error())
       }
       return
    }

    fmt.Println(result)
}

Field descriptions

Container for the TagSet and Tag elements

Tagging type:map

A collection of a set of tags

TagSet (required): Array<map>

Name of the object key

Key (required) type: String

Value of the tag

Value (required) type: String

The key and value are case-sensitive. For more information about tag limitations, please refer to User-Defined Tag Restrictions.

Last updated