UNPKG

21.6 kBMarkdownView Raw
1# Azure Storage Blob client library for JavaScript
2
3Azure Storage Blob is Microsoft's object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data. Unstructured data is data that does not adhere to a particular data model or definition, such as text or binary data.
4
5This project provides a client library in JavaScript that makes it easy to consume Microsoft Azure Storage Blob service.
6
7Use the client libraries in this package to:
8
9- Get/Set Blob Service Properties
10- Create/List/Delete Containers
11- Create/Read/List/Update/Delete Block Blobs
12- Create/Read/List/Update/Delete Page Blobs
13- Create/Read/List/Update/Delete Append Blobs
14
15Key links
16
17- [Source code](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/storage/storage-blob)
18- [Package (npm)](https://www.npmjs.com/package/@azure/storage-blob/)
19- [API Reference Documentation](https://docs.microsoft.com/javascript/api/@azure/storage-blob)
20- [Product documentation](https://docs.microsoft.com/azure/storage/blobs/storage-blobs-overview)
21- [Samples](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/storage/storage-blob/samples)
22- [Azure Storage Blob REST APIs](https://docs.microsoft.com/rest/api/storageservices/blob-service-rest-api)
23
24## Getting started
25
26### Currently supported environments
27
28- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
29- Latest versions of Safari, Chrome, Edge, and Firefox.
30
31See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUPPORT.md) for more details.
32
33### Prerequisites
34
35- An [Azure subscription](https://azure.microsoft.com/free/)
36- A [Storage Account](https://docs.microsoft.com/azure/storage/blobs/storage-quickstart-blobs-portal)
37
38### Install the package
39
40The preferred way to install the Azure Storage Blob client library for JavaScript is to use the npm package manager. Type the following into a terminal window:
41
42```bash
43npm install @azure/storage-blob
44```
45
46### Authenticate the client
47
48Azure Storage supports several ways to authenticate. In order to interact with the Azure Blob Storage service you'll need to create an instance of a Storage client - `BlobServiceClient`, `ContainerClient`, or `BlobClient` for example. See [samples for creating the `BlobServiceClient`](#create-the-blob-service-client) to learn more about authentication.
49
50- [Azure Active Directory](#with-defaultazurecredential-from-azureidentity-package)
51- [Shared Key](#with-storagesharedkeycredential)
52- [Shared access signatures](#with-sas-token)
53
54#### Azure Active Directory
55
56The Azure Blob Storage service supports the use of Azure Active Directory to authenticate requests to its APIs. The [`@azure/identity`](https://www.npmjs.com/package/@azure/identity) package provides a variety of credential types that your application can use to do this. Please see the [README for `@azure/identity`](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/README.md) for more details and samples to get you started.
57
58### Compatibility
59
60This library is compatible with Node.js and browsers, and validated against LTS Node.js versions (>=8.16.0) and latest versions of Chrome, Firefox and Edge.
61
62#### Web Workers
63
64This library requires certain DOM objects to be globally available when used in the browser, which web workers do not make available by default. You will need to polyfill these to make this library work in web workers.
65
66For more information please refer to our [documentation for using Azure SDK for JS in Web Workers](https://aka.ms/azsdk/js/web-workers)
67
68This library depends on following DOM APIs which need external polyfills loaded when used in web workers:
69
70- [`document`](https://developer.mozilla.org/docs/Web/API/Document)
71- [`DOMParser`](https://developer.mozilla.org/docs/Web/API/DOMParser)
72- [`Node`](https://developer.mozilla.org/docs/Web/API/Node)
73- [`XMLSerializer`](https://developer.mozilla.org/docs/Web/API/XMLSerializer)
74
75#### Differences between Node.js and browsers
76
77There are differences between Node.js and browsers runtime. When getting started with this library, pay attention to APIs or classes marked with _"ONLY AVAILABLE IN NODE.JS RUNTIME"_ or _"ONLY AVAILABLE IN BROWSERS"_.
78
79- If a blob holds compressed data in `gzip` or `deflate` format and its content encoding is set accordingly, downloading behavior is different between Node.js and browsers. In Node.js storage clients will download the blob in its compressed format, while in browsers the data will be downloaded in de-compressed format.
80
81##### Features, interfaces, classes or functions only available in Node.js
82
83- Shared Key Authorization based on account name and account key
84 - `StorageSharedKeyCredential`
85- Shared Access Signature(SAS) generation
86 - `generateAccountSASQueryParameters()`
87 - `generateBlobSASQueryParameters()`
88- Parallel uploading and downloading. Note that `BlockBlobClient.uploadData()` is available in both Node.js and browsers.
89 - `BlockBlobClient.uploadFile()`
90 - `BlockBlobClient.uploadStream()`
91 - `BlobClient.downloadToBuffer()`
92 - `BlobClient.downloadToFile()`
93
94##### Features, interfaces, classes or functions only available in browsers
95
96- Parallel uploading and downloading
97 - `BlockBlobClient.uploadBrowserData()`
98
99### JavaScript Bundle
100
101To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling).
102
103### CORS
104
105You need to set up [Cross-Origin Resource Sharing (CORS)](https://docs.microsoft.com/rest/api/storageservices/cross-origin-resource-sharing--cors--support-for-the-azure-storage-services) rules for your storage account if you need to develop for browsers. Go to Azure portal and Azure Storage Explorer, find your storage account, create new CORS rules for blob/queue/file/table service(s).
106
107For example, you can create following CORS settings for debugging. But please customize the settings carefully according to your requirements in production environment.
108
109- Allowed origins: \*
110- Allowed verbs: DELETE,GET,HEAD,MERGE,POST,OPTIONS,PUT
111- Allowed headers: \*
112- Exposed headers: \*
113- Maximum age (seconds): 86400
114
115## Key concepts
116
117Blob storage is designed for:
118
119- Serving images or documents directly to a browser.
120- Storing files for distributed access.
121- Streaming video and audio.
122- Writing to log files.
123- Storing data for backup and restore, disaster recovery, and archiving.
124- Storing data for analysis by an on-premises or Azure-hosted service.
125
126Blob storage offers three types of resources:
127
128- The _storage account_ used via `BlobServiceClient`
129- A _container_ in the storage account used via `ContainerClient`
130- A _blob_ in a container used via `BlobClient`
131
132## Examples
133
134- [Import the package](#import-the-package)
135- [Create the blob service client](#create-the-blob-service-client)
136- [Create a new container](#create-a-new-container)
137- [List the containers](#list-the-containers)
138- [Create a blob by uploading data](#create-a-blob-by-uploading-data)
139- [List blobs inside a container](#list-blobs-inside-a-container)
140- [Download a blob and convert it to a string (Node.js)](#download-a-blob-and-convert-it-to-a-string-nodejs)
141- [Download a blob and convert it to a string (Browsers)](#download-a-blob-and-convert-it-to-a-string-browsers)
142
143### Import the package
144
145To use the clients, import the package into your file:
146
147```javascript
148const AzureStorageBlob = require("@azure/storage-blob");
149```
150
151Alternatively, selectively import only the types you need:
152
153```javascript
154const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
155```
156
157### Create the blob service client
158
159The `BlobServiceClient` requires an URL to the blob service and an access credential. It also optionally accepts some settings in the `options` parameter.
160
161#### with `DefaultAzureCredential` from `@azure/identity` package
162
163**Recommended way to instantiate a `BlobServiceClient`**
164
165Setup : Reference - Authorize access to blobs and queues with Azure Active Directory from a client application - https://docs.microsoft.com/azure/storage/common/storage-auth-aad-app
166
167- Register a new AAD application and give permissions to access Azure Storage on behalf of the signed-in user
168
169 - Register a new application in the Azure Active Directory(in the azure-portal) - https://docs.microsoft.com/azure/active-directory/develop/quickstart-register-app
170 - In the `API permissions` section, select `Add a permission` and choose `Microsoft APIs`.
171 - Pick `Azure Storage` and select the checkbox next to `user_impersonation` and then click `Add permissions`. This would allow the application to access Azure Storage on behalf of the signed-in user.
172
173- Grant access to Azure Blob data with RBAC in the Azure Portal
174
175 - RBAC roles for blobs and queues - https://docs.microsoft.com/azure/storage/common/storage-auth-aad-rbac-portal.
176 - In the azure portal, go to your storage-account and assign **Storage Blob Data Contributor** role to the registered AAD application from `Access control (IAM)` tab (in the left-side-navbar of your storage account in the azure-portal).
177
178- Environment setup for the sample
179 - From the overview page of your AAD Application, note down the `CLIENT ID` and `TENANT ID`. In the "Certificates & Secrets" tab, create a secret and note that down.
180 - Make sure you have AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET as environment variables to successfully execute the sample(Can leverage process.env).
181
182```javascript
183const { DefaultAzureCredential } = require("@azure/identity");
184const { BlobServiceClient } = require("@azure/storage-blob");
185
186// Enter your storage account name
187const account = "<account>";
188const defaultAzureCredential = new DefaultAzureCredential();
189
190const blobServiceClient = new BlobServiceClient(
191 `https://${account}.blob.core.windows.net`,
192 defaultAzureCredential
193);
194```
195
196See the [Azure AD Auth sample](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12/javascript/azureAdAuth.js) for a complete example using this method.
197
198[Note - Above steps are only for Node.js]
199
200#### using connection string
201
202Alternatively, you can instantiate a `BlobServiceClient` using the `fromConnectionString()` static method with the full connection string as the argument. (The connection string can be obtained from the azure portal.) [ONLY AVAILABLE IN NODE.JS RUNTIME]
203
204```javascript
205const { BlobServiceClient } = require("@azure/storage-blob");
206
207const connStr = "<connection string>";
208
209const blobServiceClient = BlobServiceClient.fromConnectionString(connStr);
210```
211
212#### with `StorageSharedKeyCredential`
213
214Alternatively, you instantiate a `BlobServiceClient` with a `StorageSharedKeyCredential` by passing account-name and account-key as arguments. (The account-name and account-key can be obtained from the azure portal.)
215[ONLY AVAILABLE IN NODE.JS RUNTIME]
216
217```javascript
218const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
219
220// Enter your storage account name and shared key
221const account = "<account>";
222const accountKey = "<accountkey>";
223
224// Use StorageSharedKeyCredential with storage account and account key
225// StorageSharedKeyCredential is only available in Node.js runtime, not in browsers
226const sharedKeyCredential = new StorageSharedKeyCredential(account, accountKey);
227const blobServiceClient = new BlobServiceClient(
228 `https://${account}.blob.core.windows.net`,
229 sharedKeyCredential
230);
231```
232
233#### with SAS Token
234
235Also, You can instantiate a `BlobServiceClient` with a shared access signatures (SAS). You can get the SAS token from the Azure Portal or generate one using `generateAccountSASQueryParameters()`.
236
237```javascript
238const { BlobServiceClient } = require("@azure/storage-blob");
239
240const account = "<account name>";
241const sas = "<service Shared Access Signature Token>";
242
243const blobServiceClient = new BlobServiceClient(`https://${account}.blob.core.windows.net${sas}`);
244```
245
246### Create a new container
247
248Use `BlobServiceClient.getContainerClient()` to get a container client instance then create a new container resource.
249
250```javascript
251const { DefaultAzureCredential } = require("@azure/identity");
252const { BlobServiceClient } = require("@azure/storage-blob");
253
254const account = "<account>";
255const defaultAzureCredential = new DefaultAzureCredential();
256
257const blobServiceClient = new BlobServiceClient(
258 `https://${account}.blob.core.windows.net`,
259 defaultAzureCredential
260);
261
262async function main() {
263 // Create a container
264 const containerName = `newcontainer${new Date().getTime()}`;
265 const containerClient = blobServiceClient.getContainerClient(containerName);
266 const createContainerResponse = await containerClient.create();
267 console.log(`Create container ${containerName} successfully`, createContainerResponse.requestId);
268}
269
270main();
271```
272
273### List the containers
274
275Use `BlobServiceClient.listContainers()` function to iterate the containers,
276with the new `for-await-of` syntax:
277
278```javascript
279const { DefaultAzureCredential } = require("@azure/identity");
280const { BlobServiceClient } = require("@azure/storage-blob");
281
282const account = "<account>";
283const defaultAzureCredential = new DefaultAzureCredential();
284
285const blobServiceClient = new BlobServiceClient(
286 `https://${account}.blob.core.windows.net`,
287 defaultAzureCredential
288);
289
290async function main() {
291 let i = 1;
292 let containers = blobServiceClient.listContainers();
293 for await (const container of containers) {
294 console.log(`Container ${i++}: ${container.name}`);
295 }
296}
297
298main();
299```
300
301Alternatively without using `for-await-of`:
302
303```javascript
304const { DefaultAzureCredential } = require("@azure/identity");
305const { BlobServiceClient } = require("@azure/storage-blob");
306
307const account = "<account>";
308const defaultAzureCredential = new DefaultAzureCredential();
309
310const blobServiceClient = new BlobServiceClient(
311 `https://${account}.blob.core.windows.net`,
312 defaultAzureCredential
313);
314
315async function main() {
316 let i = 1;
317 let iter = blobServiceClient.listContainers();
318 let containerItem = await iter.next();
319 while (!containerItem.done) {
320 console.log(`Container ${i++}: ${containerItem.value.name}`);
321 containerItem = await iter.next();
322 }
323}
324
325main();
326```
327
328In addition, pagination is supported for listing too via `byPage()`:
329
330```javascript
331const { DefaultAzureCredential } = require("@azure/identity");
332const { BlobServiceClient } = require("@azure/storage-blob");
333
334const account = "<account>";
335const defaultAzureCredential = new DefaultAzureCredential();
336
337const blobServiceClient = new BlobServiceClient(
338 `https://${account}.blob.core.windows.net`,
339 defaultAzureCredential
340);
341
342async function main() {
343 let i = 1;
344 for await (const response of blobServiceClient.listContainers().byPage({ maxPageSize: 20 })) {
345 if (response.containerItems) {
346 for (const container of response.containerItems) {
347 console.log(`Container ${i++}: ${container.name}`);
348 }
349 }
350 }
351}
352
353main();
354```
355
356For a complete sample on iterating containers please see [samples/v12/typescript/src/listContainers.ts](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12/typescript/src/listContainers.ts).
357
358### Create a blob by uploading data
359
360```javascript
361const { DefaultAzureCredential } = require("@azure/identity");
362const { BlobServiceClient } = require("@azure/storage-blob");
363
364const account = "<account>";
365const defaultAzureCredential = new DefaultAzureCredential();
366
367const blobServiceClient = new BlobServiceClient(
368 `https://${account}.blob.core.windows.net`,
369 defaultAzureCredential
370);
371
372const containerName = "<container name>";
373
374async function main() {
375 const containerClient = blobServiceClient.getContainerClient(containerName);
376
377 const content = "Hello world!";
378 const blobName = "newblob" + new Date().getTime();
379 const blockBlobClient = containerClient.getBlockBlobClient(blobName);
380 const uploadBlobResponse = await blockBlobClient.upload(content, content.length);
381 console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);
382}
383
384main();
385```
386
387### List blobs inside a container
388
389Similar to listing containers.
390
391```javascript
392const { DefaultAzureCredential } = require("@azure/identity");
393const { BlobServiceClient } = require("@azure/storage-blob");
394
395const account = "<account>";
396const defaultAzureCredential = new DefaultAzureCredential();
397
398const blobServiceClient = new BlobServiceClient(
399 `https://${account}.blob.core.windows.net`,
400 defaultAzureCredential
401);
402
403const containerName = "<container name>";
404
405async function main() {
406 const containerClient = blobServiceClient.getContainerClient(containerName);
407
408 let i = 1;
409 let blobs = containerClient.listBlobsFlat();
410 for await (const blob of blobs) {
411 console.log(`Blob ${i++}: ${blob.name}`);
412 }
413}
414
415main();
416```
417
418For a complete sample on iterating blobs please see [samples/v12/typescript/src/listBlobsFlat.ts](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12/typescript/src/listBlobsFlat.ts).
419
420### Download a blob and convert it to a string (Node.js)
421
422```javascript
423const { DefaultAzureCredential } = require("@azure/identity");
424const { BlobServiceClient } = require("@azure/storage-blob");
425
426const account = "<account>";
427const defaultAzureCredential = new DefaultAzureCredential();
428
429const blobServiceClient = new BlobServiceClient(
430 `https://${account}.blob.core.windows.net`,
431 defaultAzureCredential
432);
433
434const containerName = "<container name>";
435const blobName = "<blob name>";
436
437async function main() {
438 const containerClient = blobServiceClient.getContainerClient(containerName);
439 const blobClient = containerClient.getBlobClient(blobName);
440
441 // Get blob content from position 0 to the end
442 // In Node.js, get downloaded data by accessing downloadBlockBlobResponse.readableStreamBody
443 const downloadBlockBlobResponse = await blobClient.download();
444 const downloaded = (
445 await streamToBuffer(downloadBlockBlobResponse.readableStreamBody)
446 ).toString();
447 console.log("Downloaded blob content:", downloaded);
448
449 // [Node.js only] A helper method used to read a Node.js readable stream into a Buffer
450 async function streamToBuffer(readableStream) {
451 return new Promise((resolve, reject) => {
452 const chunks = [];
453 readableStream.on("data", (data) => {
454 chunks.push(data instanceof Buffer ? data : Buffer.from(data));
455 });
456 readableStream.on("end", () => {
457 resolve(Buffer.concat(chunks));
458 });
459 readableStream.on("error", reject);
460 });
461 }
462}
463
464main();
465```
466
467### Download a blob and convert it to a string (Browsers).
468
469Please refer to the [JavaScript Bundle](#javascript-bundle) section for more information on using this library in the browser.
470
471```javascript
472const { BlobServiceClient } = require("@azure/storage-blob");
473
474const account = "<account name>";
475const sas = "<service Shared Access Signature Token>";
476const containerName = "<container name>";
477const blobName = "<blob name>";
478
479const blobServiceClient = new BlobServiceClient(`https://${account}.blob.core.windows.net${sas}`);
480
481async function main() {
482 const containerClient = blobServiceClient.getContainerClient(containerName);
483 const blobClient = containerClient.getBlobClient(blobName);
484
485 // Get blob content from position 0 to the end
486 // In browsers, get downloaded data by accessing downloadBlockBlobResponse.blobBody
487 const downloadBlockBlobResponse = await blobClient.download();
488 const downloaded = await blobToString(await downloadBlockBlobResponse.blobBody);
489 console.log("Downloaded blob content", downloaded);
490
491 // [Browsers only] A helper method used to convert a browser Blob into string.
492 async function blobToString(blob) {
493 const fileReader = new FileReader();
494 return new Promise((resolve, reject) => {
495 fileReader.onloadend = (ev) => {
496 resolve(ev.target.result);
497 };
498 fileReader.onerror = reject;
499 fileReader.readAsText(blob);
500 });
501 }
502}
503
504main();
505```
506
507A complete example of simple scenarios is at [samples/v12/typescript/src/sharedKeyAuth.ts](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12/typescript/src/sharedKeyAuth.ts).
508
509## Troubleshooting
510
511Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`:
512
513```javascript
514const { setLogLevel } = require("@azure/logger");
515
516setLogLevel("info");
517```
518
519## Next steps
520
521More code samples:
522
523- [Blob Storage Samples (JavaScript)](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/storage/storage-blob/samples/v12/javascript)
524- [Blob Storage Samples (TypeScript)](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/storage/storage-blob/samples/v12/typescript)
525- [Blob Storage Test Cases](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/storage/storage-blob/test/)
526
527## Contributing
528
529If you'd like to contribute to this library, please read the [contributing guide](https://github.com/Azure/azure-sdk-for-js/blob/main/CONTRIBUTING.md) to learn more about how to build and test the code.
530
531Also refer to [Storage specific guide](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/CONTRIBUTING.md) for additional information on setting up the test environment for storage libraries.
532
533![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js%2Fsdk%2Fstorage%2Fstorage-blob%2FREADME.png)