Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) OneDrive -- Upload Large Files with an Upload SessionSee more OneDrive ExamplesDemonstrates 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.97 or greater.
IncludeFile "CkJsonObject.pb" IncludeFile "CkHttp.pb" IncludeFile "CkFileAccess.pb" IncludeFile "CkUrl.pb" IncludeFile "CkHttpRequest.pb" IncludeFile "CkHttpResponse.pb" IncludeFile "CkStringBuilder.pb" Procedure ChilkatExample() ; This example requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. success.i ; This example uses the OAuth client credentials flow. ; See How to Create an Azure App Registration for OAuth 2.0 Client Credentials ; Use your client ID, client secret, and tenant ID in the following lines json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckUpdateString(json,"client_id","2871da2c-8176-4b7f-869b-2311aa82e743") CkJsonObject::ckUpdateString(json,"client_secret","2hu9Q~r5QuryUcEkNbg1btLtnfU1VUXzhSCG6brH") CkJsonObject::ckUpdateString(json,"scope","https://graph.microsoft.com/.default") CkJsonObject::ckUpdateString(json,"token_endpoint","https://login.microsoftonline.com/114d7ed6-71bf-4dbe-a866-748364121bf2/oauth2/v2.0/token") http.i = CkHttp::ckCreate() If http.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttp::setCkAuthToken(http, CkJsonObject::ckEmit(json)) ; ---------------------------------------------------------------------------- ; 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: ; If not using "me", then the path should be /v1.0/users/{id | userPrincipalName}/... ; POST /v1.0/users/{user_id}/drive/root:/{path_to_item}:/createUploadSession CkHttp::ckSetUrlVar(http,"path_to_item","/somefolder/big.zip") CkHttp::ckSetUrlVar(http,"user_id","4fe732c3-322e-4a6b-b729-2fd1eb5c6104") url.s = "https://graph.microsoft.com/v1.0/users/{$user_id}/drive/root:/{$path_to_item}:/createUploadSession" resp.i = CkHttp::ckPostJson2(http,url,"application/json","{}") If CkHttp::ckLastMethodSuccess(http) <> 1 Debug CkHttp::ckLastErrorText(http) CkJsonObject::ckDispose(json) CkHttp::ckDispose(http) ProcedureReturn EndIf ; If successful, a 200 status code is returned, with the session details (in JSON format). jsonSession.i = CkJsonObject::ckCreate() If jsonSession.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::setCkEmitCompact(jsonSession, 0) CkJsonObject::ckLoad(jsonSession,CkHttpResponse::ckBodyStr(resp)) If CkHttpResponse::ckStatusCode(resp) <> 200 Debug CkJsonObject::ckEmit(jsonSession) Debug "Response status = " + Str(CkHttpResponse::ckStatusCode(resp)) CkHttpResponse::ckDispose(resp) CkJsonObject::ckDispose(json) CkHttp::ckDispose(http) CkJsonObject::ckDispose(jsonSession) ProcedureReturn EndIf CkHttpResponse::ckDispose(resp) Debug CkJsonObject::ckEmit(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.) fragSize.i = 320 * 1024 localFilePath.s = "qa_data/zips/big.zip" ; Upload the file big.zip in 320KiB segments. ; Note: The FileSize method returns a signed 32-bit integer. If the file is potentially larger than 2GB, call FileSizeStr instead to return ; the size of the file as a string, then convert to an integer value. fac.i = CkFileAccess::ckCreate() If fac.i = 0 Debug "Failed to create object." ProcedureReturn EndIf fileSize.i = CkFileAccess::ckFileSize(fac,localFilePath) ; Open the file to get the number of fragments. success = CkFileAccess::ckOpenForRead(fac,localFilePath) If success = 0 Debug CkFileAccess::ckLastErrorText(fac) CkJsonObject::ckDispose(json) CkHttp::ckDispose(http) CkJsonObject::ckDispose(jsonSession) CkFileAccess::ckDispose(fac) ProcedureReturn EndIf numFragments.i = CkFileAccess::ckGetNumBlocks(fac,fragSize) CkFileAccess::ckFileClose(fac) i.i = 0 Debug "fileSize = " + Str(fileSize) Debug "numFragments = " + Str(numFragments) uploadUrl.i = CkUrl::ckCreate() If uploadUrl.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkUrl::ckParseUrl(uploadUrl,CkJsonObject::ckStringOf(jsonSession,"uploadUrl")) CkJsonObject::setCkEmitCompact(json, 0) req.i = CkHttpRequest::ckCreate() If req.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttpRequest::setCkHttpVerb(req, "PUT") CkHttpRequest::setCkPath(req, CkUrl::ckPathWithQueryParams(uploadUrl)) CkHttpRequest::setCkContentType(req, "application/octet-stream") sbOffset.i = CkStringBuilder::ckCreate() If sbOffset.i = 0 Debug "Failed to create object." ProcedureReturn EndIf sbNumBytes.i = CkStringBuilder::ckCreate() If sbNumBytes.i = 0 Debug "Failed to create object." ProcedureReturn EndIf sbRange.i = CkStringBuilder::ckCreate() If sbRange.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; 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::setCkAuthToken(http, "") bytesRemaining.i = fileSize While 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. chunkSize.i = fragSize expectedStatusCode.i = 202 If bytesRemaining < chunkSize chunkSize = bytesRemaining expectedStatusCode = 201 EndIf Debug " this chunkSize = " + Str(chunkSize) ; Indicate the fragment in the local file to be streamed in the upload. CkStringBuilder::ckClear(sbOffset) CkStringBuilder::ckAppendInt(sbOffset,i * fragSize) CkStringBuilder::ckClear(sbNumBytes) CkStringBuilder::ckAppendInt(sbNumBytes,chunkSize) CkHttpRequest::ckStreamChunkFromFile(req,localFilePath,CkStringBuilder::ckGetAsString(sbOffset),CkStringBuilder::ckGetAsString(sbNumBytes)) ; The Content-Range header field must be set for this fragment. For example: ; Content-Range: bytes 0-25/128 CkStringBuilder::ckSetString(sbRange,"bytes start-end/fileSize") numReplaced.i = CkStringBuilder::ckReplaceI(sbRange,"start",i * fragSize) numReplaced = CkStringBuilder::ckReplaceI(sbRange,"end",i * fragSize + chunkSize - 1) numReplaced = CkStringBuilder::ckReplaceI(sbRange,"fileSize",fileSize) CkHttpRequest::ckAddHeader(req,"Content-Range",CkStringBuilder::ckGetAsString(sbRange)) Debug " this content-range: " + CkStringBuilder::ckGetAsString(sbRange) resp = CkHttp::ckSynchronousRequest(http,CkUrl::ckHost(uploadUrl),443,1,req) If CkHttp::ckLastMethodSuccess(http) <> 1 Debug CkHttp::ckLastErrorText(http) CkJsonObject::ckDispose(json) CkHttp::ckDispose(http) CkJsonObject::ckDispose(jsonSession) CkFileAccess::ckDispose(fac) CkUrl::ckDispose(uploadUrl) CkHttpRequest::ckDispose(req) CkStringBuilder::ckDispose(sbOffset) CkStringBuilder::ckDispose(sbNumBytes) CkStringBuilder::ckDispose(sbRange) ProcedureReturn EndIf CkJsonObject::ckLoad(json,CkHttpResponse::ckBodyStr(resp)) ; A 202 response status code indicates success. If CkHttpResponse::ckStatusCode(resp) <> expectedStatusCode Debug CkJsonObject::ckEmit(json) Debug "Response status = " + Str(CkHttpResponse::ckStatusCode(resp)) CkHttpResponse::ckDispose(resp) CkJsonObject::ckDispose(json) CkHttp::ckDispose(http) CkJsonObject::ckDispose(jsonSession) CkFileAccess::ckDispose(fac) CkUrl::ckDispose(uploadUrl) CkHttpRequest::ckDispose(req) CkStringBuilder::ckDispose(sbOffset) CkStringBuilder::ckDispose(sbNumBytes) CkStringBuilder::ckDispose(sbRange) ProcedureReturn EndIf CkHttpResponse::ckDispose(resp) Debug CkJsonObject::ckEmit(json) Debug "---- Chunk " + Str(i) + " uploaded ----" bytesRemaining = bytesRemaining - chunkSize i = i + 1 Wend Debug "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 CkJsonObject::ckDispose(json) CkHttp::ckDispose(http) CkJsonObject::ckDispose(jsonSession) CkFileAccess::ckDispose(fac) CkUrl::ckDispose(uploadUrl) CkHttpRequest::ckDispose(req) CkStringBuilder::ckDispose(sbOffset) CkStringBuilder::ckDispose(sbNumBytes) CkStringBuilder::ckDispose(sbRange) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.