-
Notifications
You must be signed in to change notification settings - Fork 230
/
OneDriveLargeFileUploadTask.js
61 lines (51 loc) · 1.96 KB
/
OneDriveLargeFileUploadTask.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
// First, create an instance of the Microsoft Graph JS SDK Client class.
const { client } = require("../clientInitialization/ClientWithOptions");
/**
* OR
* const { client } = require("../clientInitialization/TokenCredentialAuthenticationProvider");
* OR
* require or import client created using an custom authentication provider
*/
const fs = require("fs");
const { OneDriveLargeFileUploadTask, StreamUpload } = require("@microsoft/microsoft-graph-client");
require("isomorphic-fetch");
async function upload() {
const fileName = "FILE_NAME";
const file = fs.createReadStream(`./${fileName}`);
const totalsize = stats.size;
const progress = (range, extraCallbackParam) => {
// implement the progress callback here
console.log("uploading range: ", range);
console.log(extraCallbackParam);
return true;
};
const uploadEventHandlers = {
progress,
extraCallbackParam: "any parameter needed by the callback implementation",
};
const options = {
fileName,
conflictBehavior: "rename",
rangeSize: 1024 * 1024,
uploadEventHandlers,
};
const fileObject = new StreamUpload(file, fileName, totalsize);
//OR
// You can also use a FileUpload instance
//const file = fs.readFileSync();
//const fileObject = new FileUpload(file, fileName, totalsize);
//OR
// You can also create an object from a custom implementation of the FileObject interface
const task = await OneDriveLargeFileUploadTask.createTaskWithFileObject(client, fileObject, options);
const uploadResult = await task.upload();
return uploadResult;
}
upload()
.then((uploadResult) => console.log(uploadResult))
.catch((error) => console.log(error));