azure-storage-js/file/samples/basic.sample.js

131 строка
4.1 KiB
JavaScript
Исходник Обычный вид История

// 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,
SharedKeyCredential,
AnonymousCredential
2018-11-22 11:08:48 +03:00
} = require(".."); // Change to "@azure/storage-file" in your package
async function main() {
// Enter your storage account name and shared key
2018-11-22 11:08:48 +03:00
const account = "";
const accountKey = "";
// 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
const sharedKeyCredential = new SharedKeyCredential(account, accountKey);
// Use AnonymousCredential when url already includes a SAS signature
const anonymousCredential = new AnonymousCredential();
// Use sharedKeyCredential or anonymousCredential to create a pipeline
const pipeline = StorageURL.newPipeline(sharedKeyCredential);
2018-11-22 11:08:48 +03:00
// List shares
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`,
pipeline
);
2018-11-22 11:08:48 +03:00
console.log(`List shares`);
let marker;
do {
2018-11-22 11:08:48 +03:00
const listSharesResponse = await serviceURL.listSharesSegment(
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}`);
}
} 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;
do {
2018-11-22 11:08:48 +03:00
const listFilesAndDirectoriesResponse = await directoryURL.listFilesAndDirectoriesSegment(
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}`);
}
} 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);
console.log(
2018-11-22 11:08:48 +03:00
`Downloaded file content${await streamToString(
downloadFileResponse.readableStreamBody
)}`
);
2018-11-22 11:08:48 +03:00
// Delete share
await shareURL.delete(Aborter.none);
console.log(`deleted share ${shareName}`);
}
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);
});
}
// 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
});