forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample03b_BatchingAsync.cs
175 lines (159 loc) · 7.47 KB
/
Sample03b_BatchingAsync.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using NUnit.Framework;
namespace Azure.Storage.Blobs.Samples
{
/// <summary>
/// Basic Azure Blob Storage batching samples
/// </summary>
public class Sample03b_BatchingAsync : SampleTest
{
/// <summary>
/// Delete several blobs in one request.
/// </summary>
[Test]
public async Task BatchDeleteAsync()
{
// Get a connection string to our Azure Storage account.
string connectionString = ConnectionString;
// Get a reference to a container named "sample-container" and then create it
BlobServiceClient service = new BlobServiceClient(connectionString);
BlobContainerClient container = service.GetBlobContainerClient(Randomize("sample-container"));
await container.CreateAsync();
try
{
// Create three blobs named "foo", "bar", and "baz"
BlobClient foo = container.GetBlobClient("foo");
BlobClient bar = container.GetBlobClient("bar");
BlobClient baz = container.GetBlobClient("baz");
await foo.UploadAsync(new MemoryStream(Encoding.UTF8.GetBytes("Foo!")));
await bar.UploadAsync(new MemoryStream(Encoding.UTF8.GetBytes("Bar!")));
await baz.UploadAsync(new MemoryStream(Encoding.UTF8.GetBytes("Baz!")));
// Delete all three blobs at once
BlobBatchClient batch = service.GetBlobBatchClient();
await batch.DeleteBlobsAsync(new Uri[] { foo.Uri, bar.Uri, baz.Uri });
}
finally
{
// Clean up after the test when we're finished
await container.DeleteAsync();
}
}
/// <summary>
/// Set several blob access tiers in one request.
/// </summary>
[Test]
public async Task BatchSetAccessTierAsync()
{
// Get a connection string to our Azure Storage account.
string connectionString = ConnectionString;
// Get a reference to a container named "sample-container" and then create it
BlobServiceClient service = new BlobServiceClient(connectionString);
BlobContainerClient container = service.GetBlobContainerClient(Randomize("sample-container"));
await container.CreateAsync();
try
{
// Create three blobs named "foo", "bar", and "baz"
BlobClient foo = container.GetBlobClient("foo");
BlobClient bar = container.GetBlobClient("bar");
BlobClient baz = container.GetBlobClient("baz");
await foo.UploadAsync(new MemoryStream(Encoding.UTF8.GetBytes("Foo!")));
await bar.UploadAsync(new MemoryStream(Encoding.UTF8.GetBytes("Bar!")));
await baz.UploadAsync(new MemoryStream(Encoding.UTF8.GetBytes("Baz!")));
// Set the access tier for all three blobs at once
BlobBatchClient batch = service.GetBlobBatchClient();
await batch.SetBlobsAccessTierAsync(new Uri[] { foo.Uri, bar.Uri, baz.Uri }, AccessTier.Cool);
}
finally
{
// Clean up after the test when we're finished
await container.DeleteAsync();
}
}
/// <summary>
/// Exert fine-grained control over individual operations in a batch
/// request.
/// </summary>
[Test]
public async Task FineGrainedBatchingAsync()
{
// Get a connection string to our Azure Storage account.
string connectionString = ConnectionString;
// Get a reference to a container named "sample-container" and then create it
BlobServiceClient service = new BlobServiceClient(connectionString);
BlobContainerClient container = service.GetBlobContainerClient(Randomize("sample-container"));
await container.CreateAsync();
try
{
// Create three blobs named "foo", "bar", and "baz"
BlobClient foo = container.GetBlobClient("foo");
BlobClient bar = container.GetBlobClient("bar");
BlobClient baz = container.GetBlobClient("baz");
await foo.UploadAsync(new MemoryStream(Encoding.UTF8.GetBytes("Foo!")));
await bar.UploadAsync(new MemoryStream(Encoding.UTF8.GetBytes("Bar!")));
await baz.UploadAsync(new MemoryStream(Encoding.UTF8.GetBytes("Baz!")));
// Create a batch with three deletes
BlobBatchClient batchClient = service.GetBlobBatchClient();
BlobBatch batch = batchClient.CreateBatch();
Response fooResponse = batch.DeleteBlob(foo.Uri, DeleteSnapshotsOption.IncludeSnapshots);
Response barResponse = batch.DeleteBlob(bar.Uri);
Response bazResponse = batch.DeleteBlob(baz.Uri);
// Submit the batch
await batchClient.SubmitBatchAsync(batch);
// Verify the results
Assert.AreEqual(202, fooResponse.Status);
Assert.AreEqual(202, barResponse.Status);
Assert.AreEqual(202, bazResponse.Status);
}
finally
{
// Clean up after the test when we're finished
await container.DeleteAsync();
}
}
/// <summary>
/// Catch any errors from a failed sub-operation.
/// </summary>
[Test]
public async Task BatchErrorsAsync()
{
// Get a connection string to our Azure Storage account.
string connectionString = ConnectionString;
// Get a reference to a container named "sample-container" and then create it
BlobServiceClient service = new BlobServiceClient(connectionString);
BlobContainerClient container = service.GetBlobContainerClient(Randomize("sample-container"));
await container.CreateAsync();
try
{
// Create a blob named "valid"
BlobClient valid = container.GetBlobClient("valid");
await valid.UploadAsync(new MemoryStream(Encoding.UTF8.GetBytes("Valid!")));
// Get a reference to a blob named "invalid", but never create it
BlobClient invalid = container.GetBlobClient("invalid");
// Delete both blobs at the same time
BlobBatchClient batch = service.GetBlobBatchClient();
await batch.DeleteBlobsAsync(new Uri[] { valid.Uri, invalid.Uri });
}
catch (AggregateException ex)
{
// An aggregate exception is thrown for all the indivudal failures
Assert.AreEqual(1, ex.InnerExceptions.Count);
RequestFailedException failure = ex.InnerException as RequestFailedException;
Assert.IsTrue(BlobErrorCode.BlobNotFound == failure.ErrorCode);
}
finally
{
// Clean up after the test when we're finished
await container.DeleteAsync();
}
}
}
}