Skip to content

Commit 2bb3cb8

Browse files
committed
add .net example
1 parent 05aad09 commit 2bb3cb8

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

articles/storage/blobs/versioning-enable.md

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: tamram
77

88
ms.service: storage
99
ms.topic: how-to
10-
ms.date: 04/30/2020
10+
ms.date: 05/05/2020
1111
ms.author: tamram
1212
ms.subservice: blobs
1313
---
@@ -64,7 +64,89 @@ For more information about deploying resources with templates in the Azure porta
6464

6565
## Modify a blob to trigger a new version
6666

67-
.NET example here
67+
The following code example shows how to trigger the creation of a new version with the Azure Storage client library for .NET version 12. Before running this example, make sure you have enabled versioning for your storage account.
68+
69+
The example creates a block blob, and then updates the blob's metadata. Updating the blob's metadata triggers the creation of a new version. The example retrieves the initial version and the current version, and shows that only the current version includes the metadata.
70+
71+
```csharp
72+
public static async Task UpdateVersionedBlobMetadata(string containerName, string blobName)
73+
{
74+
// Create a new service client from the connection string.
75+
BlobServiceClient blobServiceClient = new BlobServiceClient(ConnectionString);
76+
77+
// Create a new container client.
78+
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
79+
80+
try
81+
{
82+
// Create the container.
83+
await containerClient.CreateIfNotExistsAsync();
84+
85+
// Upload a block blob.
86+
BlockBlobClient blockBlobClient = containerClient.GetBlockBlobClient(blobName);
87+
88+
string blobContents = string.Format("Block blob created at {0}.", DateTime.Now);
89+
byte[] byteArray = Encoding.ASCII.GetBytes(blobContents);
90+
91+
string initalVersionId;
92+
using (MemoryStream stream = new MemoryStream(byteArray))
93+
{
94+
Response<BlobContentInfo> uploadResponse = await blockBlobClient.UploadAsync(stream, null, default);
95+
96+
// Get the version ID for the current version.
97+
initalVersionId = uploadResponse.Value.VersionId;
98+
}
99+
100+
// Update the blob's metadata to trigger the creation of a new version.
101+
Dictionary<string, string> metadata = new Dictionary<string, string>
102+
{
103+
{ "key", "value" },
104+
{ "key1", "value1" }
105+
};
106+
107+
Response<BlobInfo> metadataResponse = await blockBlobClient.SetMetadataAsync(metadata);
108+
109+
// Get the version ID for the new current version.
110+
string newVersionId = metadataResponse.Value.VersionId;
111+
112+
// Request metadata on the previous version.
113+
BlockBlobClient initalVersionBlob = blockBlobClient.WithVersion(initalVersionId);
114+
Response<BlobProperties> propertiesResponse = await initalVersionBlob.GetPropertiesAsync();
115+
PrintMetadata(propertiesResponse);
116+
117+
// Request metadata on the current version.
118+
BlockBlobClient newVersionBlob = blockBlobClient.WithVersion(newVersionId);
119+
Response<BlobProperties> newPropertiesResponse = await newVersionBlob.GetPropertiesAsync();
120+
PrintMetadata(newPropertiesResponse);
121+
}
122+
catch (RequestFailedException e)
123+
{
124+
Console.WriteLine(e.Message);
125+
Console.ReadLine();
126+
throw;
127+
}
128+
finally
129+
{
130+
await containerClient.DeleteAsync();
131+
}
132+
}
133+
134+
static void PrintMetadata(Response<BlobProperties> propertiesResponse)
135+
{
136+
if (propertiesResponse.Value.Metadata.Count > 0)
137+
{
138+
Console.WriteLine("Metadata values for version {0}:", propertiesResponse.Value.VersionId);
139+
foreach (var item in propertiesResponse.Value.Metadata)
140+
{
141+
Console.WriteLine("Key:{0} Value:{1}", item.Key, item.Value);
142+
}
143+
}
144+
else
145+
{
146+
Console.WriteLine("Version {0} has no metadata.", propertiesResponse.Value.VersionId);
147+
}
148+
}
149+
```
68150

69151
## Next steps
70152

articles/storage/blobs/versioning-overview.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: tamram
77

88
ms.service: storage
99
ms.topic: conceptual
10-
ms.date: 04/30/2020
10+
ms.date: 05/05/2020
1111
ms.author: tamram
1212
ms.subservice: blobs
1313
---
@@ -240,7 +240,7 @@ In scenario 1, the blob has a prior version. The blob has not been updated since
240240

241241
#### Scenario 2
242242

243-
In scenario 2, one block (block 3 in the diagram) in the blob has been updated. Even though the updated block contains the same data and the same ID, it is not the same as block 3 in the previous version. As a result, the account is charged for four blocks. ???here, the blob shown in #1 should have two versions - do we want to show that here?
243+
In scenario 2, one block (block 3 in the diagram) in the blob has been updated. Even though the updated block contains the same data and the same ID, it is not the same as block 3 in the previous version. As a result, the account is charged for four blocks.
244244

245245
![Azure Storage resources](./media/versioning-overview/versions-billing-scenario-2.png)
246246

@@ -252,7 +252,7 @@ In scenario 3, the blob has been updated, but the version has not. Block 3 was r
252252

253253
#### Scenario 4
254254

255-
In scenario 4, the base blob has been completely updated and contains none of its original blocks. As a result, the account is charged for all eight unique blocks -- four in the base blob, and four in the previous version. This scenario can occur if you are using an update method such as [UploadFromFile][dotnet_UploadFromFile], [UploadText][dotnet_UploadText], [UploadFromStream][dotnet_UploadFromStream], or [UploadFromByteArray][dotnet_UploadFromByteArray], because these methods replace all of the contents of a blob.
255+
In scenario 4, the base blob has been completely updated and contains none of its original blocks. As a result, the account is charged for all eight unique blocks &mdash; four in the base blob, and four in the previous version. This scenario can occur if you are writing to a blob with the Put Blob operation, because it replaces the entire contents of the base blob.
256256

257257
![Azure Storage resources](./media/versioning-overview/versions-billing-scenario-4.png)
258258

0 commit comments

Comments
 (0)