This repository was archived by the owner on Sep 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfileSample.js
More file actions
213 lines (196 loc) · 10.6 KB
/
fileSample.js
File metadata and controls
213 lines (196 loc) · 10.6 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious. No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//----------------------------------------------------------------------------------
/**
* Azure Storage File Sample - Demonstrate how to use the File Storage service.
* File storage stores unstructured data such as text, binary data, documents or media files.
* Files can be accessed from anywhere in the world via HTTP or HTTPS.
*
* Documentation References:
* - What is a Storage Account - http://azure.microsoft.com/en-us/documentation/articles/storage-whatis-account/
* - Getting Started with File - https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-files/
* - File Service Concepts - http://msdn.microsoft.com/en-us/library/dd179376.aspx
* - File Service REST API - https://msdn.microsoft.com/en-us/library/dn167006.aspx
* - File Service Node API - http://azure.github.io/azure-storage-node/FileService.html
* - Delegating Access with Shared Access Signatures - http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-shared-access-signature-part-1/
*/
var fs = require('fs');
var util = require('util');
var guid = require('node-uuid');
var crypto = require('crypto');
var storage = require('azure-storage');
runFileSamples();
function runFileSamples() {
/**
* Instructions: This sample can be run using either the Azure Storage Emulator that installs as part of this SDK - or by
* updating the app.Config file with your connection string.
*
* To run the sample using the Storage Service
* Open the app.config file and comment out the connection string for the emulator ("ueDevelopmentStorage":true) and
* set the connection string for the storage service.
*/
console.log('\nAzure Storage File Sample\n');
var current = 0;
var scenarios = [
{
scenario: basicStorageFileOperations,
message: 'Basic File Sample Completed\n'
}];
var callback = function (error) {
if (error) {
throw error;
} else {
console.log(scenarios[current].message);
current++;
if (current < scenarios.length) {
scenarios[current].scenario(callback);
}
}
};
scenarios[current].scenario(callback);
}
/**
* File basics.
* @ignore
*
* @param {errorOrResult} callback The callback function.
*/
function basicStorageFileOperations(callback) {
// Create a file client for interacting with the file service from connection string
// How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
var fileService = storage.createFileService(readConfig().connectionString);
var imageToUpload = "HelloWorld.png";
var shareName = "demofileshare-" + guid.v1();
var directoryName = "demofiledirectory";
var fileName = "demobfile-" + imageToUpload;
console.log('Basic File Sample');
// Create a share for organizing files within the storage account.
console.log('1. Creating file share');
fileService.createShareIfNotExists(shareName, function (error) {
if (error) {
callback(error);
} else {
// Create a directory under the root directory
console.log('2. Creating a directory under the root directory');
fileService.createDirectoryIfNotExists(shareName, directoryName, function (error) {
if (error) {
callback(error);
} else {
// Create a directory under the just created directory
var nextDirectoryName = directoryName + '/' + directoryName + '01';
fileService.createDirectoryIfNotExists(shareName, nextDirectoryName, function (error) {
if (error) {
callback(error);
} else {
// Uploading a local file to the directory created above
console.log('3. Uploading a file to directory');
fileService.createFileFromLocalFile(shareName, directoryName, fileName, imageToUpload, function (error) {
if (error) {
callback(error);
} else {
// List all files/directories under the root directory
console.log('4. List files/directories in root directory');
listFilesAndDirectories(fileService, shareName, directoryName, null, null, function (error, results) {
if (error) {
callback(error);
} else {
for (var i = 0; i < results.files.length; i++) {
console.log(util.format(' - %s (type: file)'), results.files[i].name);
}
for (var j = 0; j < results.directories.length; j++) {
console.log(util.format(' - %s (type: directory)'), results.directories[j].name);
}
// Download the uploaded file to your file system
console.log('5. Download the uploaded file to your file system');
var downloadedImageName = util.format('CopyOf%s', imageToUpload);
fileService.getFileToLocalFile(shareName, directoryName, fileName, downloadedImageName, function (error) {
if (error) {
callback(error);
} else {
// Clean up after the demo
console.log('6. Delete file');
fileService.deleteFileIfExists(shareName, directoryName, fileName, function (error) {
try { fs.unlinkSync(downloadedImageName); } catch (e) { }
if (error) {
callback(error);
} else {
console.log('7. Delete file share');
fileService.deleteShareIfExists(shareName, function (error) {
callback(error);
});
}
});
}
});
}
});
}
});
}
});
}
});
}
});
}
/**
* Lists file in the share.
* @ignore
*
* @param {FileService} fileService The file service client.
* @param {string} share The share name.
* @param {object} token A continuation token returned by a previous listing operation.
* Please use 'null' or 'undefined' if this is the first operation.
* @param {object} [options] The request options.
* @param {int} [options.maxResults] Specifies the maximum number of directories to return per call to Azure ServiceClient.
* This does NOT affect list size returned by this function. (maximum: 5000)
* @param {LocationMode} [options.locationMode] Specifies the location mode used to decide which location the request should be sent to.
* Please see StorageUtilities.LocationMode for the possible values.
* @param {int} [options.timeoutIntervalInMs] The server timeout interval, in milliseconds, to use for the request.
* @param {int} [options.maximumExecutionTimeInMs] The maximum execution time, in milliseconds, across all potential retries, to use when making this request.
* The maximum execution time interval begins at the time that the client begins building the request. The maximum
* execution time is checked intermittently while performing requests, and before executing retries.
* @param {string} [options.clientRequestId] A string that represents the client request ID with a 1KB character limit.
* @param {bool} [options.useNagleAlgorithm] Determines whether the Nagle algorithm is used; true to use the Nagle algorithm; otherwise, false.
* The default value is false.
* @param {errorOrResult} callback `error` will contain information
* if an error occurs; otherwise `result` will contain `entries` and `continuationToken`.
* `entries` gives a list of directories and the `continuationToken` is used for the next listing operation.
* `response` will contain information related to this operation.
*/
function listFilesAndDirectories(fileService, share, directory, token, options, callback) {
var items = { files: [], directories: []};
fileService.listFilesAndDirectoriesSegmented(share, directory, token, options, function(error, result) {
items.files.push.apply(items.files, result.entries.files);
items.directories.push.apply(items.directories, result.entries.directories);
var token = result.continuationToken;
if (token) {
console.log(' Received a page of results. There are ' + result.entries.length + ' items on this page.');
listFilesAndDirectories(fileService, share, directory, token, options, callback);
} else {
console.log(' Completed listing. There are ' + items.files.length + ' files and ' + items.directories.length + ' directories.' );
callback(null, items);
}
});
}
/**
* Reads the configurations.
* @ignore
*
* @return {Object}
*/
function readConfig() {
return JSON.parse(fs.readFileSync('app.config', 'utf8'));
}