Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(MFC) 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 <CkXml.h> #include <CkFileAccess.h> #include <CkRest.h> #include <CkAuthAws.h> #include <CkStringBuilder.h> #include <CkStream.h> void ChilkatSample(void) { CkString strOut; // 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. CkXml xmlInit; bool success = xmlInit.LoadXmlFile("s3_multipart_uploads/initiate.xml"); if (success != true) { strOut.append("Did not find the initiate.xml XML file."); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } const char *uploadId = xmlInit.getChildContent("UploadId"); strOut.append("UploadId = "); strOut.append(uploadId); strOut.append("\r\n"); // 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 const char *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. int 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. CkFileAccess fac; fac.OpenForRead(fileToUploadPath); // How many parts will there be if each part is 5242880 bytes? int numParts = fac.GetNumBlocks(partSize); strOut.append("numParts = "); strOut.appendInt(numParts); strOut.append("\r\n"); fac.FileClose(); // 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.. const char *partsListFile = "s3_multipart_uploads/partsList.xml"; CkXml partsListXml; if (fac.FileExists(partsListFile) == true) { partsListXml.LoadXmlFile(partsListFile); } // Make sure the top-level tag is "CompleteMultipartUpload" partsListXml.put_Tag("CompleteMultipartUpload"); // -------------------------------------- // Before entering the loop to upload parts, // setup the REST object with AWS authentication, // and make the initial connection. CkRest rest; // Connect to the Amazon AWS REST server. bool bTls = true; int port = 443; bool bAutoReconnect = true; success = rest.Connect("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. CkAuthAws authAws; authAws.put_AccessKey("AWS_ACCESS_KEY"); authAws.put_SecretKey("AWS_SECRET_KEY"); authAws.put_ServiceName("s3"); success = rest.SetAuthAws(authAws); // Set the bucket name via the HOST header. // In this case, the bucket name is "chilkat100". rest.put_Host("chilkat100.s3.amazonaws.com"); // -------------------------------------- int partNumber = 1; CkStringBuilder sbPartNumber; while ((partNumber <= numParts)) { strOut.append("---- "); strOut.appendInt(partNumber); strOut.append(" ----"); strOut.append("\r\n"); // 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.. sbPartNumber.Clear(); sbPartNumber.AppendInt(partNumber); bool bPartAlreadyUploaded = false; // If there are no children, then the XML is empty and no parts have yet been uploaded. int numUploadedParts = partsListXml.get_NumChildren(); 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. CkXml *xRec0 = partsListXml.GetChild(0); CkXml *foundRec = xRec0->FindNextRecord("PartNumber",sbPartNumber.getAsString()); if (xRec0->get_LastMethodSuccess() == true) { bPartAlreadyUploaded = true; strOut.append("Part "); strOut.appendInt(partNumber); strOut.append(" was previously uploaded."); strOut.append("\r\n"); strOut.append(foundRec->getXml()); strOut.append("\r\n"); delete foundRec; } delete 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) { strOut.append("Uploading part "); strOut.appendInt(partNumber); strOut.append(" ..."); strOut.append("\r\n"); // Setup the stream source for the large file to be uploaded.. CkStream fileStream; fileStream.put_SourceFile(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. fileStream.put_SourceFilePartSize(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. rest.ClearAllQueryParams(); rest.AddQueryParam("partNumber",sbPartNumber.getAsString()); rest.AddQueryParam("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. fileStream.put_SourceFilePart(partNumber - 1); // Because the SourceFilePart and SourceFilePartSize properties are set, the stream will // will provide just that part of the file. const char *responseStr = rest.fullRequestStream("PUT","/somethingBig.zip",fileStream); if (rest.get_LastMethodSuccess() != true) { strOut.append(rest.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } if (rest.get_ResponseStatusCode() != 200) { // Examine the request/response to see what happened. strOut.append("response status code = "); strOut.appendInt(rest.get_ResponseStatusCode()); strOut.append("\r\n"); strOut.append("response status text = "); strOut.append(rest.responseStatusText()); strOut.append("\r\n"); strOut.append("response header: "); strOut.append(rest.responseHeader()); strOut.append("\r\n"); strOut.append("response body: "); strOut.append(responseStr); strOut.append("\r\n"); strOut.append("---"); strOut.append("\r\n"); strOut.append("LastRequestStartLine: "); strOut.append(rest.lastRequestStartLine()); strOut.append("\r\n"); strOut.append("LastRequestHeader: "); strOut.append(rest.lastRequestHeader()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); 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. const char *etag = rest.responseHdrByName("ETag"); // It should be present, but just in case there was no ETag header... if (rest.get_LastMethodSuccess() != true) { strOut.append("No ETag response header found!"); strOut.append("\r\n"); strOut.append("response header: "); strOut.append(rest.responseHeader()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // We need to add record to the partsListXml. // The record will look like this: // <Part> // <PartNumber>PartNumber</PartNumber> // <ETag>ETag</ETag> // </Part> CkXml *xPart = partsListXml.NewChild("Part",""); xPart->NewChildInt2("PartNumber",partNumber); xPart->NewChild2("ETag",etag); delete xPart; success = partsListXml.SaveXml(partsListFile); if (success != true) { strOut.append(partsListXml.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } strOut.append("-- Part "); strOut.appendInt(partNumber); strOut.append(" uploaded. ---------------------"); strOut.append("\r\n"); } partNumber = partNumber + 1; } strOut.append("Finished. All parts uploaded."); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); } |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.