forked from microsoftgraph/msgraph-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargeFileUploadTask.ts
81 lines (66 loc) · 2.75 KB
/
LargeFileUploadTask.ts
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
79
80
81
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
/* eslint-disable @typescript-eslint/no-unused-vars*/
/* eslint-disable simple-import-sort/imports*/
// First, create an instance of the Microsoft Graph JS SDK Client class
import { LargeFileUploadSession, LargeFileUploadTask, LargeFileUploadTaskOptions, Range, StreamUpload, UploadEventHandlers, UploadResult } from "@microsoft/microsoft-graph-client";
import * as fs from "fs";
import { client } from "../clientInitialization/ClientWithOptions";
/**
* OR
* import { client } from ("../clientInitialization/TokenCredentialAuthenticationProvider");
* OR
* require or import client created using an custom authentication provider
*/
async function upload(): Promise<UploadResult> {
const file = fs.createReadStream("./test.pdf");
const stats = fs.statSync(`./test.pdf`);
const totalSize = stats.size;
const progress = (range?: Range, extraCallbackParam?: unknown) => {
// Implement the progress callback here
console.log("uploading range: ", range);
console.log(extraCallbackParam);
};
const uploadEventHandlers: UploadEventHandlers = {
progress,
extraCallbackParam: "any parameter needed by the callback implementation",
};
const options: LargeFileUploadTaskOptions = {
rangeSize: 1024 * 1024,
uploadEventHandlers,
};
// Create upload session for OneDrive or SharePoint Upload"
const payload = {
item: {
"@microsoft.graph.conflictBehavior": "rename",
},
};
const uploadSession: LargeFileUploadSession = await 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, "test.pdf", totalSize);
// OR
// You can also use a FileUpload instance
// const file = fs.readFileSync();
// const fileObject = new FileUpload(file, 'test.pdf', totalSize);
// OR
// You can also create an object from a custom implementation of FileObject
const task = new LargeFileUploadTask(client, fileObject, uploadSession, options);
const uploadResult = await task.upload();
return uploadResult;
}
upload()
.then((uploadResult) => console.log(uploadResult))
.catch((error) => console.log(error));