You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/storage/blobs/storage-quickstart-blobs-nodejs-v10.md
+88-29Lines changed: 88 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ description: Create, upload, and delete blobs and containers in Node.js with Azu
4
4
author: mhopkins-msft
5
5
6
6
ms.author: mhopkins
7
-
ms.date: 11/14/2018
7
+
ms.date: 09/24/2019
8
8
ms.service: storage
9
9
ms.subservice: blobs
10
10
ms.topic: quickstart
@@ -47,6 +47,7 @@ npm install
47
47
```
48
48
49
49
## Run the sample
50
+
50
51
Now that the dependencies are installed, you can run the sample by issuing the following command:
51
52
52
53
```bash
@@ -56,10 +57,11 @@ npm start
56
57
The output from the app will be similar to the following example:
57
58
58
59
```bash
60
+
Container "demo" is created
59
61
Containers:
60
62
- container-one
61
63
- container-two
62
-
Container "demo" is created
64
+
- demo
63
65
Blob "quickstart.txt" is uploaded
64
66
Local file "./readme.md" is uploaded
65
67
Blobs in"demo" container:
@@ -71,9 +73,11 @@ Blob "quickstart.txt" is deleted
71
73
Container "demo" is deleted
72
74
Done
73
75
```
74
-
If you're using a new storage account for this quickstart, then you may not see container names listed under the label "*Containers*".
76
+
77
+
If you're using a new storage account for this quickstart, then you may only see the *demo* container listed under the label "*Containers:*".
75
78
76
79
## Understanding the code
80
+
77
81
The sample begins by importing a number of classes and functions from the Azure Blob storage namespace. Each of the imported items is discussed in context as they're used in the sample.
The next set of constants helps to reveal the intent of file size calculations during upload operations.
126
+
122
127
```javascript
123
128
constONE_MEGABYTE=1024*1024;
124
129
constFOUR_MEGABYTES=4*ONE_MEGABYTE;
125
130
```
131
+
126
132
Requests made by the API can be set to time-out after a given interval. The [Aborter](/javascript/api/%40azure/storage-blob/aborter?view=azure-node-preview) class is responsible for managing how requests are timed-out and the following constant is used to define timeouts used in this sample.
133
+
127
134
```javascript
128
135
constONE_MINUTE=60*1000;
129
136
```
137
+
130
138
### Calling code
131
139
132
140
To support JavaScript's *async/await* syntax, all the calling code is wrapped in a function named *execute*. Then *execute* is called and handled as a promise.
The following classes are used in this block of code:
160
170
161
171
- The [SharedKeyCredential](/javascript/api/%40azure/storage-blob/sharedkeycredential?view=azure-node-preview) class is responsible for wrapping storage account credentials to provide them to a request pipeline.
@@ -170,6 +180,7 @@ The instance of *ServiceURL* is used with the [ContainerURL](/javascript/api/%40
The *containerURL* and *blockBlobURL* variables are reused throughout the sample to act on the storage account.
174
185
175
186
At this point, the container doesn't exist in the storage account. The instance of *ContainerURL* represents a URL that you can act upon. By using this instance, you can create and delete the container. The location of this container equates to a location such as this:
@@ -183,15 +194,28 @@ The *blockBlobURL* is used to manage individual blobs, allowing you to upload, d
As with the container, the block blob doesn't exist yet. The *blockBlobURL* variable is used later to create the blob by uploading content.
187
199
200
+
### Create a container
201
+
202
+
To create a container, the *ContainerURL*'s *create* method is used.
203
+
204
+
```javascript
205
+
awaitcontainerURL.create(aborter);
206
+
console.log(`Container: "${containerName}" is created`);
207
+
```
208
+
209
+
As the name of the container is defined when calling *ContainerURL.fromServiceURL(serviceURL, containerName)*, calling the *create* method is all that's required to create the container.
210
+
188
211
### Using the Aborter class
189
212
190
213
Requests made by the API can be set to time-out after a given interval. The *Aborter* class is responsible for managing how requests are timed out. The following code creates a context where a set of requests is given 30 minutes to execute.
191
214
192
215
```javascript
193
216
constaborter=Aborter.timeout(30*ONE_MINUTE);
194
217
```
218
+
195
219
Aborters give you control over requests by allowing you to:
196
220
197
221
- designate the amount of time given for a batch of requests
@@ -200,54 +224,54 @@ Aborters give you control over requests by allowing you to:
200
224
- use the *Aborter.none* static member to stop your requests from timing out all together
201
225
202
226
### Show container names
227
+
203
228
Accounts can store a vast number of containers. The following code demonstrates how to list containers in a segmented fashion, which allows you to cycle through a large number of containers. The *showContainerNames* function is passed instances of *ServiceURL* and *Aborter*.
204
229
205
230
```javascript
206
231
console.log("Containers:");
207
232
awaitshowContainerNames(serviceURL, aborter);
208
233
```
234
+
209
235
The *showContainerNames* function uses the *listContainersSegment* method to request batches of container names from the storage account.
When the response is returned, then the *containerItems* are iterated to log the name to the console.
226
250
227
-
### Create a container
228
-
229
-
To create a container, the *ContainerURL*'s *create* method is used.
230
-
231
-
```javascript
232
-
awaitcontainerURL.create(aborter);
233
-
console.log(`Container: "${containerName}" is created`);
234
-
```
235
-
As the name of the container is defined when calling *ContainerURL.fromServiceURL(serviceURL, containerName)*, calling the *create* method is all that's required to create the container.
251
+
When the response is returned, then the *containerItems* are iterated to log the name to the console.
236
252
237
253
### Upload text
254
+
238
255
To upload text to the blob, use the *upload* method.
console.log(`Local file "${localFilePath}" is uploaded`);
249
271
```
272
+
250
273
The *uploadLocalFile* function calls the *uploadFileToBlockBlob* function, which takes the file path and an instance of the destination of the block blob as arguments.
@@ -291,51 +319,82 @@ async function uploadStream(aborter, containerURL, filePath) {
291
319
uploadOptions.maxBuffers);
292
320
}
293
321
```
322
+
294
323
During an upload, *uploadStreamToBlockBlob* allocates buffers to cache data from the stream in case a retry is necessary. The *maxBuffers* value designates at most how many buffers are used as each buffer creates a separate upload request. Ideally, more buffers equate to higher speeds, but at the cost of higher memory usage. The upload speed plateaus when the number of buffers is high enough that the bottleneck transitions to the network or disk instead of the client.
295
324
296
325
### Show blob names
297
-
Just as accounts can contain many containers, each container can potentially contain a vast amount of blobs. Access to each blob in a container are available via an instance of the *ContainerURL* class.
326
+
327
+
Just as accounts can contain many containers, each container can potentially contain a vast amount of blobs. Access to each blob in a container are available via an instance of the *ContainerURL* class.
328
+
298
329
```javascript
299
330
console.log(`Blobs in "${containerName}" container:`);
300
331
awaitshowBlobNames(aborter, containerURL);
301
332
```
333
+
302
334
The function *showBlobNames* calls *listBlobFlatSegment* to request batches of blobs from the container.
0 commit comments