2018-11-08 16:45:01 +03:00
|
|
|
// Steps to run this sample
|
|
|
|
// 1. npm install
|
|
|
|
// 2. Enter your storage account name and shared key in main()
|
|
|
|
|
|
|
|
const {
|
|
|
|
Aborter,
|
|
|
|
StorageURL,
|
2018-11-22 11:08:48 +03:00
|
|
|
ServiceURL,
|
|
|
|
ShareURL,
|
|
|
|
DirectoryURL,
|
|
|
|
FileURL,
|
2018-11-08 16:45:01 +03:00
|
|
|
SharedKeyCredential,
|
2018-11-28 12:50:58 +03:00
|
|
|
AnonymousCredential
|
2018-11-22 11:08:48 +03:00
|
|
|
} = require(".."); // Change to "@azure/storage-file" in your package
|
2018-11-08 16:45:01 +03:00
|
|
|
|
|
|
|
async function main() {
|
|
|
|
// Enter your storage account name and shared key
|
2018-11-22 11:08:48 +03:00
|
|
|
const account = "";
|
|
|
|
const accountKey = "";
|
2018-11-08 16:45:01 +03:00
|
|
|
|
|
|
|
// Use SharedKeyCredential with storage account and account key
|
2018-11-22 11:08:48 +03:00
|
|
|
// SharedKeyCredential is only avaiable in Node.js runtime, not in browsers
|
2018-11-08 16:45:01 +03:00
|
|
|
const sharedKeyCredential = new SharedKeyCredential(account, accountKey);
|
|
|
|
|
|
|
|
// Use AnonymousCredential when url already includes a SAS signature
|
|
|
|
const anonymousCredential = new AnonymousCredential();
|
|
|
|
|
2018-11-28 12:50:58 +03:00
|
|
|
// Use sharedKeyCredential or anonymousCredential to create a pipeline
|
2018-11-08 16:45:01 +03:00
|
|
|
const pipeline = StorageURL.newPipeline(sharedKeyCredential);
|
|
|
|
|
2018-11-22 11:08:48 +03:00
|
|
|
// List shares
|
2018-11-08 16:45:01 +03:00
|
|
|
const serviceURL = new ServiceURL(
|
2018-11-22 11:08:48 +03:00
|
|
|
// When using AnonymousCredential, following url should include a valid SAS
|
|
|
|
`https://${account}.file.core.windows.net`,
|
2018-11-08 16:45:01 +03:00
|
|
|
pipeline
|
|
|
|
);
|
|
|
|
|
2018-11-22 11:08:48 +03:00
|
|
|
console.log(`List shares`);
|
2018-11-08 16:45:01 +03:00
|
|
|
let marker;
|
|
|
|
do {
|
2018-11-22 11:08:48 +03:00
|
|
|
const listSharesResponse = await serviceURL.listSharesSegment(
|
2018-11-08 16:45:01 +03:00
|
|
|
Aborter.none,
|
|
|
|
marker
|
|
|
|
);
|
|
|
|
|
2018-11-22 11:08:48 +03:00
|
|
|
marker = listSharesResponse.nextMarker;
|
|
|
|
for (const share of listSharesResponse.shareItems) {
|
|
|
|
console.log(`\tShare: ${share.name}`);
|
2018-11-08 16:45:01 +03:00
|
|
|
}
|
|
|
|
} while (marker);
|
|
|
|
|
2018-11-22 11:08:48 +03:00
|
|
|
// Create a share
|
|
|
|
const shareName = `newshare${new Date().getTime()}`;
|
|
|
|
const shareURL = ShareURL.fromServiceURL(serviceURL, shareName);
|
|
|
|
await shareURL.create(Aborter.none);
|
|
|
|
console.log(`Create share ${shareName} successfully`);
|
|
|
|
|
|
|
|
// Create a directory
|
|
|
|
const directoryName = `newdirectory${new Date().getTime()}`;
|
|
|
|
const directoryURL = DirectoryURL.fromShareURL(shareURL, directoryName);
|
|
|
|
await directoryURL.create(Aborter.none);
|
|
|
|
console.log(`Create directory ${directoryName} successfully`);
|
|
|
|
|
|
|
|
// Create a file
|
|
|
|
const content = "Hello World!";
|
|
|
|
const fileName = "newfile" + new Date().getTime();
|
|
|
|
const fileURL = FileURL.fromDirectoryURL(directoryURL, fileName);
|
|
|
|
await fileURL.create(Aborter.none, content.length);
|
|
|
|
console.log(`Create file ${fileName} successfully`);
|
|
|
|
|
|
|
|
// Upload file range
|
|
|
|
await fileURL.uploadRange(Aborter.none, content, 0, content.length);
|
|
|
|
console.log(`Upload file range "${content}" to ${fileName} successfully`);
|
|
|
|
|
|
|
|
// List directories and files
|
|
|
|
console.log(`List directories and files under directory ${directoryName}`);
|
|
|
|
marker = undefined;
|
2018-11-08 16:45:01 +03:00
|
|
|
do {
|
2018-11-22 11:08:48 +03:00
|
|
|
const listFilesAndDirectoriesResponse = await directoryURL.listFilesAndDirectoriesSegment(
|
2018-11-08 16:45:01 +03:00
|
|
|
Aborter.none,
|
|
|
|
marker
|
|
|
|
);
|
|
|
|
|
2018-11-22 11:08:48 +03:00
|
|
|
marker = listFilesAndDirectoriesResponse.nextMarker;
|
|
|
|
for (const file of listFilesAndDirectoriesResponse.segment.fileItems) {
|
|
|
|
console.log(`\tFile: ${file.name}`);
|
|
|
|
}
|
|
|
|
for (const directory of listFilesAndDirectoriesResponse.segment
|
|
|
|
.directoryItems) {
|
|
|
|
console.log(`\tDirectory: ${directory.name}`);
|
2018-11-08 16:45:01 +03:00
|
|
|
}
|
|
|
|
} while (marker);
|
|
|
|
|
2018-11-22 11:08:48 +03:00
|
|
|
// Get file content from position 0 to the end
|
|
|
|
// In Node.js, get downloaded data by accessing downloadFileResponse.readableStreamBody
|
|
|
|
// In browsers, get downloaded data by accessing downloadFileResponse.blobBody
|
|
|
|
const downloadFileResponse = await fileURL.download(Aborter.none, 0);
|
2018-11-08 16:45:01 +03:00
|
|
|
console.log(
|
2018-11-22 11:08:48 +03:00
|
|
|
`Downloaded file content${await streamToString(
|
|
|
|
downloadFileResponse.readableStreamBody
|
|
|
|
)}`
|
2018-11-08 16:45:01 +03:00
|
|
|
);
|
|
|
|
|
2018-11-22 11:08:48 +03:00
|
|
|
// Delete share
|
|
|
|
await shareURL.delete(Aborter.none);
|
|
|
|
console.log(`deleted share ${shareName}`);
|
|
|
|
}
|
2018-11-08 16:45:01 +03:00
|
|
|
|
2018-11-22 11:08:48 +03:00
|
|
|
// A helper method used to read a Node.js readable stream into string
|
|
|
|
async function streamToString(readableStream) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const chunks = [];
|
|
|
|
readableStream.on("data", data => {
|
|
|
|
chunks.push(data.toString());
|
|
|
|
});
|
|
|
|
readableStream.on("end", () => {
|
|
|
|
resolve(chunks.join(""));
|
|
|
|
});
|
|
|
|
readableStream.on("error", reject);
|
|
|
|
});
|
2018-11-08 16:45:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// An async method returns a Promise object, which is compatible with then().catch() coding style.
|
|
|
|
main()
|
|
|
|
.then(() => {
|
|
|
|
console.log("Successfully executed sample.");
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err.message);
|
2018-11-22 11:08:48 +03:00
|
|
|
});
|