Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Tcl Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
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
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Tcl) OneDrive -- Upload Large Files with an Upload Session

Demonstrates how to upload large files with an upload session. See OneDrive Upload Session for more general information.

Note: This example requires Chilkat v9.5.0.68 or greater.

Chilkat Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

set http [new_CkHttp]

# Use your previously obtained access token here:
# See the following examples for getting an access token:
#    Get Microsoft Graph OAuth2 Access Token (Azure AD v2.0 Endpoint).
#    Get Microsoft Graph OAuth2 Access Token (Azure AD Endpoint).
#    Refresh Access Token (Azure AD v2.0 Endpoint).
#    Refresh Access Token (Azure AD Endpoint).

# (Make sure your token was obtained with the Files.ReadWrite scope.)
CkHttp_put_AuthToken $http "MICROSOFT_GRAPH_ACCESS_TOKEN"

# ----------------------------------------------------------------------------
# Step 1: Create an upload session

# To begin a large file upload, your app must first request a new upload session. This creates a 
# temporary storage location where the bytes of the file will be saved until the complete file is uploaded. 
# Once the last byte of the file has been uploaded the upload session is completed and the final file is shown 
# in the destination folder.

# Send the following POST to create an upload session:
# POST /v1.0/users/me/drive/root:/{path_to_item}:/createUploadSession

CkHttp_SetUrlVar $http "path_to_item" "/somefolder/big.zip"
set url "https://graph.microsoft.com/v1.0/me/drive/root:/{$path_to_item}:/createUploadSession"
# resp is a CkHttpResponse
set resp [CkHttp_PostJson2 $http $url "application/json" "{}"]
if {[expr [CkHttp_get_LastMethodSuccess $http] != 1]} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    exit
}

# If successful, a 200 status code is returned, with the session details (in JSON format).
set jsonSession [new_CkJsonObject]

CkJsonObject_put_EmitCompact $jsonSession 0
CkJsonObject_Load $jsonSession [CkHttpResponse_bodyStr $resp]

if {[expr [CkHttpResponse_get_StatusCode $resp] != 200]} then {

    puts [CkJsonObject_emit $jsonSession]
    puts "Response status = [CkHttpResponse_get_StatusCode $resp]"
    delete_CkHttpResponse $resp

    delete_CkHttp $http
    delete_CkJsonObject $jsonSession
    exit
}

delete_CkHttpResponse $resp

puts [CkJsonObject_emit $jsonSession]

# A sample response:

# 	{
# 	  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#microsoft.graph.uploadSession",
# 	  "uploadUrl": "https://api.onedrive.com/rup/3a33fceb9b74cc15/eyJSZXNvdXJjZUlEI ... 65yDYUiS3JTDnnhqCHxw",
# 	  "expirationDateTime": "2017-06-11T12:40:23.239Z",
# 	  "nextExpectedRanges": [
# 	    "0-"
# 	  ]
# 	}
# 

# ----------------------------------------------------------------------------
# Step 2: Upload Data in Segments (a.k.a. Fragments or Chunks)

# Microsoft states this requirement: Use a fragment size that is a multiple of 320 KiB (320 * 1024 bytes). 
# Failing to use a fragment size that is a multiple of 320 KiB can result in large file transfers failing after the 
# last fragment is uploaded.  (Note: This is a detail imposed by Microsoft's OneDrive server-side implementation.)

set fragSize [expr 320 * 1024]
set localFilePath "qa_data/zips/big.zip"

# Upload the file big.zip in 320KiB segments.
set fac [new_CkFileAccess]

set fileSize [CkFileAccess_FileSize $fac $localFilePath]

# Open the file to get the number of fragments.
set success [CkFileAccess_OpenForRead $fac $localFilePath]
if {[expr $success == 0]} then {
    puts [CkFileAccess_lastErrorText $fac]
    delete_CkHttp $http
    delete_CkJsonObject $jsonSession
    delete_CkFileAccess $fac
    exit
}

set numFragments [CkFileAccess_GetNumBlocks $fac $fragSize]
CkFileAccess_FileClose $fac

set i 0

puts "fileSize = $fileSize"
puts "numFragments = $numFragments"

set uploadUrl [new_CkUrl]

CkUrl_ParseUrl $uploadUrl [CkJsonObject_stringOf $jsonSession "uploadUrl"]

set json [new_CkJsonObject]

CkJsonObject_put_EmitCompact $json 0

set req [new_CkHttpRequest]

CkHttpRequest_put_HttpVerb $req "PUT"
CkHttpRequest_put_Path $req [CkUrl_pathWithQueryParams $uploadUrl]
CkHttpRequest_put_ContentType $req "application/octet-stream"

set sbOffset [new_CkStringBuilder]

set sbNumBytes [new_CkStringBuilder]

set sbRange [new_CkStringBuilder]

# IMPORTANT: The uploadUrl is a temporary URL to be used to upload the fragment.
# It requires no authentication (because the URL itself could only have been obtained from an authenticated
# request to start the upload session).  Therefore, do not allow the upload URL to be publicly seen,
# otherwise anybody could upload to your OneDrive.
CkHttp_put_AuthToken $http ""

set bytesRemaining $fileSize
while {[expr $i < $numFragments]} {

    # The success response code for intermediate chunks is 202,
    # whereas the final chunk will have a 201 success response where
    # the response body is the JSON DriveItem.
    set chunkSize $fragSize
    set expectedStatusCode 202
    if {[expr $bytesRemaining < $chunkSize]} then {
        set chunkSize $bytesRemaining
        set expectedStatusCode 201
    }

    puts "  this chunkSize = $chunkSize"

    # Indicate the fragment in the local file to be streamed in the upload.
    [CkStringBuilder_Clear $sbOffset]
    [CkStringBuilder_AppendInt $sbOffset [expr $i * $fragSize]]
    [CkStringBuilder_Clear $sbNumBytes]
    [CkStringBuilder_AppendInt $sbNumBytes $chunkSize]
    [CkHttpRequest_StreamChunkFromFile $req $localFilePath [CkStringBuilder_getAsString $sbOffset] [CkStringBuilder_getAsString $sbNumBytes]]

    # The Content-Range header field must be set for this fragment.  For example:
    # Content-Range: bytes 0-25/128
    [CkStringBuilder_SetString $sbRange "bytes start-end/fileSize"]
    set numReplaced [CkStringBuilder_ReplaceI $sbRange "start" [expr $i * $fragSize]]
    set numReplaced [CkStringBuilder_ReplaceI $sbRange "end" [expr [expr $i * $fragSize] + [expr $chunkSize - 1]]]
    set numReplaced [CkStringBuilder_ReplaceI $sbRange "fileSize" $fileSize]
    [CkHttpRequest_AddHeader $req "Content-Range" [CkStringBuilder_getAsString $sbRange]]
    puts "  this content-range: [CkStringBuilder_getAsString $sbRange]"

    set resp [CkHttp_SynchronousRequest $http [CkUrl_host $uploadUrl] 443 1 $req]
    if {[expr [CkHttp_get_LastMethodSuccess $http] != 1]} then {
        puts [CkHttp_lastErrorText $http]
        delete_CkHttp $http
        delete_CkJsonObject $jsonSession
        delete_CkFileAccess $fac
        delete_CkUrl $uploadUrl
        delete_CkJsonObject $json
        delete_CkHttpRequest $req
        delete_CkStringBuilder $sbOffset
        delete_CkStringBuilder $sbNumBytes
        delete_CkStringBuilder $sbRange
        exit
    }

    [CkJsonObject_Load $json [CkHttpResponse_bodyStr $resp]]
    # A 202 response status code indicates success.
    if {[expr [CkHttpResponse_get_StatusCode $resp] != $expectedStatusCode]} then {

        puts [CkJsonObject_emit $json]
        puts "Response status = [CkHttpResponse_get_StatusCode $resp]"
        delete_CkHttpResponse $resp

        delete_CkHttp $http
        delete_CkJsonObject $jsonSession
        delete_CkFileAccess $fac
        delete_CkUrl $uploadUrl
        delete_CkJsonObject $json
        delete_CkHttpRequest $req
        delete_CkStringBuilder $sbOffset
        delete_CkStringBuilder $sbNumBytes
        delete_CkStringBuilder $sbRange
        exit
    }

    delete_CkHttpResponse $resp

    puts [CkJsonObject_emit $json]
    puts "---- Chunk $i uploaded ----"

    set bytesRemaining [expr $bytesRemaining - $chunkSize]
    set i [expr $i + 1]
}

puts "data uploaded."

# ----------------------------------------------------------------------------
# Sample output for the above session:

# {
#   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#microsoft.graph.uploadSession",
#   "uploadUrl": "https://api.onedrive.com/rup/3a33fceb9b74cc15/eyJSZXNvd ... QoKK2iuh1A",
#   "expirationDateTime": "2017-06-11T14:04:45.438Z",
#   "nextExpectedRanges": [
#     "0-"
#   ]
# }
# 
# fileSize = 1366807
# numFragments = 5
#   this chunkSize = 327680
#   this content-range: bytes 0-327679/1366807
# {
#   "expirationDateTime": "2017-06-11T14:04:45.438Z",
#   "nextExpectedRanges": [
#     "327680-1366806"
#   ]
# }
# 
# ---- Chunk 0 uploaded ----
#   this chunkSize = 327680
#   this content-range: bytes 327680-655359/1366807
# {
#   "expirationDateTime": "2017-06-11T14:04:45.438Z",
#   "nextExpectedRanges": [
#     "655360-1366806"
#   ]
# }
# 
# ---- Chunk 1 uploaded ----
#   this chunkSize = 327680
#   this content-range: bytes 655360-983039/1366807
# {
#   "expirationDateTime": "2017-06-11T14:04:45.438Z",
#   "nextExpectedRanges": [
#     "983040-1366806"
#   ]
# }
# 
# ---- Chunk 2 uploaded ----
#   this chunkSize = 327680
#   this content-range: bytes 983040-1310719/1366807
# {
#   "expirationDateTime": "2017-06-11T14:04:45.438Z",
#   "nextExpectedRanges": [
#     "1310720-1366806"
#   ]
# }
# 
# ---- Chunk 3 uploaded ----
#   this chunkSize = 56087
#   this content-range: bytes 1310720-1366806/1366807
# {
#   "createdBy": {
#     "application": {
#       "displayName": "Chilkat",
#       "id": "441c9990"
#     },
#     "user": {
#       "id": "3a33fceb9b74cc15"
#     }
#   },
#   "createdDateTime": "2017-06-04T14:04:47.247Z",
#   "cTag": "aYzozQTMzRkNFQjlCNzRDQzE1ITQ4NjguMjU3",
#   "eTag": "aM0EzM0ZDRUI5Qjc0Q0MxNSE0ODY4LjA",
#   "id": "3A33FCEB9B74CC15!4868",
#   "lastModifiedBy": {
#     "application": {
#       "displayName": "Chilkat",
#       "id": "441c9990"
#     },
#     "user": {
#       "id": "3a33fceb9b74cc15"
#     }
#   },
#   "lastModifiedDateTime": "2017-06-04T14:04:47.247Z",
#   "name": "big.zip",
#   "parentReference": {
#     "driveId": "3a33fceb9b74cc15",
#     "id": "3A33FCEB9B74CC15!4862",
#     "name": "someFolder",
#     "path": "/drive/root:/someFolder"
#   },
#   "size": 1366807,
#   "webUrl": "https://1drv.ms/u/s!ABXMdJvr_DM6pgQ",
#   "file": {
#     "hashes": {
#       "sha1Hash": "252059AA13004220DB912B97D4D3FF9599CCD8D9"
#     },
#     "mimeType": "application/zip"
#   },
#   "fileSystemInfo": {
#     "createdDateTime": "2017-06-04T14:04:47.246Z",
#     "lastModifiedDateTime": "2017-06-04T14:04:47.246Z"
#   },
#   "tags": [
#   ],
#   "lenses": [
#   ]
# }
# 
# Response status = 201

delete_CkHttp $http
delete_CkJsonObject $jsonSession
delete_CkFileAccess $fac
delete_CkUrl $uploadUrl
delete_CkJsonObject $json
delete_CkHttpRequest $req
delete_CkStringBuilder $sbOffset
delete_CkStringBuilder $sbNumBytes
delete_CkStringBuilder $sbRange

 

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