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

Visual FoxPro 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

 

 

 

(Visual FoxPro) 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

LOCAL loXmlInit
LOCAL lnSuccess
LOCAL lcUploadId
LOCAL lcFileToUploadPath
LOCAL lnPartSize
LOCAL loFac
LOCAL lnNumParts
LOCAL lcPartsListFile
LOCAL loPartsListXml
LOCAL loRest
LOCAL lnBTls
LOCAL lnPort
LOCAL lnBAutoReconnect
LOCAL loAuthAws
LOCAL lnPartNumber
LOCAL loSbPartNumber
LOCAL lnBPartAlreadyUploaded
LOCAL lnNumUploadedParts
LOCAL loXRec0
LOCAL loFoundRec
LOCAL loFileStream
LOCAL lcResponseStr
LOCAL lcEtag
LOCAL loXPart

* 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.

* For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Xml')
loXmlInit = CreateObject('Chilkat.Xml')
lnSuccess = loXmlInit.LoadXmlFile("s3_multipart_uploads/initiate.xml")
IF (lnSuccess <> 1) THEN
    ? "Did not find the initiate.xml XML file."
    RELEASE loXmlInit
    CANCEL
ENDIF

lcUploadId = loXmlInit.GetChildContent("UploadId")
? "UploadId = " + lcUploadId

* 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
lcFileToUploadPath = "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.
lnPartSize = 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.
* For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.FileAccess')
loFac = CreateObject('Chilkat.FileAccess')
loFac.OpenForRead(lcFileToUploadPath)

* How many parts will there be if each part is 5242880 bytes?
lnNumParts = loFac.GetNumBlocks(lnPartSize)
? "numParts = " + STR(lnNumParts)
loFac.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..
lcPartsListFile = "s3_multipart_uploads/partsList.xml"
* For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Xml')
loPartsListXml = CreateObject('Chilkat.Xml')
IF (loFac.FileExists(lcPartsListFile) = 1) THEN
    loPartsListXml.LoadXmlFile(lcPartsListFile)
ENDIF

* Make sure the top-level tag is "CompleteMultipartUpload"
loPartsListXml.Tag = "CompleteMultipartUpload"

* --------------------------------------
* Before entering the loop to upload parts,
* setup the REST object with AWS authentication,
* and make the initial connection.
* For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Rest')
loRest = CreateObject('Chilkat.Rest')

* Connect to the Amazon AWS REST server.
lnBTls = 1
lnPort = 443
lnBAutoReconnect = 1
lnSuccess = loRest.Connect("s3.amazonaws.com",lnPort,lnBTls,lnBAutoReconnect)

* ----------------------------------------------------------------------------
* 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.
* For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.AuthAws')
loAuthAws = CreateObject('Chilkat.AuthAws')
loAuthAws.AccessKey = "AWS_ACCESS_KEY"
loAuthAws.SecretKey = "AWS_SECRET_KEY"
loAuthAws.ServiceName = "s3"
lnSuccess = loRest.SetAuthAws(loAuthAws)

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

lnPartNumber = 1
* For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.StringBuilder')
loSbPartNumber = CreateObject('Chilkat.StringBuilder')

DO WHILE (lnPartNumber <= lnNumParts)
    ? "---- " + STR(lnPartNumber) + " ----"

    * 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..
    loSbPartNumber.Clear()
    loSbPartNumber.AppendInt(lnPartNumber)

    lnBPartAlreadyUploaded = 0

    * If there are no children, then the XML is empty and no parts have yet been uploaded.
    lnNumUploadedParts = loPartsListXml.NumChildren
    IF (lnNumUploadedParts > 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.
        loXRec0 = loPartsListXml.GetChild(0)
        loFoundRec = loXRec0.FindNextRecord("PartNumber",loSbPartNumber.GetAsString())
        IF (loXRec0.LastMethodSuccess = 1) THEN
            lnBPartAlreadyUploaded = 1
            ? "Part " + STR(lnPartNumber) + " was previously uploaded."
            ? loFoundRec.GetXml()
            RELEASE loFoundRec
        ENDIF

        RELEASE loXRec0
    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 (lnBPartAlreadyUploaded = 0) THEN
        ? "Uploading part " + STR(lnPartNumber) + " ..."

        * Setup the stream source for the large file to be uploaded..
        * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Stream')
        loFileStream = CreateObject('Chilkat.Stream')
        loFileStream.SourceFile = lcFileToUploadPath
        * The Chilkat Stream API has features to make uploading a parts
        * of a file easy.  Indicate the part size by setting the SourceFilePartSize
        * property.
        loFileStream.SourceFilePartSize = lnPartSize

        * 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.
        loRest.ClearAllQueryParams()
        loRest.AddQueryParam("partNumber",loSbPartNumber.GetAsString())
        loRest.AddQueryParam("uploadId",lcUploadId)

        * 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.
        loFileStream.SourceFilePart = lnPartNumber - 1

        * Because the SourceFilePart and SourceFilePartSize properties are set, the stream will 
        * will provide just that part of the file.  
        lcResponseStr = loRest.FullRequestStream("PUT","/somethingBig.zip",loFileStream)
        IF (loRest.LastMethodSuccess <> 1) THEN
            ? loRest.LastErrorText
            RELEASE loXmlInit
            RELEASE loFac
            RELEASE loPartsListXml
            RELEASE loRest
            RELEASE loAuthAws
            RELEASE loSbPartNumber
            RELEASE loFileStream
            CANCEL
        ENDIF

        IF (loRest.ResponseStatusCode <> 200) THEN
            * Examine the request/response to see what happened.
            ? "response status code = " + STR(loRest.ResponseStatusCode)
            ? "response status text = " + loRest.ResponseStatusText
            ? "response header: " + loRest.ResponseHeader
            ? "response body: " + lcResponseStr
            ? "---"
            ? "LastRequestStartLine: " + loRest.LastRequestStartLine
            ? "LastRequestHeader: " + loRest.LastRequestHeader
            RELEASE loXmlInit
            RELEASE loFac
            RELEASE loPartsListXml
            RELEASE loRest
            RELEASE loAuthAws
            RELEASE loSbPartNumber
            RELEASE loFileStream
            CANCEL
        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.
        lcEtag = loRest.ResponseHdrByName("ETag")
        * It should be present, but just in case there was no ETag header...
        IF (loRest.LastMethodSuccess <> 1) THEN
            ? "No ETag response header found!"
            ? "response header: " + loRest.ResponseHeader
            RELEASE loXmlInit
            RELEASE loFac
            RELEASE loPartsListXml
            RELEASE loRest
            RELEASE loAuthAws
            RELEASE loSbPartNumber
            RELEASE loFileStream
            CANCEL
        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>
        loXPart = loPartsListXml.NewChild("Part","")
        loXPart.NewChildInt2("PartNumber",lnPartNumber)
        loXPart.NewChild2("ETag",lcEtag)
        RELEASE loXPart

        lnSuccess = loPartsListXml.SaveXml(lcPartsListFile)
        IF (lnSuccess <> 1) THEN
            ? loPartsListXml.LastErrorText
            RELEASE loXmlInit
            RELEASE loFac
            RELEASE loPartsListXml
            RELEASE loRest
            RELEASE loAuthAws
            RELEASE loSbPartNumber
            RELEASE loFileStream
            CANCEL
        ENDIF

        ? "-- Part " + STR(lnPartNumber) + " uploaded. ---------------------"
    ENDIF

    lnPartNumber = lnPartNumber + 1
ENDDO

? "Finished.  All parts uploaded."

RELEASE loXmlInit
RELEASE loFac
RELEASE loPartsListXml
RELEASE loRest
RELEASE loAuthAws
RELEASE loSbPartNumber
RELEASE loFileStream


 

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