-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
advancedRequestOptions.ts
102 lines (86 loc) · 3.81 KB
/
advancedRequestOptions.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* @summary use advanced HTTP pipeline and request options for several methods
*/
import * as fs from "fs";
import { AbortController } from "@azure/abort-controller";
import { AnonymousCredential, ShareServiceClient, newPipeline } from "@azure/storage-file-share";
// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();
export async function main() {
// Fill in following settings before running this sample
const account = process.env.ACCOUNT_NAME || "";
const accountSas = process.env.ACCOUNT_SAS || "";
const localFilePath = "README.md";
const pipeline = newPipeline(new AnonymousCredential(), {
// httpClient: MyHTTPClient, // A customized HTTP client implementing IHttpClient interface
retryOptions: { maxTries: 4 }, // Retry options
userAgentOptions: { userAgentPrefix: "AdvancedSample V1.0.0" }, // Customized user-agent string
keepAliveOptions: {
// Keep alive is enabled by default, disable keep alive by setting false
enable: false
}
});
const serviceClient = new ShareServiceClient(
`https://${account}.file.core.windows.net${accountSas}`,
pipeline
);
// Create a share
const shareName = `newshare${new Date().getTime()}`;
const shareClient = serviceClient.getShareClient(shareName);
await shareClient.create();
console.log(`Created share ${shareName} successfully`);
// Create a directory
const directoryName = `newdirectory${new Date().getTime()}`;
const directoryClient = shareClient.getDirectoryClient(directoryName);
await directoryClient.create();
console.log(`Created directory ${directoryName} successfully`);
// Upload local file to Azure file parallelly
const fileName = "newfile" + new Date().getTime();
const fileClient = directoryClient.getFileClient(fileName);
const fileSize = fs.statSync(localFilePath).size;
// Parallel uploading with ShareFileClient.uploadFile() in Node.js runtime
// ShareFileClient.uploadFile() is only available in Node.js
await fileClient.uploadFile(localFilePath, {
rangeSize: 4 * 1024 * 1024, // 4MB range size
concurrency: 20, // 20 concurrency
onProgress: (ev) => console.log(ev)
});
console.log("uploadFile succeeded");
// Parallel uploading a Readable stream with ShareFileClient.uploadStream() in Node.js runtime
// ShareFileClient.uploadStream() is only available in Node.js
await fileClient.uploadStream(fs.createReadStream(localFilePath), fileSize, 4 * 1024 * 1024, 20, {
abortSignal: AbortController.timeout(30 * 60 * 1000), // Abort uploading with timeout in 30mins
onProgress: (ev: any) => console.log(ev)
});
console.log("uploadStream succeeded");
// Parallel uploading a browser File/Blob/ArrayBuffer in browsers with ShareFileClient.uploadBrowserData()
// Uncomment following code in browsers because ShareFileClient.uploadBrowserData() is only available in browsers
/*
const browserFile = document.getElementById("fileinput").files[0];
await fileClient.uploadBrowserData(browserFile, {
rangeSize: 4 * 1024 * 1024, // 4MB range size
concurrency: 20, // 20 concurrency
onProgress: ev => console.log(ev)
});
*/
// Parallel downloading an Azure file into Node.js buffer
// ShareFileClient.downloadToBuffer() is only available in Node.js
const buffer = Buffer.alloc(fileSize);
await fileClient.downloadToBuffer(buffer, undefined, undefined, {
abortSignal: AbortController.timeout(30 * 60 * 1000),
rangeSize: 4 * 1024 * 1024, // 4MB range size
concurrency: 20, // 20 concurrency
onProgress: (ev) => console.log(ev)
});
console.log("downloadToBuffer succeeded");
// Delete share
await shareClient.delete();
console.log(`Deleted share ${shareClient.name}`);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});