Sample code for 30+ languages & platforms
Unicode C++

S3 Upload Binary File from BinData

See more Amazon S3 (new) Examples

Upload a binary file contained in a BinData object to Amazon S3.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkRestW.h>
#include <CkAuthAwsW.h>
#include <CkBinDataW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkRestW rest;

    // Connect to the Amazon AWS REST server.
    bool bTls = true;
    int port = 443;
    bool bAutoReconnect = true;
    // Make sure to connect to the region where the bucket is located..
    success = rest.Connect(L"s3-us-west-2.amazonaws.com",port,bTls,bAutoReconnect);

    // Provide AWS credentials for the REST call.
    CkAuthAwsW authAws;
    authAws.put_AccessKey(L"AWS_ACCESS_KEY");
    authAws.put_SecretKey(L"AWS_SECRET_KEY");
    // Use the correct region..
    authAws.put_Region(L"us-west-2");
    authAws.put_ServiceName(L"s3");

    success = rest.SetAuthAws(authAws);

    // Set the bucket name via the HOST header.
    // In this case, the bucket name is "chilkat.qa".
    // (Also make sure to use the correct region.)
    rest.put_Host(L"chilkat.qa.s3-us-west-2.amazonaws.com");

    // Load a text file into memory.
    CkBinDataW pngData;
    success = pngData.LoadFile(L"qa_data/png/anemone.png");
    if (success != true) {
        wprintf(L"Failed to load file from local filesystem.\n");
        return;
    }

    // Indicate the Content-Type of our upload.  (This is optional)
    rest.AddHeader(L"Content-Type",L"image/png");

    // Upload the file to Amazon S3.
    CkStringBuilderW sbResponse;
    success = rest.FullRequestBd(L"PUT",L"/images/sea_creatures/anemone.png",pngData,sbResponse);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    // Did we get a 200 response indicating success?
    int statusCode = rest.get_ResponseStatusCode();
    if (statusCode != 200) {
        wprintf(L"Error response: %s\n",sbResponse.getAsString());
        wprintf(L"Status code: %d, Status text: %s\n",statusCode,rest.responseStatusText());
        return;
    }

    wprintf(L"File successfully uploaded.\n");
    }