Sample code for 30+ languages & platforms
C

S3 Add Tags to an Object

See more Amazon S3 (new) Examples

Demonstrates how to add one or more tags to an S3 object.

Chilkat C Downloads

C
#include <C_CkRest.h>
#include <C_CkAuthAws.h>
#include <C_CkXml.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRest rest;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkAuthAws authAws;
    HCkXml xml;
    HCkStringBuilder sbRequestBody;
    HCkStringBuilder sbResponse;

    success = FALSE;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    rest = CkRest_Create();

    // Connect to the Amazon AWS REST server in the desired region.
    // (for us-east-1, we use "s3.amazonaws.com", but for another region, such as us-west-2, we would use "s3-us-west-2.amazonaws.com")
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRest_Connect(rest,"s3.amazonaws.com",port,bTls,bAutoReconnect);

    // Provide AWS credentials.
    authAws = CkAuthAws_Create();
    CkAuthAws_putAccessKey(authAws,"AWS_ACCESS_KEY");
    CkAuthAws_putSecretKey(authAws,"AWS_SECRET_KEY");
    CkAuthAws_putServiceName(authAws,"s3");
    CkAuthAws_putRegion(authAws,"us-east-1");

    CkRest_SetAuthAws(rest,authAws);

    // Set the bucket name via the HOST header.
    // In this case, the bucket name is "chilkat100".
    // Note that the Host header should use "bucketName.s3.amazonaws.com", not "bucketName.s3-us-east-1.amazonaws.com"
    // The same applies to aother regions.  The Host header should simply be <bucketName>.s3.amazonaws.com regardless of the region.
    CkRest_putHost(rest,"chilkat100.s3.amazonaws.com");

    xml = CkXml_Create();
    CkXml_putTag(xml,"Tagging");
    CkXml_UpdateChildContent(xml,"TagSet|Tag|Key","plant");
    CkXml_UpdateChildContent(xml,"TagSet|Tag|Value","chili pepper");

    sbRequestBody = CkStringBuilder_Create();
    CkXml_GetXmlSb(xml,sbRequestBody);

    // It is important to add the terminating "=" after the "?tagging".
    sbResponse = CkStringBuilder_Create();
    success = CkRest_FullRequestSb(rest,"PUT","/chiliPepper.gif?tagging=",sbRequestBody,sbResponse);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkAuthAws_Dispose(authAws);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(sbRequestBody);
        CkStringBuilder_Dispose(sbResponse);
        return;
    }

    printf("Response status code: %d\n",CkRest_getResponseStatusCode(rest));

    // When successful, the S3 Storage service will respond with a 200 response code,
    // with an XML body.  
    if (CkRest_getResponseStatusCode(rest) != 200) {
        // Examine the request/response to see what happened.
        printf("response status code = %d\n",CkRest_getResponseStatusCode(rest));
        printf("response status text = %s\n",CkRest_responseStatusText(rest));
        printf("response header: %s\n",CkRest_responseHeader(rest));
        printf("response body: %s\n",CkStringBuilder_getAsString(sbResponse));
        printf("---\n");
        printf("LastRequestStartLine: %s\n",CkRest_lastRequestStartLine(rest));
        printf("LastRequestHeader: %s\n",CkRest_lastRequestHeader(rest));
    }

    printf("%s\n",CkStringBuilder_getAsString(sbResponse));
    printf("Success.\n");


    CkRest_Dispose(rest);
    CkAuthAws_Dispose(authAws);
    CkXml_Dispose(xml);
    CkStringBuilder_Dispose(sbRequestBody);
    CkStringBuilder_Dispose(sbResponse);

    }