Skip to content

Commit d8dc375

Browse files
authored
Merge pull request #203843 from diberry/diberry/0705-blob-sas
Blob Storage - JS - Sas tokens
2 parents 76cbd00 + 1670154 commit d8dc375

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

articles/storage/blobs/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,8 @@ items:
594594
items:
595595
- name: Get started
596596
href: storage-blob-javascript-get-started.md
597+
- name: Work with SAS tokens
598+
href: storage-blob-account-delegation-sas-create-javascript.md
597599
- name: Work with containers
598600
items:
599601
- name: Create a container
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
title: Create account SAS tokens - JavaScript
3+
titleSuffix: Azure Storage
4+
description: Create and use account SAS tokens in a JavaScript application that works with Azure Blob Storage. This article helps you set up a project and authorizes access to an Azure Blob Storage endpoint.
5+
services: storage
6+
author: normesta
7+
8+
ms.service: storage
9+
ms.topic: how-to
10+
ms.date: 07/05/2022
11+
ms.author: normesta
12+
ms.subservice: blobs
13+
ms.custom: template-how-to, devx-track-js
14+
---
15+
16+
# Create and use account SAS tokens with Azure Blob Storage and JavaScript
17+
18+
This article shows you how to create and use account SAS tokens to use the Azure Blob Storage client library v12 for JavaScript. Once connected, your code can operate on containers, blobs, and features of the Blob Storage service.
19+
20+
The [sample code snippets](https://github.com/Azure-Samples/AzureStorageSnippets/tree/master/blobs/howto/JavaScript/NodeJS-v12/dev-guide) are available in GitHub as runnable Node.js files.
21+
22+
[Package (npm)](https://www.npmjs.com/package/@azure/storage-blob) | [Samples](../common/storage-samples-javascript.md?toc=%2fazure%2fstorage%2fblobs%2ftoc.json#blob-samples) | [API reference](/javascript/api/preview-docs/@azure/storage-blob) | [Library source code](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/storage/storage-blob) | [Give Feedback](https://github.com/Azure/azure-sdk-for-js/issues)
23+
24+
## Account SAS tokens
25+
26+
An **account SAS token** is one [type of SAS token](../common/storage-sas-overview.md?toc=%2Fazure%2Fstorage%2Fblobs%2Ftoc.json#types-of-shared-access-signatures) for access delegation provided by Azure Storage. An account SAS token provides access to Azure Storage. The token is only as restrictive as you define it when creating it. Because anyone with the token can use it to access your Storage account, you should define the token with the most restrictive permissions that still allow the token to complete the required tasks.
27+
28+
[Best practices for token](../common/storage-sas-overview.md#best-practices-when-using-sas) creation include limiting permissions:
29+
30+
* Services: blob, file, queue, table
31+
* Resource types: service, container, or object
32+
* Permissions such as create, read, write, update, and delete
33+
34+
## Add required dependencies to your application
35+
36+
Include the required dependencies to create an account SAS token.
37+
38+
:::code language="javascript" source="~/azure_storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/create-account-sas.js" id="Snippet_Dependencies":::
39+
40+
## Get environment variables to create shared key credential
41+
42+
Use the Blob Storage account name and key to create a [StorageSharedKeyCredential](/javascript/api/@azure/storage-blob/storagesharedkeycredential). This key is required to create the SAS token and to use the SAS token.
43+
44+
Create a [StorageSharedKeyCredential](/javascript/api/@azure/storage-blob/storagesharedkeycredential) by using the storage account name and account key. Then use the StorageSharedKeyCredential to initialize a [BlobServiceClient](/javascript/api/@azure/storage-blob/blobserviceclient).
45+
46+
:::code language="javascript" source="~/azure_storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/create-account-sas.js" id="Snippet_EnvironmentVariables":::
47+
48+
## Async operation boilerplate
49+
50+
The remaining sample code snippets assume the following async boilerplate code for Node.js.
51+
52+
:::code language="javascript" source="~/azure_storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/create-account-sas.js" id="Snippet_AsyncBoilerplate":::
53+
54+
## Create SAS token
55+
56+
Because this token can be used with blobs, queues, tables, and files, some of the settings are more broad than just blob options.
57+
58+
1. Create the options object.
59+
60+
The scope of the abilities of a SAS token is defined by the [AccountSASSignatureValues](/javascript/api/@azure/storage-blob/accountsassignaturevalues).
61+
62+
Use the following helper functions provided by the SDK to create the correct value types for the values:
63+
64+
* [AccountSASServices.parse("btqf").toString()](/javascript/api/@azure/storage-blob/accountsasservices):
65+
* b: blob
66+
* t: table
67+
* q: query
68+
* f: file
69+
* [resourceTypes: AccountSASResourceTypes.parse("sco").toString()](/javascript/api/@azure/storage-blob/accountsasresourcetypes)
70+
* s: service
71+
* c: container - such as blob container, table or queue
72+
* o: object - blob, row, message
73+
* [permissions: AccountSASPermissions.parse("rwdlacupi")](/javascript/api/@azure/storage-blob/accountsaspermissions)
74+
* r: read
75+
* w: write
76+
* d: delete
77+
* l: list
78+
* f: filter
79+
* a: add
80+
* c: create
81+
* u: update
82+
* t: tag access
83+
* p: process - such as process messages in a queue
84+
* i: set immutability policy
85+
86+
1. Pass the object to the [generateAccountSASQueryParameters](/javascript/api/@azure/storage-blob/#@azure-storage-blob-generateaccountsasqueryparameters) function, along with the [SharedKeyCredential](/javascript/api/@azure/storage-blob/#@azure-storage-blob-generateaccountsasqueryparameters), to create the SAS token.
87+
88+
Before returning the SAS token, prepend the query string delimiter, `?`.
89+
90+
:::code language="javascript" source="~/azure_storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/create-account-sas.js" id="Snippet_GetSas":::
91+
92+
1. Secure the SAS token until it is used.
93+
94+
## Use Blob service with account SAS token
95+
96+
To use the account SAS token, you need to combine it with the account name to create the URI. Pass the URI to create the blobServiceClient. Once you have the blobServiceClient, you can use that client to access your Blob service.
97+
98+
:::code language="javascript" source="~/azure_storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/create-account-sas.js" id="Snippet_UseSas":::
99+
100+
101+
## See also
102+
103+
- [Types of SAS tokens](../common/storage-sas-overview.md?toc=%2Fazure%2Fstorage%2Fblobs%2Ftoc.json)
104+
- [How a shared access signature works](../common/storage-sas-overview.md?toc=%2Fazure%2Fstorage%2Fblobs%2Ftoc.json#how-a-shared-access-signature-works)
105+
- [API reference](/javascript/api/@azure/storage-blob/)
106+
- [Library source code](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob)
107+
- [Give Feedback](https://github.com/Azure/azure-sdk-for-js/issues)

0 commit comments

Comments
 (0)