Sample code for 30+ languages & platforms
AutoIt

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 AutoIt Downloads

AutoIt
Local $bSuccess = 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.

$oXmlInit = ObjCreate("Chilkat.Xml")
$bSuccess = $oXmlInit.LoadXmlFile("s3_multipart_uploads/initiate.xml")
If ($bSuccess <> True) Then
    ConsoleWrite("Did not find the initiate.xml XML file." & @CRLF)
    Exit
EndIf

Local $sUploadId = $oXmlInit.GetChildContent("UploadId")
ConsoleWrite("UploadId = " & $sUploadId & @CRLF)

;  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
Local $sFileToUploadPath = "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.
Local $iPartSize = 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.
$oFac = ObjCreate("Chilkat.FileAccess")
$oFac.OpenForRead($sFileToUploadPath)

;  How many parts will there be if each part is 5242880 bytes?
Local $iNumParts = $oFac.GetNumBlocks($iPartSize)
ConsoleWrite("numParts = " & $iNumParts & @CRLF)
$oFac.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..
Local $sPartsListFile = "s3_multipart_uploads/partsList.xml"
$oPartsListXml = ObjCreate("Chilkat.Xml")
If ($oFac.FileExists($sPartsListFile) = True) Then
    $oPartsListXml.LoadXmlFile($sPartsListFile)
EndIf

;  Make sure the top-level tag is "CompleteMultipartUpload"
$oPartsListXml.Tag = "CompleteMultipartUpload"

;  --------------------------------------
;  Before entering the loop to upload parts,
;  setup the REST object with AWS authentication,
;  and make the initial connection.
$oRest = ObjCreate("Chilkat.Rest")

;  Connect to the Amazon AWS REST server.
Local $bTls = True
Local $iPort = 443
Local $bAutoReconnect = True
$bSuccess = $oRest.Connect("s3.amazonaws.com",$iPort,$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.
$oAuthAws = ObjCreate("Chilkat.AuthAws")
$oAuthAws.AccessKey = "AWS_ACCESS_KEY"
$oAuthAws.SecretKey = "AWS_SECRET_KEY"
$oAuthAws.ServiceName = "s3"
$bSuccess = $oRest.SetAuthAws($oAuthAws)

;  Set the bucket name via the HOST header.
;  In this case, the bucket name is "chilkat100".
$oRest.Host = "chilkat100.s3.amazonaws.com"
;  --------------------------------------

Local $iPartNumber = 1
$oSbPartNumber = ObjCreate("Chilkat.StringBuilder")

While ($iPartNumber <= $iNumParts)
    ConsoleWrite("---- " & $iPartNumber & " ----" & @CRLF)

    ;  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..
    $oSbPartNumber.Clear 
    $oSbPartNumber.AppendInt($iPartNumber)

Local $bPartAlreadyUploaded = False

    ;  If there are no children, then the XML is empty and no parts have yet been uploaded.
Local $iNumUploadedParts = $oPartsListXml.NumChildren
    If ($iNumUploadedParts > 0) Then
        ;  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.
Local $oXRec0 = $oPartsListXml.GetChild(0)
Local $oFoundRec = $oXRec0.FindNextRecord("PartNumber",$oSbPartNumber.GetAsString())
        If ($oXRec0.LastMethodSuccess = True) Then
            $bPartAlreadyUploaded = True
            ConsoleWrite("Part " & $iPartNumber & " was previously uploaded." & @CRLF)
            ConsoleWrite($oFoundRec.GetXml() & @CRLF)

        EndIf

    EndIf

    ;  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) Then
        ConsoleWrite("Uploading part " & $iPartNumber & " ..." & @CRLF)

        ;  Setup the stream source for the large file to be uploaded..
        $oFileStream = ObjCreate("Chilkat.Stream")
        $oFileStream.SourceFile = $sFileToUploadPath
        ;  The Chilkat Stream API has features to make uploading a parts
        ;  of a file easy.  Indicate the part size by setting the SourceFilePartSize
        ;  property.
        $oFileStream.SourceFilePartSize = $iPartSize

        ;  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.
        $oRest.ClearAllQueryParams()
        $oRest.AddQueryParam("partNumber",$oSbPartNumber.GetAsString())
        $oRest.AddQueryParam("uploadId",$sUploadId)

        ;  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.
        $oFileStream.SourceFilePart = $iPartNumber - 1

        ;  Because the SourceFilePart and SourceFilePartSize properties are set, the stream will 
        ;  will provide just that part of the file.  
Local $sResponseStr = $oRest.FullRequestStream("PUT","/somethingBig.zip",$oFileStream)
        If ($oRest.LastMethodSuccess <> True) Then
            ConsoleWrite($oRest.LastErrorText & @CRLF)
            Exit
        EndIf

        If ($oRest.ResponseStatusCode <> 200) Then
            ;  Examine the request/response to see what happened.
            ConsoleWrite("response status code = " & $oRest.ResponseStatusCode & @CRLF)
            ConsoleWrite("response status text = " & $oRest.ResponseStatusText & @CRLF)
            ConsoleWrite("response header: " & $oRest.ResponseHeader & @CRLF)
            ConsoleWrite("response body: " & $sResponseStr & @CRLF)
            ConsoleWrite("---" & @CRLF)
            ConsoleWrite("LastRequestStartLine: " & $oRest.LastRequestStartLine & @CRLF)
            ConsoleWrite("LastRequestHeader: " & $oRest.LastRequestHeader & @CRLF)
            Exit
        EndIf

        ;  OK, this part was uploaded..
        ;  The response will have a 0-length body.  The only information we need is the 
        ;  ETag response header field.
Local $sEtag = $oRest.ResponseHdrByName("ETag")
        ;  It should be present, but just in case there was no ETag header...
        If ($oRest.LastMethodSuccess <> True) Then
            ConsoleWrite("No ETag response header found!" & @CRLF)
            ConsoleWrite("response header: " & $oRest.ResponseHeader & @CRLF)
            Exit
        EndIf

        ;  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>
Local $oXPart = $oPartsListXml.NewChild("Part","")
        $oXPart.NewChildInt2 "PartNumber",$iPartNumber
        $oXPart.NewChild2 "ETag",$sEtag

        $bSuccess = $oPartsListXml.SaveXml($sPartsListFile)
        If ($bSuccess <> True) Then
            ConsoleWrite($oPartsListXml.LastErrorText & @CRLF)
            Exit
        EndIf

        ConsoleWrite("-- Part " & $iPartNumber & " uploaded. ---------------------" & @CRLF)
    EndIf

    $iPartNumber = $iPartNumber + 1
Wend

ConsoleWrite("Finished.  All parts uploaded." & @CRLF)