forked from microsoftgraph/msgraph-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargeFileUploadTask.js
78 lines (66 loc) · 2.54 KB
/
LargeFileUploadTask.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* -------------------------------------------------------------------------------------------
* 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 { LargeFileUploadTask } = require("@microsoft/microsoft-graph-types");
require("isomorphic-fetch");
async function upload() {
const fileName = "FILE_NAME";
const file = fs.createReadStream(`./${fileName}`);
const stats = fs.statSync(`./${fileName}`);
const totalSize = stats.size;
const progress = (range, extraCallbackParam) => {
// Implement the progress callback here
console.log("uploading range: ", range);
console.log(extraCallbackParam);
};
const uploadEventHandlers = {
progress,
extraCallbackParam: "Any parameter needed by the callback implementation",
};
const options = {
rangeSize: 1024 * 1024,
uploadEventHandlers,
};
// Create upload session for OneDrive or SharePoint Upload"
const payload = {
item: {
"@microsoft.graph.conflictBehavior": "rename",
},
};
const uploadSession = await new LargeFileUploadTask.createUploadSession(client, "https://graph.microsoft.com/v1.0/sites/root/drive/items/{item-id}/createuploadsession", payload);
// OR
// Create upload session for Outlook API
// const uploadSessionURL = `me/messages/${messageId}/attachments/createUploadSession`;
// const payload = {
// AttachmentItem: {
// attachmentType: 'file',
// name: "FILE_NAME",
// size: totalSize,
// }
// }
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 = new MicrosoftGraph.LargeFileUploadTask(client, fileObject, uploadSession, options);
const uploadResult = await task.upload();
return uploadResult;
}
upload()
.then((uploadResult) => console.log(uploadResult))
.catch((error) => console.log(error));