Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const fileClient = directoryClient.getFileClient(fileName);
const fileSize = fs.statSync(localFilePath).size;
// Parallel uploading with FileClient.uploadFile() in Node.js runtime
// FileClient.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 success");
// Parallel uploading a Readable stream with FileClient.uploadStream() in Node.js runtime
// FileClient.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 success");
// Parallel uploading a browser File/Blob/ArrayBuffer in browsers with FileClient.uploadBrowserData()
// Uncomment following code in browsers because FileClient.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
// Parallel uploading a browser File/Blob/ArrayBuffer in browsers with FileClient.uploadBrowserData()
// Uncomment following code in browsers because FileClient.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
// FileClient.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 success");
// Delete share
await shareClient.delete();
console.log("deleted share");
}
const fileClient = directoryClient.getFileClient(fileName);
const fileSize = fs.statSync(localFilePath).size;
// Parallel uploading with FileClient.uploadFile() in Node.js runtime
// FileClient.uploadFile() is only available in Node.js
await fileClient.uploadFile(localFilePath, {
rangeSize: 4 * 1024 * 1024, // 4MB range size
parallelism: 20, // 20 concurrency
onProgress: (ev) => console.log(ev)
});
console.log("uploadFile success");
// Parallel uploading a Readable stream with FileClient.uploadStream() in Node.js runtime
// FileClient.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) => console.log(ev)
});
console.log("uploadStream success");
// Parallel uploading a browser File/Blob/ArrayBuffer in browsers with FileClient.uploadBrowserData()
// Uncomment following code in browsers because FileClient.uploadBrowserData() is only available in browsers
/*
const browserFile = document.getElementById("fileinput").files[0];
await fileClient.uploadBrowserData(browserFile, {
rangeSize: 4 * 1024 * 1024, // 4MB range size
parallelism: 20, // 20 concurrency
onProgress: ev => console.log(ev)
});
*/
// Parallel downloading an Azure file into Node.js buffer
/*
const browserFile = document.getElementById("fileinput").files[0];
await blockBlobClient.uploadBrowserData(browserFile, {
blockSize: 4 * 1024 * 1024, // 4MB block size
concurrency: 20, // 20 concurrency
onProgress: ev => console.log(ev)
});
*/
// Parallel downloading a block blob into Node.js buffer
// downloadToBuffer is only available in Node.js
const fileSize = fs.statSync(localFilePath).size;
const buffer = Buffer.alloc(fileSize);
try {
await blockBlobClient.downloadToBuffer(buffer, 0, undefined, {
abortSignal: AbortController.timeout(30 * 60 * 1000), // Abort uploading with timeout in 30mins
blockSize: 4 * 1024 * 1024, // 4MB block size
concurrency: 20, // 20 concurrency
onProgress: (ev) => console.log(ev)
});
console.log("downloadToBuffer succeeds");
} catch (err) {
console.log(
`downloadToBuffer failed, requestId - ${err.details.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`
);
}
// Archive the blob - Log the error codes
await blockBlobClient.setAccessTier("Archive");
try {
// Downloading an archived blockBlob fails
console.log("// Downloading an archived blockBlob fails...");
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
parallelism: 20, // 20 concurrency
onProgress: (ev) => console.log(ev)
});
console.log("uploadFile success");
// 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) => console.log(ev)
});
console.log("uploadStream success");
// 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
parallelism: 20, // 20 concurrency
onProgress: ev => console.log(ev)
});
*/
// Parallel downloading an Azure file into Node.js buffer
/*
const browserFile = document.getElementById("fileinput").files[0];
await blockBlobClient.uploadBrowserData(browserFile, {
blockSize: 4 * 1024 * 1024, // 4MB block size
concurrency: 20, // 20 concurrency
onProgress: ev => console.log(ev)
});
*/
// Parallel downloading a block blob into Node.js buffer
// downloadToBuffer is only available in Node.js
const fileSize = fs.statSync(localFilePath).size;
const buffer = Buffer.alloc(fileSize);
try {
await blockBlobClient.downloadToBuffer(buffer, 0, undefined, {
abortSignal: AbortController.timeout(30 * 60 * 1000), // Abort uploading with timeout in 30mins
blockSize: 4 * 1024 * 1024, // 4MB block size
concurrency: 20, // 20 concurrency
onProgress: (ev) => console.log(ev)
});
console.log("downloadToBuffer succeeds");
} catch (err) {
console.log(
`downloadToBuffer failed, requestId - ${err.details.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`
);
}
// Archive the blob - Log the error codes
await blockBlobClient.setAccessTier("Archive");
try {
// Downloading an archived blockBlob fails
console.log("// Downloading an archived blockBlob fails...");
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 success");
// 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 success");
// 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
async function execute() {
const containerName = "demo";
const blobName = "quickstart.txt";
const content = "Hello Node SDK";
const localFilePath = "../readme.md";
const credentials = new StorageSharedKeyCredential(STORAGE_ACCOUNT_NAME, ACCOUNT_ACCESS_KEY);
const blobServiceClient = new BlobServiceClient(`https://${STORAGE_ACCOUNT_NAME}.blob.core.windows.net`,credentials);
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobClient = containerClient.getBlobClient(blobName);
const blockBlobClient = blobClient.getBlockBlobClient();
const aborter = AbortController.timeout(30 * ONE_MINUTE);
await containerClient.create();
console.log(`Container: "${containerName}" is created`);
console.log("Containers:");
await showContainerNames(aborter, blobServiceClient);
await blockBlobClient.upload(content, content.length, aborter);
console.log(`Blob "${blobName}" is uploaded`);
await uploadLocalFile(aborter, containerClient, localFilePath);
console.log(`Local file "${localFilePath}" is uploaded`);
await uploadStream(aborter, containerClient, localFilePath);
console.log(`Local file "${localFilePath}" is uploaded as a stream`);