Skip to content

Commit 4d7de06

Browse files
Add articles for JS and Python samples
1 parent 9c77e95 commit 4d7de06

File tree

6 files changed

+269
-188
lines changed

6 files changed

+269
-188
lines changed

articles/storage/blobs/TOC.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,14 +1131,16 @@ items:
11311131
href: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage
11321132
- name: Azure Storage client library version 2.1
11331133
href: https://github.com/Azure/azure-storage-python
1134-
- name: Code examples for deprecated SDKs
1134+
- name: Code samples using deprecated SDKs
11351135
items:
11361136
- name: .NET version 11.x
1137-
items:
1138-
- name: Azure Storage client library code examples
1139-
href: blob-v11-samples-dotnet.md
1140-
- name: Design highly available applications (.NET version 11.x)
1141-
href: ../common/geo-redundant-design-legacy.md?toc=/azure/storage/blobs/toc.json&bc=/azure/storage/blobs/breadcrumb/toc.json
1137+
href: blob-v11-samples-dotnet.md
1138+
- name: JavaScript version 11.x
1139+
href: blob-v11-samples-javascript.md
1140+
- name: Python version 2.1
1141+
href: blob-v2-samples-python.md
1142+
- name: Design highly available applications (.NET version 11.x)
1143+
href: ../common/geo-redundant-design-legacy.md?toc=/azure/storage/blobs/toc.json&bc=/azure/storage/blobs/breadcrumb/toc.json
11421144
- name: Compliance offerings
11431145
href: ../common/storage-compliance-offerings.md?toc=/azure/storage/blobs/toc.json&bc=/azure/storage/blobs/breadcrumb/toc.json
11441146
- name: Data Lake Storage Gen2

articles/storage/blobs/blob-v11-samples-dotnet.md

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: Azure Blob Storage client library for .NET version 11.x code examples
2+
title: Azure Blob Storage code samples using .NET version 11.x client libraries
33
titleSuffix: Azure Storage
4-
description: View code examples that use the Azure Blob Storage client library for .NET version 11.x.
4+
description: View code samples that use the Azure Blob Storage client library for .NET version 11.x.
55
services: storage
66
author: pauljewellmsft
77
ms.service: storage
@@ -11,13 +11,11 @@ ms.date: 04/03/2023
1111
ms.author: pauljewell
1212
---
1313

14-
# Azure Blob Storage client library for .NET version 11.x code examples
14+
# Azure Blob Storage code samples using .NET version 11.x client libraries
1515

16-
This article shows code examples that use version 11.x of the Azure Blob Storage client library for .NET.
16+
This article shows code samples that use version 11.x of the Azure Blob Storage client library for .NET.
1717

18-
On 31 March 2023, we retired support for Azure SDK libraries which do not conform to our [current Azure SDK guidelines](https://azure.github.io/azure-sdk/general_introduction.html). The new Azure SDK libraries are updated regularly to drive consistent experiences and strengthen your security posture. Microsoft recommends that you transition to the new Azure SDK libraries to take advantage of the new capabilities and critical security updates.
19-
20-
Although the older libraries can still be used beyond 31 March 2023, they will no longer receive official support and updates from Microsoft. For more information, see [this announcement](https://azure.microsoft.com/updates/support-for-azure-sdk-libraries-that-do-not-conform-to-our-current-azure-sdk-guidelines-will-be-retired-as-of-31-march-2023/).
18+
[!INCLUDE [storage-v11-sdk-support-retirement](../../../includes/storage-v11-sdk-support-retirement.md)]
2119

2220
## Create a snapshot
2321

@@ -409,14 +407,37 @@ public void DemonstratePessimisticConcurrencyBlob(string containerName, string b
409407

410408
## Build a highly available app with Blob Storage
411409

412-
Related article: [Tutorial: Build a highly available application with Blob storage](storage-create-geo-redundant-storage.md)
413-
Full code sample: Use [git](https://git-scm.com/) to download a copy of the application to your development environment. The sample project in the v11 folder contains a console application.
410+
Related article: [Tutorial: Build a highly available application with Blob storage](storage-create-geo-redundant-storage.md).
411+
412+
### Download the sample
413+
414+
Download the [sample project](https://github.com/Azure-Samples/storage-dotnet-circuit-breaker-pattern-ha-apps-using-ra-grs/archive/master.zip), extract (unzip) the storage-dotnet-circuit-breaker-pattern-ha-apps-using-ra-grs.zip file, then navigate to the **v11** folder to find the project files.
415+
416+
You can also use [git](https://git-scm.com/) to download a copy of the application to your development environment. The sample project in the v11 folder contains a console application.
414417

415418
```bash
416419
git clone https://github.com/Azure-Samples/storage-dotnet-circuit-breaker-pattern-ha-apps-using-ra-grs.git
417420
```
418421

419-
### Retry event handler
422+
### Configure the sample
423+
424+
In the application, you must provide the connection string for your storage account. You can store this connection string within an environment variable on the local machine running the application. Follow one of the examples below depending on your Operating System to create the environment variable.
425+
426+
In the Azure portal, navigate to your storage account. Select **Access keys** under **Settings** in your storage account. Copy the **connection string** from the primary or secondary key. Run one of the following commands based on your operating system, replacing \<yourconnectionstring\> with your actual connection string. This command saves an environment variable to the local machine. In Windows, the environment variable isn't available until you reload the **Command Prompt** or shell you're using.
427+
428+
### Run the console application
429+
430+
In Visual Studio, press **F5** or select **Start** to begin debugging the application. Visual Studio automatically restores missing NuGet packages if package restore is configured, visit [Installing and reinstalling packages with package restore](/nuget/consume-packages/package-restore#package-restore-overview) to learn more.
431+
432+
A console window launches and the application begins running. The application uploads the **HelloWorld.png** image from the solution to the storage account. The application checks to ensure the image has replicated to the secondary RA-GZRS endpoint. It then begins downloading the image up to 999 times. Each read is represented by a **P** or an **S**. Where **P** represents the primary endpoint and **S** represents the secondary endpoint.
433+
434+
![Screenshot of Console application output.](media/storage-create-geo-redundant-storage/figure3.png)
435+
436+
In the sample code, the `RunCircuitBreakerAsync` task in the `Program.cs` file is used to download an image from the storage account using the [DownloadToFileAsync](/dotnet/api/microsoft.azure.storage.blob.cloudblob.downloadtofileasync) method. Prior to the download, an [OperationContext](/dotnet/api/microsoft.azure.cosmos.table.operationcontext) is defined. The operation context defines event handlers that fire when a download completes successfully, or if a download fails and is retrying.
437+
438+
### Understand the sample code
439+
440+
#### Retry event handler
420441

421442
The `OperationContextRetrying` event handler is called when the download of the image fails and is set to retry. If the maximum number of retries defined in the application are reached, the [LocationMode](/dotnet/api/microsoft.azure.storage.blob.blobrequestoptions.locationmode) of the request is changed to `SecondaryOnly`. This setting forces the application to attempt to download the image from the secondary endpoint. This configuration reduces the time taken to request the image as the primary endpoint isn't retried indefinitely.
422443

@@ -444,7 +465,7 @@ private static void OperationContextRetrying(object sender, RequestEventArgs e)
444465
}
445466
```
446467

447-
### Request completed event handler
468+
#### Request completed event handler
448469

449470
The `OperationContextRequestCompleted` event handler is called when the download of the image is successful. If the application is using the secondary endpoint, the application continues to use this endpoint up to 20 times. After 20 times, the application sets the [LocationMode](/dotnet/api/microsoft.azure.storage.blob.blobrequestoptions.locationmode) back to `PrimaryThenSecondary` and retries the primary endpoint. If a request is successful, the application continues to read from the primary endpoint.
450471

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: Azure Blob Storage code samples using JavaScript version 11.x client libraries
3+
titleSuffix: Azure Storage
4+
description: View code samples that use the Azure Blob Storage client library for JavaScript version 11.x.
5+
services: storage
6+
author: pauljewellmsft
7+
ms.service: storage
8+
ms.subservice: blobs
9+
ms.topic: how-to
10+
ms.date: 04/03/2023
11+
ms.author: pauljewell
12+
---
13+
14+
# Azure Blob Storage code samples using JavaScript version 11.x client libraries
15+
16+
This article shows code samples that use version 11.x of the Azure Blob Storage client library for JavaScript.
17+
18+
[!INCLUDE [storage-v11-sdk-support-retirement](../../../includes/storage-v11-sdk-support-retirement.md)]
19+
20+
## Build a highly available app with Blob Storage
21+
22+
Related article: [Tutorial: Build a highly available application with Blob storage](storage-create-geo-redundant-storage.md)
23+
24+
### Download the sample
25+
26+
[Download the sample project](https://github.com/Azure-Samples/storage-node-v10-ha-ra-grs) and unzip the file. You can also use [git](https://git-scm.com/) to download a copy of the application to your development environment. The sample project contains a basic Node.js application.
27+
28+
```bash
29+
git clone https://github.com/Azure-Samples/storage-node-v10-ha-ra-grs.git
30+
```
31+
32+
### Configure the sample
33+
34+
To run this sample, you must add your storage account credentials to the `.env.example` file and then rename it to `.env`.
35+
36+
```
37+
AZURE_STORAGE_ACCOUNT_NAME=<replace with your storage account name>
38+
AZURE_STORAGE_ACCOUNT_ACCESS_KEY=<replace with your storage account access key>
39+
```
40+
41+
You can find this information in the Azure portal by navigating to your storage account and selecting **Access keys** in the **Settings** section.
42+
43+
Install the required dependencies by opening a command prompt, navigating to the sample folder, then entering `npm install`.
44+
45+
### Run the console application
46+
47+
To run the sample, open a command prompt, navigate to the sample folder, then enter `node index.js`.
48+
49+
The sample creates a container in your Blob storage account, uploads **HelloWorld.png** into the container, then repeatedly checks whether the container and image have replicated to the secondary region. After replication, it prompts you to enter **D** or **Q** (followed by ENTER) to download or quit. Your output should look similar to the following example:
50+
51+
```
52+
Created container successfully: newcontainer1550799840726
53+
Uploaded blob: HelloWorld.png
54+
Checking to see if container and blob have replicated to secondary region.
55+
[0] Container has not replicated to secondary region yet: newcontainer1550799840726 : ContainerNotFound
56+
[1] Container has not replicated to secondary region yet: newcontainer1550799840726 : ContainerNotFound
57+
...
58+
[31] Container has not replicated to secondary region yet: newcontainer1550799840726 : ContainerNotFound
59+
[32] Container found, but blob has not replicated to secondary region yet.
60+
...
61+
[67] Container found, but blob has not replicated to secondary region yet.
62+
[68] Blob has replicated to secondary region.
63+
Ready for blob download. Enter (D) to download or (Q) to quit, followed by ENTER.
64+
> D
65+
Attempting to download blob...
66+
Blob downloaded from primary endpoint.
67+
> Q
68+
Exiting...
69+
Deleted container newcontainer1550799840726
70+
```
71+
72+
### Understand the code sample
73+
74+
With the Node.js V10 SDK, callback handlers are unnecessary. Instead, the sample creates a pipeline configured with retry options and a secondary endpoint. This configuration allows the application to automatically switch to the secondary pipeline if it fails to reach your data through the primary pipeline.
75+
76+
```javascript
77+
const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME;
78+
const storageAccessKey = process.env.AZURE_STORAGE_ACCOUNT_ACCESS_KEY;
79+
const sharedKeyCredential = new SharedKeyCredential(accountName, storageAccessKey);
80+
81+
const primaryAccountURL = `https://${accountName}.blob.core.windows.net`;
82+
const secondaryAccountURL = `https://${accountName}-secondary.blob.core.windows.net`;
83+
84+
const pipeline = StorageURL.newPipeline(sharedKeyCredential, {
85+
retryOptions: {
86+
maxTries: 3,
87+
tryTimeoutInMs: 10000,
88+
retryDelayInMs: 500,
89+
maxRetryDelayInMs: 1000,
90+
secondaryHost: secondaryAccountURL
91+
}
92+
});
93+
```
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
title: Azure Blob Storage code samples using Python version 2.1 client libraries
3+
titleSuffix: Azure Storage
4+
description: View code samples that use the Azure Blob Storage client library for Python version 2.1.
5+
services: storage
6+
author: pauljewellmsft
7+
ms.service: storage
8+
ms.subservice: blobs
9+
ms.topic: how-to
10+
ms.date: 04/03/2023
11+
ms.author: pauljewell
12+
---
13+
14+
# Azure Blob Storage code samples using Python version 2.1 client libraries
15+
16+
This article shows code samples that use version 2.1 of the Azure Blob Storage client library for Python.
17+
18+
[!INCLUDE [storage-v11-sdk-support-retirement](../../../includes/storage-v11-sdk-support-retirement.md)]
19+
20+
## Build a highly available app with Blob Storage
21+
22+
Related article: [Tutorial: Build a highly available application with Blob storage](storage-create-geo-redundant-storage.md)
23+
24+
### Download the sample
25+
26+
[Download the sample project](https://github.com/Azure-Samples/storage-python-circuit-breaker-pattern-ha-apps-using-ra-grs/archive/master.zip) and extract (unzip) the storage-python-circuit-breaker-pattern-ha-apps-using-ra-grs.zip file. You can also use [git](https://git-scm.com/) to download a copy of the application to your development environment. The sample project contains a basic Python application.
27+
28+
```bash
29+
git clone https://github.com/Azure-Samples/storage-python-circuit-breaker-pattern-ha-apps-using-ra-grs.git
30+
```
31+
32+
### Configure the sample
33+
34+
In the application, you must provide your storage account credentials. You can store this information in environment variables on the local machine running the application. Follow one of the examples below depending on your Operating System to create the environment variables.
35+
36+
In the Azure portal, navigate to your storage account. Select **Access keys** under **Settings** in your storage account. Paste the **Storage account name** and **Key** values into the following commands, replacing the \<youraccountname\> and \<youraccountkey\> placeholders. This command saves the environment variables to the local machine. In Windows, the environment variable isn't available until you reload the **Command Prompt** or shell you're using.
37+
38+
#### Linux
39+
40+
```bash
41+
export accountname=<youraccountname>
42+
export accountkey=<youraccountkey>
43+
```
44+
45+
#### Windows
46+
47+
```powershell
48+
setx accountname "<youraccountname>"
49+
setx accountkey "<youraccountkey>"
50+
```
51+
52+
### Run the console application
53+
54+
To run the application on a terminal or command prompt, go to the **circuitbreaker.py** directory, then enter `python circuitbreaker.py`. The application uploads the **HelloWorld.png** image from the solution to the storage account. The application checks to ensure the image has replicated to the secondary RA-GZRS endpoint. It then begins downloading the image up to 999 times. Each read is represented by a **P** or an **S**. Where **P** represents the primary endpoint and **S** represents the secondary endpoint.
55+
56+
![Console app running](media/storage-create-geo-redundant-storage/figure3.png)
57+
58+
In the sample code, the `run_circuit_breaker` method in the `circuitbreaker.py` file is used to download an image from the storage account using the [get_blob_to_path](/python/api/azure-storage-blob/azure.storage.blob.baseblobservice.baseblobservice#get-blob-to-path-container-name--blob-name--file-path--open-mode--wb---snapshot-none--start-range-none--end-range-none--validate-content-false--progress-callback-none--max-connections-2--lease-id-none--if-modified-since-none--if-unmodified-since-none--if-match-none--if-none-match-none--timeout-none-) method.
59+
60+
The Storage object retry function is set to a linear retry policy. The retry function determines whether to retry a request, and specifies the number of seconds to wait before retrying the request. Set the **retry\_to\_secondary** value to true, if request should be retried to secondary in case the initial request to primary fails. In the sample application, a custom retry policy is defined in the `retry_callback` function of the storage object.
61+
62+
Before the download, the Service object [retry_callback](/python/api/azure-storage-common/azure.storage.common.storageclient.storageclient) and [response_callback](/python/api/azure-storage-common/azure.storage.common.storageclient.storageclient) function is defined. These functions define event handlers that fire when a download completes successfully or if a download fails and is retrying.
63+
64+
### Understand the code sample
65+
66+
#### Retry event handler
67+
68+
The `retry_callback` event handler is called when the download of the image fails and is set to retry. If the maximum number of retries defined in the application are reached, the [LocationMode](/python/api/azure-storage-common/azure.storage.common.models.locationmode) of the request is changed to `SECONDARY`. This setting forces the application to attempt to download the image from the secondary endpoint. This configuration reduces the time taken to request the image as the primary endpoint isn't retried indefinitely.
69+
70+
```python
71+
def retry_callback(retry_context):
72+
global retry_count
73+
retry_count = retry_context.count
74+
sys.stdout.write(
75+
"\nRetrying event because of failure reading the primary. RetryCount= {0}".format(retry_count))
76+
sys.stdout.flush()
77+
78+
# Check if we have more than n-retries in which case switch to secondary
79+
if retry_count >= retry_threshold:
80+
81+
# Check to see if we can fail over to secondary.
82+
if blob_client.location_mode != LocationMode.SECONDARY:
83+
blob_client.location_mode = LocationMode.SECONDARY
84+
retry_count = 0
85+
else:
86+
raise Exception("Both primary and secondary are unreachable. "
87+
"Check your application's network connection.")
88+
```
89+
90+
#### Request completed event handler
91+
92+
The `response_callback` event handler is called when the download of the image is successful. If the application is using the secondary endpoint, the application continues to use this endpoint up to 20 times. After 20 times, the application sets the [LocationMode](/python/api/azure-storage-common/azure.storage.common.models.locationmode) back to `PRIMARY` and retries the primary endpoint. If a request is successful, the application continues to read from the primary endpoint.
93+
94+
```python
95+
def response_callback(response):
96+
global secondary_read_count
97+
if blob_client.location_mode == LocationMode.SECONDARY:
98+
99+
# You're reading the secondary. Let it read the secondary [secondaryThreshold] times,
100+
# then switch back to the primary and see if it is available now.
101+
secondary_read_count += 1
102+
if secondary_read_count >= secondary_threshold:
103+
blob_client.location_mode = LocationMode.PRIMARY
104+
secondary_read_count = 0
105+
```

0 commit comments

Comments
 (0)