Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

PowerBuilder Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(PowerBuilder) S3 Upload the Parts for a Multipart Upload

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

ActiveX for 32-bit and 64-bit Windows

integer li_rc
oleobject loo_XmlInit
integer li_Success
string ls_UploadId
string ls_FileToUploadPath
integer li_PartSize
oleobject loo_Fac
integer li_NumParts
string ls_PartsListFile
oleobject loo_PartsListXml
oleobject loo_Rest
integer li_BTls
integer li_Port
integer li_BAutoReconnect
oleobject loo_AuthAws
integer li_PartNumber
oleobject loo_SbPartNumber
integer li_BPartAlreadyUploaded
integer li_NumUploadedParts
oleobject loo_XRec0
oleobject loo_FoundRec
oleobject loo_FileStream
string ls_ResponseStr
string ls_Etag
oleobject loo_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.

loo_XmlInit = create oleobject
// Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0
li_rc = loo_XmlInit.ConnectToNewObject("Chilkat.Xml")
if li_rc < 0 then
    destroy loo_XmlInit
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_XmlInit.LoadXmlFile("s3_multipart_uploads/initiate.xml")
if li_Success <> 1 then
    Write-Debug "Did not find the initiate.xml XML file."
    destroy loo_XmlInit
    return
end if

ls_UploadId = loo_XmlInit.GetChildContent("UploadId")
Write-Debug "UploadId = " + ls_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
ls_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.
li_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.
loo_Fac = create oleobject
// Use "Chilkat_9_5_0.FileAccess" for versions of Chilkat < 10.0.0
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")

loo_Fac.OpenForRead(ls_FileToUploadPath)

// How many parts will there be if each part is 5242880 bytes?
li_NumParts = loo_Fac.GetNumBlocks(li_PartSize)
Write-Debug "numParts = " + string(li_NumParts)
loo_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..
ls_PartsListFile = "s3_multipart_uploads/partsList.xml"
loo_PartsListXml = create oleobject
// Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0
li_rc = loo_PartsListXml.ConnectToNewObject("Chilkat.Xml")

if loo_Fac.FileExists(ls_PartsListFile) = 1 then
    loo_PartsListXml.LoadXmlFile(ls_PartsListFile)
end if

// Make sure the top-level tag is "CompleteMultipartUpload"
loo_PartsListXml.Tag = "CompleteMultipartUpload"

// --------------------------------------
// Before entering the loop to upload parts,
// setup the REST object with AWS authentication,
// and make the initial connection.
loo_Rest = create oleobject
// Use "Chilkat_9_5_0.Rest" for versions of Chilkat < 10.0.0
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")

// Connect to the Amazon AWS REST server.
li_BTls = 1
li_Port = 443
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("s3.amazonaws.com",li_Port,li_BTls,li_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.
loo_AuthAws = create oleobject
// Use "Chilkat_9_5_0.AuthAws" for versions of Chilkat < 10.0.0
li_rc = loo_AuthAws.ConnectToNewObject("Chilkat.AuthAws")

loo_AuthAws.AccessKey = "AWS_ACCESS_KEY"
loo_AuthAws.SecretKey = "AWS_SECRET_KEY"
loo_AuthAws.ServiceName = "s3"
li_Success = loo_Rest.SetAuthAws(loo_AuthAws)

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

li_PartNumber = 1
loo_SbPartNumber = create oleobject
// Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
li_rc = loo_SbPartNumber.ConnectToNewObject("Chilkat.StringBuilder")

do while (li_PartNumber <= li_NumParts)
    Write-Debug "---- " + string(li_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..
    loo_SbPartNumber.Clear()
    loo_SbPartNumber.AppendInt(li_PartNumber)

    li_BPartAlreadyUploaded = 0

    // If there are no children, then the XML is empty and no parts have yet been uploaded.
    li_NumUploadedParts = loo_PartsListXml.NumChildren
    if li_NumUploadedParts > 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.
        loo_XRec0 = loo_PartsListXml.GetChild(0)
        loo_FoundRec = loo_XRec0.FindNextRecord("PartNumber",loo_SbPartNumber.GetAsString())
        if loo_XRec0.LastMethodSuccess = 1 then
            li_BPartAlreadyUploaded = 1
            Write-Debug "Part " + string(li_PartNumber) + " was previously uploaded."
            Write-Debug loo_FoundRec.GetXml()
            destroy loo_FoundRec
        end if

        destroy loo_XRec0
    end if

    // If this part was not already uploaded, we need to upload.
    // Also update the partsListXml and save as each part is successfully uploaded.
    if li_BPartAlreadyUploaded = 0 then
        Write-Debug "Uploading part " + string(li_PartNumber) + " ..."

        // Setup the stream source for the large file to be uploaded..
        loo_FileStream = create oleobject
        // Use "Chilkat_9_5_0.Stream" for versions of Chilkat < 10.0.0
        li_rc = loo_FileStream.ConnectToNewObject("Chilkat.Stream")

        loo_FileStream.SourceFile = ls_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.
        loo_FileStream.SourceFilePartSize = li_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.
        loo_Rest.ClearAllQueryParams()
        loo_Rest.AddQueryParam("partNumber",loo_SbPartNumber.GetAsString())
        loo_Rest.AddQueryParam("uploadId",ls_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.
        loo_FileStream.SourceFilePart = li_PartNumber - 1

        // Because the SourceFilePart and SourceFilePartSize properties are set, the stream will 
        // will provide just that part of the file.  
        ls_ResponseStr = loo_Rest.FullRequestStream("PUT","/somethingBig.zip",loo_FileStream)
        if loo_Rest.LastMethodSuccess <> 1 then
            Write-Debug loo_Rest.LastErrorText
            destroy loo_XmlInit
            destroy loo_Fac
            destroy loo_PartsListXml
            destroy loo_Rest
            destroy loo_AuthAws
            destroy loo_SbPartNumber
            destroy loo_FileStream
            return
        end if

        if loo_Rest.ResponseStatusCode <> 200 then
            // Examine the request/response to see what happened.
            Write-Debug "response status code = " + string(loo_Rest.ResponseStatusCode)
            Write-Debug "response status text = " + loo_Rest.ResponseStatusText
            Write-Debug "response header: " + loo_Rest.ResponseHeader
            Write-Debug "response body: " + ls_ResponseStr
            Write-Debug "---"
            Write-Debug "LastRequestStartLine: " + loo_Rest.LastRequestStartLine
            Write-Debug "LastRequestHeader: " + loo_Rest.LastRequestHeader
            destroy loo_XmlInit
            destroy loo_Fac
            destroy loo_PartsListXml
            destroy loo_Rest
            destroy loo_AuthAws
            destroy loo_SbPartNumber
            destroy loo_FileStream
            return
        end if

        // OK, this part was uploaded..
        // The response will have a 0-length body.  The only information we need is the 
        // ETag response header field.
        ls_Etag = loo_Rest.ResponseHdrByName("ETag")
        // It should be present, but just in case there was no ETag header...
        if loo_Rest.LastMethodSuccess <> 1 then
            Write-Debug "No ETag response header found!"
            Write-Debug "response header: " + loo_Rest.ResponseHeader
            destroy loo_XmlInit
            destroy loo_Fac
            destroy loo_PartsListXml
            destroy loo_Rest
            destroy loo_AuthAws
            destroy loo_SbPartNumber
            destroy loo_FileStream
            return
        end if

        // 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>
        loo_XPart = loo_PartsListXml.NewChild("Part","")
        loo_XPart.NewChildInt2("PartNumber",li_PartNumber)
        loo_XPart.NewChild2("ETag",ls_Etag)
        destroy loo_XPart

        li_Success = loo_PartsListXml.SaveXml(ls_PartsListFile)
        if li_Success <> 1 then
            Write-Debug loo_PartsListXml.LastErrorText
            destroy loo_XmlInit
            destroy loo_Fac
            destroy loo_PartsListXml
            destroy loo_Rest
            destroy loo_AuthAws
            destroy loo_SbPartNumber
            destroy loo_FileStream
            return
        end if

        Write-Debug "-- Part " + string(li_PartNumber) + " uploaded. ---------------------"
    end if

    li_PartNumber = li_PartNumber + 1
loop

Write-Debug "Finished.  All parts uploaded."


destroy loo_XmlInit
destroy loo_Fac
destroy loo_PartsListXml
destroy loo_Rest
destroy loo_AuthAws
destroy loo_SbPartNumber
destroy loo_FileStream

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.