Sample code for 30+ languages & platforms
C Requires Chilkat v11.6.0+

Amazon SNS: Add a Restricted S3 Publisher to an Existing Topic Policy

See more Amazon SNS Examples

Uses the current Amazon SNS Query API (Version=2010-03-31) through Chilkat.Rest and AuthAws, with HTTPS, Signature Version 4, and service sns. The API version date remains current. Action and input parameters are URL-encoded into a POST body, avoiding message text and recipient addresses in the URL. Set the actual region once; other AWS partitions need their documented endpoint suffix and ARN partition.

Load private qa_data/aws/sns.json with nonempty string fields access_key, secret_key, and session_token when using temporary credentials. Prefer short-lived role credentials. This sample does not discover profiles, obtain credentials, or refresh them automatically. The session token is included in the signed form body on every request. A Socket explicitly enables TLS certificate verification before passing the connection to Rest.

Checks setup calls, transport completion, HTTP status, XML parsing, result root, and required fields. Normal output includes identifiers, not full messages or headers. Keep detailed diagnostics private. For retryable failures, use bounded backoff with jitter in the application; these examples do not automatically retry writes. A Publish timeout can leave an accepted message and retrying a standard-topic/SMS publish can duplicate it. A returned MessageId confirms SNS acceptance, not subscriber or handset delivery.

Reads and preserves the existing policy and appends one uniquely named statement, constrained to a particular S3 bucket and source account. Replace both placeholders with the actual bucket owner details. An existing matching Sid causes a stop for review rather than an overwrite. The sample supports Statement arrays and rejects other shapes. SetTopicAttributes replaces the policy as a whole and has no conditional version check; serialize policy writers externally to avoid lost concurrent updates. Configure S3 notifications separately. KMS permissions are separate when the topic is encrypted.

Required IAM actions: sns:GetTopicAttributes, sns:SetTopicAttributes. Related SQS/KMS permissions are separate.

AWS references: GetTopicAttributes SetTopicAttributes

Chilkat C Downloads

C
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
#include <C_CkAuthAws.h>
#include <C_CkRest.h>
#include <C_CkXml.h>
#include <C_CkSocket.h>
#include <C_CkJsonArray.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilder stringCompare;
    HCkJsonObject credentials;
    BOOL stringMatch1;
    BOOL stringMatch2;
    const char *region;
    HCkStringBuilder endpoint;
    HCkAuthAws aws;
    HCkRest rest;
    BOOL stringMatch3;
    HCkXml xml;
    const char *value;
    const char *responseXml;
    const char *topicArn;
    HCkStringBuilder topic;
    HCkStringBuilder arnPrefix;
    HCkSocket connection;
    BOOL stringMatch4;
    BOOL stringMatch5;
    HCkJsonObject attributes;
    int mapIndex;
    BOOL stringMatch6;
    const char *mapKey;
    HCkJsonObject policy;
    HCkJsonArray statements;
    int statementIndex;
    BOOL stringMatch7;
    HCkJsonObject newStatement;
    HCkStringBuilder bucketArn;
    BOOL stringMatch8;
    BOOL stringMatch9;

    success = FALSE;

    stringCompare = CkStringBuilder_Create();
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    //  Private credentials; do not put this file in public web content or source control.
    credentials = CkJsonObject_Create();
    CkJsonObject_putStrict(credentials,TRUE);
    success = CkJsonObject_LoadFile(credentials,"qa_data/aws/sns.json");
    if (success != TRUE) {
        printf("Could not load private SNS credentials.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        return;
    }

    CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(credentials,"access_key"));
    stringMatch1 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
    if ((CkJsonObject_JsonTypeOf(credentials,"access_key") != 1) || (stringMatch1 == TRUE)) {
        printf("Missing or invalid access_key.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        return;
    }

    CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(credentials,"secret_key"));
    stringMatch2 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
    if ((CkJsonObject_JsonTypeOf(credentials,"secret_key") != 1) || (stringMatch2 == TRUE)) {
        printf("Missing or invalid secret_key.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        return;
    }

    region = "us-west-2";
    endpoint = CkStringBuilder_Create();
    CkStringBuilder_Append(endpoint,"sns.");
    CkStringBuilder_Append(endpoint,region);
    CkStringBuilder_Append(endpoint,".amazonaws.com");
    aws = CkAuthAws_Create();
    CkAuthAws_putAccessKey(aws,CkJsonObject_stringOf(credentials,"access_key"));
    CkAuthAws_putSecretKey(aws,CkJsonObject_stringOf(credentials,"secret_key"));
    CkAuthAws_putRegion(aws,region);
    CkAuthAws_putServiceName(aws,"sns");
    CkAuthAws_putSignatureVersion(aws,4);
    rest = CkRest_Create();
    CkRest_putConnectTimeoutMs(rest,20000);
    CkRest_putIdleTimeoutMs(rest,60000);
    CkRest_putVerboseLogging(rest,FALSE);
    success = CkRest_SetAuthAws(rest,aws);
    if (success != TRUE) {
        printf("Could not configure AWS signing.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        return;
    }

    if (CkJsonObject_HasMember(credentials,"session_token") == TRUE) {
        CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(credentials,"session_token"));
        stringMatch3 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
        if ((CkJsonObject_JsonTypeOf(credentials,"session_token") != 1) || (stringMatch3 == TRUE)) {
            printf("session_token must be a nonempty string when supplied.\n");
            CkStringBuilder_Dispose(stringCompare);
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            return;
        }

    }

    xml = CkXml_Create();
    CkXml_putStrict(xml,TRUE);

    topicArn = "arn:aws:sns:us-west-2:123456789012:chilkat-standard-example";
    topic = CkStringBuilder_Create();
    CkStringBuilder_Append(topic,topicArn);
    arnPrefix = CkStringBuilder_Create();
    CkStringBuilder_Append(arnPrefix,"arn:aws:sns:");
    CkStringBuilder_Append(arnPrefix,region);
    CkStringBuilder_Append(arnPrefix,":");
    if (CkStringBuilder_StartsWith(topic,CkStringBuilder_getAsString(arnPrefix),TRUE) != TRUE) {
        printf("Select an SNS topic ARN in the configured region and commercial AWS partition.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        return;
    }

    //  Explicit certificate verification; keep this Socket alive while Rest uses it.
    connection = CkSocket_Create();
    CkSocket_putRequireSslCertVerify(connection,TRUE);
    CkSocket_putMaxReadIdleMs(connection,60000);
    CkSocket_putMaxSendIdleMs(connection,60000);
    success = CkSocket_Connect(connection,CkStringBuilder_getAsString(endpoint),443,TRUE,20000);
    if (success != TRUE) {
        printf("TLS connection failed; inspect diagnostics privately.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        return;
    }

    success = CkRest_UseConnection(rest,connection,TRUE);
    if (success != TRUE) {
        printf("Could not attach TLS connection.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        return;
    }

    success = CkRest_ClearAllQueryParams(rest);
    if (success != TRUE) {
        printf("Could not clear parameters from the previous request.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        return;
    }

    //  Build a fresh form for each request; do not retain parameters from a previous action.
    //  AddQueryParam encodes raw values into the POST body. Do not pre-encode them.
    success = CkRest_AddQueryParam(rest,"Action","GetTopicAttributes");
    if (success != TRUE) {
        printf("Could not set Action.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        return;
    }

    success = CkRest_AddQueryParam(rest,"Version","2010-03-31");
    if (success != TRUE) {
        printf("Could not set Version.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        return;
    }

    //  Include temporary credentials in the signed form body on every request.
    if (CkJsonObject_HasMember(credentials,"session_token") == TRUE) {
        success = CkRest_AddQueryParam(rest,"X-Amz-Security-Token",CkJsonObject_stringOf(credentials,"session_token"));
        if (success != TRUE) {
            printf("Could not set X-Amz-Security-Token.\n");
            CkStringBuilder_Dispose(stringCompare);
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkXml_Dispose(xml);
            CkStringBuilder_Dispose(topic);
            CkStringBuilder_Dispose(arnPrefix);
            CkSocket_Dispose(connection);
            return;
        }

    }

    success = CkRest_AddQueryParam(rest,"TopicArn",topicArn);
    if (success != TRUE) {
        printf("Could not set TopicArn.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        return;
    }

    responseXml = CkRest_fullRequestFormUrlEncoded(rest,"POST","/");
    if (CkRest_getLastMethodSuccess(rest) != TRUE) {
        printf("Request did not complete. Inspect diagnostics privately.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        return;
    }

    success = CkXml_LoadXml(xml,responseXml);
    if (CkRest_getResponseStatusCode(rest) != 200) {
        printf("SNS HTTP status: %d\n",CkRest_getResponseStatusCode(rest));
        if (success == TRUE) {
            value = CkXml_getChildContent(xml,"Error|Code");
            if (CkXml_getLastMethodSuccess(xml) == TRUE) {
                printf("AWS error code: %s\n",value);
            }

            value = CkXml_getChildContent(xml,"RequestId");
            if (CkXml_getLastMethodSuccess(xml) == TRUE) {
                printf("AWS request ID: %s\n",value);
            }

        }

        //  Full messages, headers, and signing diagnostics may contain private data.
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        return;
    }

    if (success != TRUE) {
        printf("Expected a well-formed XML response.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        return;
    }

    CkStringBuilder_SetString(stringCompare,CkXml_tag(xml));
    stringMatch4 = CkStringBuilder_ContentsEqual(stringCompare,"GetTopicAttributesResponse",TRUE);
    if (stringMatch4 != TRUE) {
        printf("Unexpected SNS response element.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        return;
    }

    value = CkXml_getChildContent(xml,"ResponseMetadata|RequestId");
    CkStringBuilder_SetString(stringCompare,value);
    stringMatch5 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
    if ((CkXml_getLastMethodSuccess(xml) != TRUE) || (stringMatch5 == TRUE)) {
        printf("Missing or empty ResponseMetadata|RequestId.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        return;
    }

    printf("AWS request ID: %s\n",value);
    if (CkXml_HasChildWithTag(xml,"GetTopicAttributesResult") != TRUE) {
        printf("Missing result.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        return;
    }

    attributes = CkJsonObject_Create();
    mapIndex = 0;
    while ((mapIndex < CkXml_NumChildrenAt(xml,"GetTopicAttributesResult|Attributes"))) {
        CkXml_putI(xml,mapIndex);
        value = CkXml_getChildContent(xml,"GetTopicAttributesResult|Attributes|entry[i]|key");
        CkStringBuilder_SetString(stringCompare,value);
        stringMatch6 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
        if ((CkXml_getLastMethodSuccess(xml) != TRUE) || (stringMatch6 == TRUE)) {
            printf("Missing or empty GetTopicAttributesResult|Attributes|entry[i]|key.\n");
            CkStringBuilder_Dispose(stringCompare);
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkXml_Dispose(xml);
            CkStringBuilder_Dispose(topic);
            CkStringBuilder_Dispose(arnPrefix);
            CkSocket_Dispose(connection);
            CkJsonObject_Dispose(attributes);
            return;
        }

        mapKey = value;
        value = CkXml_getChildContent(xml,"GetTopicAttributesResult|Attributes|entry[i]|value");
        if (CkXml_getLastMethodSuccess(xml) != TRUE) {
            printf("Missing map value.\n");
            CkStringBuilder_Dispose(stringCompare);
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkXml_Dispose(xml);
            CkStringBuilder_Dispose(topic);
            CkStringBuilder_Dispose(arnPrefix);
            CkSocket_Dispose(connection);
            CkJsonObject_Dispose(attributes);
            return;
        }

        success = CkJsonObject_AppendString(attributes,mapKey,value);
        if (success != TRUE) {
            printf("Invalid or duplicate map entry.\n");
            CkStringBuilder_Dispose(stringCompare);
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkXml_Dispose(xml);
            CkStringBuilder_Dispose(topic);
            CkStringBuilder_Dispose(arnPrefix);
            CkSocket_Dispose(connection);
            CkJsonObject_Dispose(attributes);
            return;
        }

        mapIndex = mapIndex + 1;
    }

    if (CkJsonObject_JsonTypeOf(attributes,"Policy") != 1) {
        printf("Missing policy; refusing to replace it with a new default.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        return;
    }

    policy = CkJsonObject_Create();
    CkJsonObject_putStrict(policy,TRUE);
    success = CkJsonObject_Load(policy,CkJsonObject_stringOf(attributes,"Policy"));
    if (success != TRUE) {
        printf("Existing policy is not valid JSON.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        return;
    }

    if (CkJsonObject_JsonTypeOf(policy,"Statement") != 4) {
        printf("This example requires a Statement array; normalize other policy shapes separately.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        return;
    }

    statements = CkJsonArray_Create();
    success = CkJsonObject_ArrayOf2(policy,"Statement",statements);
    if (success != TRUE) {
        printf("Could not read statements.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        return;
    }

    statementIndex = 0;
    while ((statementIndex < CkJsonArray_getSize(statements))) {
        CkJsonObject_putI(policy,statementIndex);
        if (CkJsonObject_JsonTypeOf(policy,"Statement[i]") != 3) {
            printf("Malformed existing statement.\n");
            CkStringBuilder_Dispose(stringCompare);
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkXml_Dispose(xml);
            CkStringBuilder_Dispose(topic);
            CkStringBuilder_Dispose(arnPrefix);
            CkSocket_Dispose(connection);
            CkJsonObject_Dispose(attributes);
            CkJsonObject_Dispose(policy);
            CkJsonArray_Dispose(statements);
            return;
        }

        CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(policy,"Statement[i].Sid"));
        stringMatch7 = CkStringBuilder_ContentsEqual(stringCompare,"AllowOrderBucketPublish",TRUE);
        if (stringMatch7 == TRUE) {
            printf("The chosen Sid already exists; inspect it before making another change.\n");
            CkStringBuilder_Dispose(stringCompare);
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkXml_Dispose(xml);
            CkStringBuilder_Dispose(topic);
            CkStringBuilder_Dispose(arnPrefix);
            CkSocket_Dispose(connection);
            CkJsonObject_Dispose(attributes);
            CkJsonObject_Dispose(policy);
            CkJsonArray_Dispose(statements);
            return;
        }

        statementIndex = statementIndex + 1;
    }

    newStatement = CkJsonObject_Create();
    CkJsonObject_putStrict(newStatement,TRUE);
    success = CkJsonObject_Load(newStatement,"{\"Sid\":\"AllowOrderBucketPublish\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"s3.amazonaws.com\"},\"Action\":\"sns:Publish\",\"Resource\":\"placeholder\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"placeholder\"},\"StringEquals\":{\"aws:SourceAccount\":\"123456789012\"}}}");
    if (success != TRUE) {
        printf("Could not construct JSON.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        CkJsonObject_Dispose(newStatement);
        return;
    }

    bucketArn = CkStringBuilder_Create();
    CkStringBuilder_Append(bucketArn,"arn:aws:s3:");
    CkStringBuilder_Append(bucketArn,"::example-order-events");
    success = CkJsonObject_UpdateString(newStatement,"Condition.ArnEquals.aws:SourceArn",CkStringBuilder_getAsString(bucketArn));
    if (success != TRUE) {
        printf("Could not set source bucket.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        CkJsonObject_Dispose(newStatement);
        CkStringBuilder_Dispose(bucketArn);
        return;
    }

    success = CkJsonObject_UpdateString(newStatement,"Resource",topicArn);
    if (success != TRUE) {
        printf("Could not set resource.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        CkJsonObject_Dispose(newStatement);
        CkStringBuilder_Dispose(bucketArn);
        return;
    }

    success = CkJsonArray_AddObjectCopyAt(statements,-1,newStatement);
    if (success != TRUE) {
        printf("Could not append policy statement.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        CkJsonObject_Dispose(newStatement);
        CkStringBuilder_Dispose(bucketArn);
        return;
    }

    success = CkRest_ClearAllQueryParams(rest);
    if (success != TRUE) {
        printf("Could not clear parameters from the previous request.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        CkJsonObject_Dispose(newStatement);
        CkStringBuilder_Dispose(bucketArn);
        return;
    }

    //  Build a fresh form for each request; do not retain parameters from a previous action.
    //  AddQueryParam encodes raw values into the POST body. Do not pre-encode them.
    success = CkRest_AddQueryParam(rest,"Action","SetTopicAttributes");
    if (success != TRUE) {
        printf("Could not set Action.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        CkJsonObject_Dispose(newStatement);
        CkStringBuilder_Dispose(bucketArn);
        return;
    }

    success = CkRest_AddQueryParam(rest,"Version","2010-03-31");
    if (success != TRUE) {
        printf("Could not set Version.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        CkJsonObject_Dispose(newStatement);
        CkStringBuilder_Dispose(bucketArn);
        return;
    }

    //  Include temporary credentials in the signed form body on every request.
    if (CkJsonObject_HasMember(credentials,"session_token") == TRUE) {
        success = CkRest_AddQueryParam(rest,"X-Amz-Security-Token",CkJsonObject_stringOf(credentials,"session_token"));
        if (success != TRUE) {
            printf("Could not set X-Amz-Security-Token.\n");
            CkStringBuilder_Dispose(stringCompare);
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkXml_Dispose(xml);
            CkStringBuilder_Dispose(topic);
            CkStringBuilder_Dispose(arnPrefix);
            CkSocket_Dispose(connection);
            CkJsonObject_Dispose(attributes);
            CkJsonObject_Dispose(policy);
            CkJsonArray_Dispose(statements);
            CkJsonObject_Dispose(newStatement);
            CkStringBuilder_Dispose(bucketArn);
            return;
        }

    }

    success = CkRest_AddQueryParam(rest,"TopicArn",topicArn);
    if (success != TRUE) {
        printf("Could not set TopicArn.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        CkJsonObject_Dispose(newStatement);
        CkStringBuilder_Dispose(bucketArn);
        return;
    }

    success = CkRest_AddQueryParam(rest,"AttributeName","Policy");
    if (success != TRUE) {
        printf("Could not set AttributeName.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        CkJsonObject_Dispose(newStatement);
        CkStringBuilder_Dispose(bucketArn);
        return;
    }

    success = CkRest_AddQueryParam(rest,"AttributeValue",CkJsonObject_emit(policy));
    if (success != TRUE) {
        printf("Could not set AttributeValue.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        CkJsonObject_Dispose(newStatement);
        CkStringBuilder_Dispose(bucketArn);
        return;
    }

    responseXml = CkRest_fullRequestFormUrlEncoded(rest,"POST","/");
    if (CkRest_getLastMethodSuccess(rest) != TRUE) {
        printf("Request did not complete. Its outcome may be uncertain; reconcile before retrying.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        CkJsonObject_Dispose(newStatement);
        CkStringBuilder_Dispose(bucketArn);
        return;
    }

    success = CkXml_LoadXml(xml,responseXml);
    if (CkRest_getResponseStatusCode(rest) != 200) {
        printf("SNS HTTP status: %d\n",CkRest_getResponseStatusCode(rest));
        if (success == TRUE) {
            value = CkXml_getChildContent(xml,"Error|Code");
            if (CkXml_getLastMethodSuccess(xml) == TRUE) {
                printf("AWS error code: %s\n",value);
            }

            value = CkXml_getChildContent(xml,"RequestId");
            if (CkXml_getLastMethodSuccess(xml) == TRUE) {
                printf("AWS request ID: %s\n",value);
            }

        }

        //  Full messages, headers, and signing diagnostics may contain private data.
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        CkJsonObject_Dispose(newStatement);
        CkStringBuilder_Dispose(bucketArn);
        return;
    }

    if (success != TRUE) {
        printf("Expected a well-formed XML response.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        CkJsonObject_Dispose(newStatement);
        CkStringBuilder_Dispose(bucketArn);
        return;
    }

    CkStringBuilder_SetString(stringCompare,CkXml_tag(xml));
    stringMatch8 = CkStringBuilder_ContentsEqual(stringCompare,"SetTopicAttributesResponse",TRUE);
    if (stringMatch8 != TRUE) {
        printf("Unexpected SNS response element.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        CkJsonObject_Dispose(newStatement);
        CkStringBuilder_Dispose(bucketArn);
        return;
    }

    value = CkXml_getChildContent(xml,"ResponseMetadata|RequestId");
    CkStringBuilder_SetString(stringCompare,value);
    stringMatch9 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
    if ((CkXml_getLastMethodSuccess(xml) != TRUE) || (stringMatch9 == TRUE)) {
        printf("Missing or empty ResponseMetadata|RequestId.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(topic);
        CkStringBuilder_Dispose(arnPrefix);
        CkSocket_Dispose(connection);
        CkJsonObject_Dispose(attributes);
        CkJsonObject_Dispose(policy);
        CkJsonArray_Dispose(statements);
        CkJsonObject_Dispose(newStatement);
        CkStringBuilder_Dispose(bucketArn);
        return;
    }

    printf("AWS request ID: %s\n",value);
    printf("Example completed.\n");


    CkStringBuilder_Dispose(stringCompare);
    CkJsonObject_Dispose(credentials);
    CkStringBuilder_Dispose(endpoint);
    CkAuthAws_Dispose(aws);
    CkRest_Dispose(rest);
    CkXml_Dispose(xml);
    CkStringBuilder_Dispose(topic);
    CkStringBuilder_Dispose(arnPrefix);
    CkSocket_Dispose(connection);
    CkJsonObject_Dispose(attributes);
    CkJsonObject_Dispose(policy);
    CkJsonArray_Dispose(statements);
    CkJsonObject_Dispose(newStatement);
    CkStringBuilder_Dispose(bucketArn);

    }