Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) S3 Upload the Parts for a Multipart UploadThis 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.
#include <C_CkXmlW.h> #include <C_CkFileAccessW.h> #include <C_CkRestW.h> #include <C_CkAuthAwsW.h> #include <C_CkStringBuilderW.h> #include <C_CkStreamW.h> void ChilkatSample(void) { HCkXmlW xmlInit; BOOL success; const wchar_t *uploadId; const wchar_t *fileToUploadPath; int partSize; HCkFileAccessW fac; int numParts; const wchar_t *partsListFile; HCkXmlW partsListXml; HCkRestW rest; BOOL bTls; int port; BOOL bAutoReconnect; HCkAuthAwsW authAws; int partNumber; HCkStringBuilderW sbPartNumber; BOOL bPartAlreadyUploaded; int numUploadedParts; HCkXmlW xRec0; HCkXmlW foundRec; HCkStreamW fileStream; const wchar_t *responseStr; const wchar_t *etag; HCkXmlW xPart; // 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 = CkXmlW_Create(); success = CkXmlW_LoadXmlFile(xmlInit,L"s3_multipart_uploads/initiate.xml"); if (success != TRUE) { wprintf(L"Did not find the initiate.xml XML file.\n"); CkXmlW_Dispose(xmlInit); return; } uploadId = CkXmlW_getChildContent(xmlInit,L"UploadId"); wprintf(L"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 = L"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 = CkFileAccessW_Create(); CkFileAccessW_OpenForRead(fac,fileToUploadPath); // How many parts will there be if each part is 5242880 bytes? numParts = CkFileAccessW_GetNumBlocks(fac,partSize); wprintf(L"numParts = %d\n",numParts); CkFileAccessW_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 = L"s3_multipart_uploads/partsList.xml"; partsListXml = CkXmlW_Create(); if (CkFileAccessW_FileExists(fac,partsListFile) == TRUE) { CkXmlW_LoadXmlFile(partsListXml,partsListFile); } // Make sure the top-level tag is "CompleteMultipartUpload" CkXmlW_putTag(partsListXml,L"CompleteMultipartUpload"); // -------------------------------------- // Before entering the loop to upload parts, // setup the REST object with AWS authentication, // and make the initial connection. rest = CkRestW_Create(); // Connect to the Amazon AWS REST server. bTls = TRUE; port = 443; bAutoReconnect = TRUE; success = CkRestW_Connect(rest,L"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 = CkAuthAwsW_Create(); CkAuthAwsW_putAccessKey(authAws,L"AWS_ACCESS_KEY"); CkAuthAwsW_putSecretKey(authAws,L"AWS_SECRET_KEY"); CkAuthAwsW_putServiceName(authAws,L"s3"); success = CkRestW_SetAuthAws(rest,authAws); // Set the bucket name via the HOST header. // In this case, the bucket name is "chilkat100". CkRestW_putHost(rest,L"chilkat100.s3.amazonaws.com"); // -------------------------------------- partNumber = 1; sbPartNumber = CkStringBuilderW_Create(); while ((partNumber <= numParts)) { wprintf(L"---- %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.. CkStringBuilderW_Clear(sbPartNumber); CkStringBuilderW_AppendInt(sbPartNumber,partNumber); bPartAlreadyUploaded = FALSE; // If there are no children, then the XML is empty and no parts have yet been uploaded. numUploadedParts = CkXmlW_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 = CkXmlW_GetChild(partsListXml,0); foundRec = CkXmlW_FindNextRecord(xRec0,L"PartNumber",CkStringBuilderW_getAsString(sbPartNumber)); if (CkXmlW_getLastMethodSuccess(xRec0) == TRUE) { bPartAlreadyUploaded = TRUE; wprintf(L"Part %d was previously uploaded.\n",partNumber); wprintf(L"%s\n",CkXmlW_getXml(foundRec)); CkXmlW_Dispose(foundRec); } CkXmlW_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) { wprintf(L"Uploading part %d ...\n",partNumber); // Setup the stream source for the large file to be uploaded.. fileStream = CkStreamW_Create(); CkStreamW_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. CkStreamW_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. CkRestW_ClearAllQueryParams(rest); CkRestW_AddQueryParam(rest,L"partNumber",CkStringBuilderW_getAsString(sbPartNumber)); CkRestW_AddQueryParam(rest,L"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. CkStreamW_putSourceFilePart(fileStream,partNumber - 1); // Because the SourceFilePart and SourceFilePartSize properties are set, the stream will // will provide just that part of the file. responseStr = CkRestW_fullRequestStream(rest,L"PUT",L"/somethingBig.zip",fileStream); if (CkRestW_getLastMethodSuccess(rest) != TRUE) { wprintf(L"%s\n",CkRestW_lastErrorText(rest)); CkXmlW_Dispose(xmlInit); CkFileAccessW_Dispose(fac); CkXmlW_Dispose(partsListXml); CkRestW_Dispose(rest); CkAuthAwsW_Dispose(authAws); CkStringBuilderW_Dispose(sbPartNumber); CkStreamW_Dispose(fileStream); return; } 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",responseStr); wprintf(L"---\n"); wprintf(L"LastRequestStartLine: %s\n",CkRestW_lastRequestStartLine(rest)); wprintf(L"LastRequestHeader: %s\n",CkRestW_lastRequestHeader(rest)); CkXmlW_Dispose(xmlInit); CkFileAccessW_Dispose(fac); CkXmlW_Dispose(partsListXml); CkRestW_Dispose(rest); CkAuthAwsW_Dispose(authAws); CkStringBuilderW_Dispose(sbPartNumber); CkStreamW_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 = CkRestW_responseHdrByName(rest,L"ETag"); // It should be present, but just in case there was no ETag header... if (CkRestW_getLastMethodSuccess(rest) != TRUE) { wprintf(L"No ETag response header found!\n"); wprintf(L"response header: %s\n",CkRestW_responseHeader(rest)); CkXmlW_Dispose(xmlInit); CkFileAccessW_Dispose(fac); CkXmlW_Dispose(partsListXml); CkRestW_Dispose(rest); CkAuthAwsW_Dispose(authAws); CkStringBuilderW_Dispose(sbPartNumber); CkStreamW_Dispose(fileStream); return; } // We need to add record to the partsListXml. // The record will look like this: // <Part> // <PartNumber>PartNumber</PartNumber> // <ETag>ETag</ETag> // </Part> xPart = CkXmlW_NewChild(partsListXml,L"Part",L""); CkXmlW_NewChildInt2(xPart,L"PartNumber",partNumber); CkXmlW_NewChild2(xPart,L"ETag",etag); CkXmlW_Dispose(xPart); success = CkXmlW_SaveXml(partsListXml,partsListFile); if (success != TRUE) { wprintf(L"%s\n",CkXmlW_lastErrorText(partsListXml)); CkXmlW_Dispose(xmlInit); CkFileAccessW_Dispose(fac); CkXmlW_Dispose(partsListXml); CkRestW_Dispose(rest); CkAuthAwsW_Dispose(authAws); CkStringBuilderW_Dispose(sbPartNumber); CkStreamW_Dispose(fileStream); return; } wprintf(L"-- Part %d uploaded. ---------------------\n",partNumber); } partNumber = partNumber + 1; } wprintf(L"Finished. All parts uploaded.\n"); CkXmlW_Dispose(xmlInit); CkFileAccessW_Dispose(fac); CkXmlW_Dispose(partsListXml); CkRestW_Dispose(rest); CkAuthAwsW_Dispose(authAws); CkStringBuilderW_Dispose(sbPartNumber); CkStreamW_Dispose(fileStream); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.