C# AWS S3: Add a Set of Tags to an S3 Object

Back to Index

Adds a set of tags to an existing S3 object. This example adds tags to the object named "starfish.jpg" located in the bucket "chilkat100".

Documentation: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUTtagging.html

CURL Command

curl -X PUT https://chilkat100.s3.amazonaws.com/starfish.jpg?tagging \
  -d '<Tagging>
  <TagSet>
     <Tag>
       <Key>animal</Key>
       <Value>starfish</Value>
     </Tag>
  </TagSet>
</Tagging>'

C# Example

Chilkat.Rest rest = new Chilkat.Rest();
bool success;

Chilkat.AuthAws authAws = new Chilkat.AuthAws();
authAws.AccessKey = "AWS_ACCESS_KEY";
authAws.SecretKey = "AWS_SECRET_KEY";
authAws.Region = "us-east-1";
authAws.ServiceName = "s3";
rest.SetAuthAws(authAws);

//  URL: https://chilkat100.s3.amazonaws.com/starfish.jpg?tagging
bool bTls = true;
int port = 443;
bool bAutoReconnect = true;
success = rest.Connect("chilkat100.s3.amazonaws.com",port,bTls,bAutoReconnect);
if (success != true) {
    Debug.WriteLine("ConnectFailReason: " + Convert.ToString(rest.ConnectFailReason));
    Debug.WriteLine(rest.LastErrorText);
    return;
}

//  See the Online Tool for Generating XML Creation Code
Chilkat.Xml xml = new Chilkat.Xml();
xml.Tag = "Tagging";
xml.UpdateChildContent("TagSet|Tag|Key","animal");
xml.UpdateChildContent("TagSet|Tag|Value","starfish");

Chilkat.StringBuilder sbRequestBody = new Chilkat.StringBuilder();
xml.GetXmlSb(sbRequestBody);
Chilkat.StringBuilder sbResponseBody = new Chilkat.StringBuilder();
success = rest.FullRequestSb("PUT","/starfish.jpg?tagging",sbRequestBody,sbResponseBody);
if (success != true) {
    Debug.WriteLine(rest.LastErrorText);
    return;
}

int respStatusCode = rest.ResponseStatusCode;
if (respStatusCode >= 400) {
    Debug.WriteLine("Response Status Code = " + Convert.ToString(respStatusCode));
    Debug.WriteLine("Response Header:");
    Debug.WriteLine(rest.ResponseHeader);
    Debug.WriteLine("Response Body:");
    Debug.WriteLine(sbResponseBody.GetAsString());
    return;
}