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
(PowerBuilder) 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.
integer li_rc integer li_Success oleobject loo_Json oleobject loo_Http string ls_Url oleobject loo_Resp oleobject loo_JsonSession integer li_FragSize string ls_LocalFilePath oleobject loo_Fac integer li_FileSize integer li_NumFragments integer i oleobject loo_UploadUrl oleobject loo_Req oleobject loo_SbOffset oleobject loo_SbNumBytes oleobject loo_SbRange integer li_BytesRemaining integer li_ChunkSize integer li_ExpectedStatusCode integer li_NumReplaced // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // 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 loo_Json = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject") if li_rc < 0 then destroy loo_Json MessageBox("Error","Connecting to COM object failed") return end if loo_Json.UpdateString("client_id","2871da2c-8176-4b7f-869b-2311aa82e743") loo_Json.UpdateString("client_secret","2hu9Q~~r5QuryUcEkNbg1btLtnfU1VUXzhSCG6brH") loo_Json.UpdateString("scope","https://graph.microsoft.com/.default") loo_Json.UpdateString("token_endpoint","https://login.microsoftonline.com/114d7ed6-71bf-4dbe-a866-748364121bf2/oauth2/v2.0/token") loo_Http = create oleobject // Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0 li_rc = loo_Http.ConnectToNewObject("Chilkat.Http") loo_Http.AuthToken = loo_Json.Emit() // ---------------------------------------------------------------------------- // 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 loo_Http.SetUrlVar("path_to_item","/somefolder/big.zip") loo_Http.SetUrlVar("user_id","4fe732c3-322e-4a6b-b729-2fd1eb5c6104") ls_Url = "https://graph.microsoft.com/v1.0/users/{$user_id}/drive/root:/{$path_to_item}:/createUploadSession" loo_Resp = loo_Http.PostJson2(ls_Url,"application/json","{}") if loo_Http.LastMethodSuccess <> 1 then Write-Debug loo_Http.LastErrorText destroy loo_Json destroy loo_Http return end if // If successful, a 200 status code is returned, with the session details (in JSON format). loo_JsonSession = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_JsonSession.ConnectToNewObject("Chilkat.JsonObject") loo_JsonSession.EmitCompact = 0 loo_JsonSession.Load(loo_Resp.BodyStr) if loo_Resp.StatusCode <> 200 then Write-Debug loo_JsonSession.Emit() Write-Debug "Response status = " + string(loo_Resp.StatusCode) destroy loo_Resp destroy loo_Json destroy loo_Http destroy loo_JsonSession return end if destroy loo_Resp Write-Debug loo_JsonSession.Emit() // 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.) li_FragSize = 320 * 1024 ls_LocalFilePath = "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. loo_Fac = create oleobject // Use "Chilkat_9_5_0.FileAccess" for versions of Chilkat < 10.0.0 li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess") li_FileSize = loo_Fac.FileSize(ls_LocalFilePath) // Open the file to get the number of fragments. li_Success = loo_Fac.OpenForRead(ls_LocalFilePath) if li_Success = 0 then Write-Debug loo_Fac.LastErrorText destroy loo_Json destroy loo_Http destroy loo_JsonSession destroy loo_Fac return end if li_NumFragments = loo_Fac.GetNumBlocks(li_FragSize) loo_Fac.FileClose() i = 0 Write-Debug "fileSize = " + string(li_FileSize) Write-Debug "numFragments = " + string(li_NumFragments) loo_UploadUrl = create oleobject // Use "Chilkat_9_5_0.Url" for versions of Chilkat < 10.0.0 li_rc = loo_UploadUrl.ConnectToNewObject("Chilkat.Url") loo_UploadUrl.ParseUrl(loo_JsonSession.StringOf("uploadUrl")) loo_Json.EmitCompact = 0 loo_Req = create oleobject // Use "Chilkat_9_5_0.HttpRequest" for versions of Chilkat < 10.0.0 li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest") loo_Req.HttpVerb = "PUT" loo_Req.Path = loo_UploadUrl.PathWithQueryParams loo_Req.ContentType = "application/octet-stream" loo_SbOffset = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbOffset.ConnectToNewObject("Chilkat.StringBuilder") loo_SbNumBytes = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbNumBytes.ConnectToNewObject("Chilkat.StringBuilder") loo_SbRange = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbRange.ConnectToNewObject("Chilkat.StringBuilder") // 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. loo_Http.AuthToken = "" li_BytesRemaining = li_FileSize do while i < li_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. li_ChunkSize = li_FragSize li_ExpectedStatusCode = 202 if li_BytesRemaining < li_ChunkSize then li_ChunkSize = li_BytesRemaining li_ExpectedStatusCode = 201 end if Write-Debug " this chunkSize = " + string(li_ChunkSize) // Indicate the fragment in the local file to be streamed in the upload. loo_SbOffset.Clear() loo_SbOffset.AppendInt(i * li_FragSize) loo_SbNumBytes.Clear() loo_SbNumBytes.AppendInt(li_ChunkSize) loo_Req.StreamChunkFromFile(ls_LocalFilePath,loo_SbOffset.GetAsString(),loo_SbNumBytes.GetAsString()) // The Content-Range header field must be set for this fragment. For example: // Content-Range: bytes 0-25/128 loo_SbRange.SetString("bytes start-end/fileSize") li_NumReplaced = loo_SbRange.ReplaceI("start",i * li_FragSize) li_NumReplaced = loo_SbRange.ReplaceI("end",i * li_FragSize + li_ChunkSize - 1) li_NumReplaced = loo_SbRange.ReplaceI("fileSize",li_FileSize) loo_Req.AddHeader("Content-Range",loo_SbRange.GetAsString()) Write-Debug " this content-range: " + loo_SbRange.GetAsString() loo_Resp = loo_Http.SynchronousRequest(loo_UploadUrl.Host,443,1,loo_Req) if loo_Http.LastMethodSuccess <> 1 then Write-Debug loo_Http.LastErrorText destroy loo_Json destroy loo_Http destroy loo_JsonSession destroy loo_Fac destroy loo_UploadUrl destroy loo_Req destroy loo_SbOffset destroy loo_SbNumBytes destroy loo_SbRange return end if loo_Json.Load(loo_Resp.BodyStr) // A 202 response status code indicates success. if loo_Resp.StatusCode <> li_ExpectedStatusCode then Write-Debug loo_Json.Emit() Write-Debug "Response status = " + string(loo_Resp.StatusCode) destroy loo_Resp destroy loo_Json destroy loo_Http destroy loo_JsonSession destroy loo_Fac destroy loo_UploadUrl destroy loo_Req destroy loo_SbOffset destroy loo_SbNumBytes destroy loo_SbRange return end if destroy loo_Resp Write-Debug loo_Json.Emit() Write-Debug "---- Chunk " + string(i) + " uploaded ----" li_BytesRemaining = li_BytesRemaining - li_ChunkSize i = i + 1 loop Write-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 destroy loo_Json destroy loo_Http destroy loo_JsonSession destroy loo_Fac destroy loo_UploadUrl destroy loo_Req destroy loo_SbOffset destroy loo_SbNumBytes destroy loo_SbRange |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.