Sample code for 30+ languages & platforms
Unicode 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 Unicode C Downloads

Unicode C
#include <C_CkRestW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkXmlW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRestW rest;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkAuthAwsW authAws;
    HCkXmlW xml;
    HCkStringBuilderW sbRequestBody;
    HCkStringBuilderW sbResponse;

    success = FALSE;

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

    rest = CkRestW_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 = CkRestW_Connect(rest,L"s3.amazonaws.com",port,bTls,bAutoReconnect);

    // Provide AWS credentials.
    authAws = CkAuthAwsW_Create();
    CkAuthAwsW_putAccessKey(authAws,L"AWS_ACCESS_KEY");
    CkAuthAwsW_putSecretKey(authAws,L"AWS_SECRET_KEY");
    CkAuthAwsW_putServiceName(authAws,L"s3");
    CkAuthAwsW_putRegion(authAws,L"us-east-1");

    CkRestW_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.
    CkRestW_putHost(rest,L"chilkat100.s3.amazonaws.com");

    xml = CkXmlW_Create();
    CkXmlW_putTag(xml,L"Tagging");
    CkXmlW_UpdateChildContent(xml,L"TagSet|Tag|Key",L"plant");
    CkXmlW_UpdateChildContent(xml,L"TagSet|Tag|Value",L"chili pepper");

    sbRequestBody = CkStringBuilderW_Create();
    CkXmlW_GetXmlSb(xml,sbRequestBody);

    // It is important to add the terminating "=" after the "?tagging".
    sbResponse = CkStringBuilderW_Create();
    success = CkRestW_FullRequestSb(rest,L"PUT",L"/chiliPepper.gif?tagging=",sbRequestBody,sbResponse);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkAuthAwsW_Dispose(authAws);
        CkXmlW_Dispose(xml);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkStringBuilderW_Dispose(sbResponse);
        return;
    }

    wprintf(L"Response status code: %d\n",CkRestW_getResponseStatusCode(rest));

    // When successful, the S3 Storage service will respond with a 200 response code,
    // with an XML body.  
    if (CkRestW_getResponseStatusCode(rest) != 200) {
        // Examine the request/response to see what happened.
        wprintf(L"response status code = %d\n",CkRestW_getResponseStatusCode(rest));
        wprintf(L"response status text = %s\n",CkRestW_responseStatusText(rest));
        wprintf(L"response header: %s\n",CkRestW_responseHeader(rest));
        wprintf(L"response body: %s\n",CkStringBuilderW_getAsString(sbResponse));
        wprintf(L"---\n");
        wprintf(L"LastRequestStartLine: %s\n",CkRestW_lastRequestStartLine(rest));
        wprintf(L"LastRequestHeader: %s\n",CkRestW_lastRequestHeader(rest));
    }

    wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponse));
    wprintf(L"Success.\n");


    CkRestW_Dispose(rest);
    CkAuthAwsW_Dispose(authAws);
    CkXmlW_Dispose(xml);
    CkStringBuilderW_Dispose(sbRequestBody);
    CkStringBuilderW_Dispose(sbResponse);

    }