Sample code for 30+ languages & platforms
C

S3 Upload the Parts for a Multipart Upload

See more Amazon S3 (new) Examples

This example uploads a large file in parts. The multipart upload needs to have been first initiated prior to uploading the parts.

See http://docs.aws.amazon.com/AmazonS3/latest/API/mpUploadUploadPart.html for more information about uploading parts.

Chilkat C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkXml xmlInit;
    const char *uploadId;
    const char *fileToUploadPath;
    int partSize;
    HCkFileAccess fac;
    int numParts;
    const char *partsListFile;
    HCkXml partsListXml;
    HCkRest rest;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkAuthAws authAws;
    int partNumber;
    HCkStringBuilder sbPartNumber;
    BOOL bPartAlreadyUploaded;
    int numUploadedParts;
    HCkXml xRec0;
    HCkXml foundRec;
    HCkStream fileStream;
    const char *responseStr;
    const char *etag;
    HCkXml xPart;

    success = FALSE;

    // In the 1st step for uploading a large file, the multipart upload was initiated
    // as shown here: Initiate Multipart Upload

    // Other S3 Multipart Upload Examples:
    // Complete Multipart Upload
    // Abort Multipart Upload
    // List Parts

    // When we initiated the multipart upload, we saved the XML response to a file.  This
    // XML response contains the UploadId.  We'll begin by loading that XML and getting
    // the Upload ID.

    xmlInit = CkXml_Create();
    success = CkXml_LoadXmlFile(xmlInit,"s3_multipart_uploads/initiate.xml");
    if (success != TRUE) {
        printf("Did not find the initiate.xml XML file.\n");
        CkXml_Dispose(xmlInit);
        return;
    }

    uploadId = CkXml_getChildContent(xmlInit,"UploadId");
    printf("UploadId = %s\n",uploadId);

    // When uploading parts, we need to keep an XML record of each part number
    // and its corresponding ETag, which is received in the response for each part.
    // There can be up to 10000 parts, numbered 1 to 10000.  
    // After all parts have been uploaded, the final step will be to complete
    // the multipart upload (see Complete Multipart Upload)

    // In this example, the large file we want to upload is somethingBig.zip
    fileToUploadPath = "s3_multipart_uploads/somethingBig.zip";

    // The minimum allowed part size is 5MB (5242880 bytes).  The last part can be smaller because
    // it will contain the remainder of the file.  (This minimum is enforced by the AWS service.)
    // We'll use the minimum allowed part size for this example.
    partSize = 5242880;

    // Let's use Chilkat's FileAccess API to examine the file to be uploaded.  We'll get the size
    // of the file and find out how many parts will be needed, including the final "partial" part.
    fac = CkFileAccess_Create();
    CkFileAccess_OpenForRead(fac,fileToUploadPath);

    // How many parts will there be if each part is 5242880 bytes?
    numParts = CkFileAccess_GetNumBlocks(fac,partSize);
    printf("numParts = %d\n",numParts);
    CkFileAccess_FileClose(fac);

    // Imagine that we may be running this for the 1st time, or maybe we already
    // attempted to upload parts, and something failed. Maybe there was a network problem
    // the resulted in not all parts getting uploaded.  We'll write this code so that if run again,
    // it will upload whatever parts haven't yet been uploaded.

    // We'll keep a partsList.xml file to record the parts that have already been successfully
    // uploaded.  If this file does not yet exist, we'll create it..
    partsListFile = "s3_multipart_uploads/partsList.xml";
    partsListXml = CkXml_Create();
    if (CkFileAccess_FileExists(fac,partsListFile) == TRUE) {
        CkXml_LoadXmlFile(partsListXml,partsListFile);
    }

    // Make sure the top-level tag is "CompleteMultipartUpload"
    CkXml_putTag(partsListXml,"CompleteMultipartUpload");

    // --------------------------------------
    // Before entering the loop to upload parts,
    // setup the REST object with AWS authentication,
    // and make the initial connection.
    rest = CkRest_Create();

    // Connect to the Amazon AWS REST server.
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRest_Connect(rest,"s3.amazonaws.com",port,bTls,bAutoReconnect);

    // ----------------------------------------------------------------------------
    // Important: For buckets created in regions outside us-east-1,
    // there are three important changes that need to be made.
    // See Working with S3 Buckets in Non-us-east-1 Regions for the details.
    // ----------------------------------------------------------------------------

    // Provide AWS credentials for the REST call.
    authAws = CkAuthAws_Create();
    CkAuthAws_putAccessKey(authAws,"AWS_ACCESS_KEY");
    CkAuthAws_putSecretKey(authAws,"AWS_SECRET_KEY");
    CkAuthAws_putServiceName(authAws,"s3");
    success = CkRest_SetAuthAws(rest,authAws);

    // Set the bucket name via the HOST header.
    // In this case, the bucket name is "chilkat100".
    CkRest_putHost(rest,"chilkat100.s3.amazonaws.com");
    // --------------------------------------

    partNumber = 1;
    sbPartNumber = CkStringBuilder_Create();

    while ((partNumber <= numParts)) {
        printf("---- %d ----\n",partNumber);

        // This cumbersome way of converting an integer to a string is because
        // Chilkat examples are written in a script that is converted to many programming languages.
        // At this time, the translator does not have integer-to-string code generation capability..
        CkStringBuilder_Clear(sbPartNumber);
        CkStringBuilder_AppendInt(sbPartNumber,partNumber);

        bPartAlreadyUploaded = FALSE;

        // If there are no children, then the XML is empty and no parts have yet been uploaded.
        numUploadedParts = CkXml_getNumChildren(partsListXml);
        if (numUploadedParts > 0) {
            // If some parts have been uploaded, check to see if this particular part was already upload.
            // If so, then it can be skipped.

            // Position ourselves at the 1st record.
            xRec0 = CkXml_GetChild(partsListXml,0);
            foundRec = CkXml_FindNextRecord(xRec0,"PartNumber",CkStringBuilder_getAsString(sbPartNumber));
            if (CkXml_getLastMethodSuccess(xRec0) == TRUE) {
                bPartAlreadyUploaded = TRUE;
                printf("Part %d was previously uploaded.\n",partNumber);
                printf("%s\n",CkXml_getXml(foundRec));
                CkXml_Dispose(foundRec);
            }

            CkXml_Dispose(xRec0);
        }

        // If this part was not already uploaded, we need to upload.
        // Also update the partsListXml and save as each part is successfully uploaded.
        if (bPartAlreadyUploaded == FALSE) {
            printf("Uploading part %d ...\n",partNumber);

            // Setup the stream source for the large file to be uploaded..
            fileStream = CkStream_Create();
            CkStream_putSourceFile(fileStream,fileToUploadPath);
            // The Chilkat Stream API has features to make uploading a parts
            // of a file easy.  Indicate the part size by setting the SourceFilePartSize
            // property.
            CkStream_putSourceFilePartSize(fileStream,partSize);

            // Our HTTP start line to upload a part will look like this:
            // PUT /ObjectName?partNumber=PartNumber&uploadId=UploadId HTTP/1.1

            // Set the query params.  We'll need partNumber and uploadId.
            // Make sure the query params from previous iterations are clear.
            CkRest_ClearAllQueryParams(rest);
            CkRest_AddQueryParam(rest,"partNumber",CkStringBuilder_getAsString(sbPartNumber));
            CkRest_AddQueryParam(rest,"uploadId",uploadId);

            // Upload this particular file part.
            // Tell the fileStream which part is being uploaded.
            // Our partNumber is 1-based (the 1st part is at index 1), but the fileStream's SourceFilePart
            // property is 0-based.  Therefore we use partNumber-1.
            CkStream_putSourceFilePart(fileStream,partNumber - 1);

            // Because the SourceFilePart and SourceFilePartSize properties are set, the stream will 
            // will provide just that part of the file.  
            responseStr = CkRest_fullRequestStream(rest,"PUT","/somethingBig.zip",fileStream);
            if (CkRest_getLastMethodSuccess(rest) != TRUE) {
                printf("%s\n",CkRest_lastErrorText(rest));
                CkXml_Dispose(xmlInit);
                CkFileAccess_Dispose(fac);
                CkXml_Dispose(partsListXml);
                CkRest_Dispose(rest);
                CkAuthAws_Dispose(authAws);
                CkStringBuilder_Dispose(sbPartNumber);
                CkStream_Dispose(fileStream);
                return;
            }

            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",responseStr);
                printf("---\n");
                printf("LastRequestStartLine: %s\n",CkRest_lastRequestStartLine(rest));
                printf("LastRequestHeader: %s\n",CkRest_lastRequestHeader(rest));
                CkXml_Dispose(xmlInit);
                CkFileAccess_Dispose(fac);
                CkXml_Dispose(partsListXml);
                CkRest_Dispose(rest);
                CkAuthAws_Dispose(authAws);
                CkStringBuilder_Dispose(sbPartNumber);
                CkStream_Dispose(fileStream);
                return;
            }

            // OK, this part was uploaded..
            // The response will have a 0-length body.  The only information we need is the 
            // ETag response header field.
            etag = CkRest_responseHdrByName(rest,"ETag");
            // It should be present, but just in case there was no ETag header...
            if (CkRest_getLastMethodSuccess(rest) != TRUE) {
                printf("No ETag response header found!\n");
                printf("response header: %s\n",CkRest_responseHeader(rest));
                CkXml_Dispose(xmlInit);
                CkFileAccess_Dispose(fac);
                CkXml_Dispose(partsListXml);
                CkRest_Dispose(rest);
                CkAuthAws_Dispose(authAws);
                CkStringBuilder_Dispose(sbPartNumber);
                CkStream_Dispose(fileStream);
                return;
            }

            // We need to add record to the partsListXml.
            // The record will look like this:
            // &lt;Part>
            //   &lt;PartNumber>PartNumber&lt;/PartNumber>
            //   &lt;ETag>ETag&lt;/ETag>
            // &lt;/Part>
            xPart = CkXml_NewChild(partsListXml,"Part","");
            CkXml_NewChildInt2(xPart,"PartNumber",partNumber);
            CkXml_NewChild2(xPart,"ETag",etag);
            CkXml_Dispose(xPart);

            success = CkXml_SaveXml(partsListXml,partsListFile);
            if (success != TRUE) {
                printf("%s\n",CkXml_lastErrorText(partsListXml));
                CkXml_Dispose(xmlInit);
                CkFileAccess_Dispose(fac);
                CkXml_Dispose(partsListXml);
                CkRest_Dispose(rest);
                CkAuthAws_Dispose(authAws);
                CkStringBuilder_Dispose(sbPartNumber);
                CkStream_Dispose(fileStream);
                return;
            }

            printf("-- Part %d uploaded. ---------------------\n",partNumber);
        }

        partNumber = partNumber + 1;
    }

    printf("Finished.  All parts uploaded.\n");


    CkXml_Dispose(xmlInit);
    CkFileAccess_Dispose(fac);
    CkXml_Dispose(partsListXml);
    CkRest_Dispose(rest);
    CkAuthAws_Dispose(authAws);
    CkStringBuilder_Dispose(sbPartNumber);
    CkStream_Dispose(fileStream);

    }