forked from microsoftgraph/msgraph-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOneDriveLargeFileUploadTask.ts
56 lines (48 loc) · 1.97 KB
/
OneDriveLargeFileUploadTask.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
/**
* -------------------------------------------------------------------------------------------
* 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
/* eslint-disable simple-import-sort/imports*/
import { OneDriveLargeFileUploadOptions, OneDriveLargeFileUploadTask, Range, StreamUpload, UploadEventHandlers, UploadResult } from "@microsoft/microsoft-graph-client";
import * as fs from "fs";
import { Readable } from "stream";
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() {
const file = fs.createReadStream("./test.pdf");
const fileName = "FILENAME";
const fileDescription = "FILEDESCRIPTION";
const stats = fs.statSync(`./test.pdf`);
const totalSize = stats.size;
const progress = (range?: Range, extraCallBackParam?: unknown) => {
console.log("uploading range: ", range);
console.log(extraCallBackParam);
return true;
};
const uploadEventHandlers: UploadEventHandlers = {
progress,
extraCallbackParam: "any parameter needed by the callback implementation",
};
const options: OneDriveLargeFileUploadOptions = {
fileName,
fileDescription,
conflictBehavior: "rename",
rangeSize: 1024 * 1024,
uploadEventHandlers,
};
const stream = new StreamUpload(file, "test.pdf", totalSize);
const task = await OneDriveLargeFileUploadTask.createTaskWithFileObject<Readable>(client, stream, options);
const uploadResult: UploadResult = await task.upload();
return uploadResult;
}
upload()
.then((uploadResult) => console.log(uploadResult))
.catch((error) => console.log(error));