-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathBlobsExtensionConfigProvider.cs
374 lines (308 loc) · 15 KB
/
BlobsExtensionConfigProvider.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Description;
using Microsoft.Azure.WebJobs.Extensions.Storage.Blobs.Triggers;
using Microsoft.Azure.WebJobs.Host.Bindings;
using Microsoft.Azure.WebJobs.Host.Blobs.Listeners;
using Microsoft.Azure.WebJobs.Host.Config;
using Microsoft.Azure.WebJobs.Host.Protocols;
using Microsoft.Azure.Storage.Blob;
namespace Microsoft.Azure.WebJobs.Host.Blobs.Bindings
{
[Extension("AzureStorageBlobs", "Blobs")]
internal class BlobsExtensionConfigProvider : IExtensionConfigProvider,
IConverter<BlobAttribute, CloudBlobContainer>,
IConverter<BlobAttribute, CloudBlobDirectory>,
IConverter<BlobAttribute, BlobsExtensionConfigProvider.MultiBlobContext>
{
private readonly BlobTriggerAttributeBindingProvider _triggerBinder;
private StorageAccountProvider _accountProvider;
private IContextGetter<IBlobWrittenWatcher> _blobWrittenWatcherGetter;
private readonly INameResolver _nameResolver;
private IConverterManager _converterManager;
public BlobsExtensionConfigProvider(StorageAccountProvider accountProvider,
BlobTriggerAttributeBindingProvider triggerBinder,
IContextGetter<IBlobWrittenWatcher> contextAccessor,
INameResolver nameResolver,
IConverterManager converterManager)
{
_accountProvider = accountProvider;
_triggerBinder = triggerBinder;
_blobWrittenWatcherGetter = contextAccessor;
_nameResolver = nameResolver;
_converterManager = converterManager;
}
public void Initialize(ExtensionConfigContext context)
{
InitilizeBlobBindings(context);
InitializeBlobTriggerBindings(context);
}
private void InitilizeBlobBindings(ExtensionConfigContext context)
{
var rule = context.AddBindingRule<BlobAttribute>();
// Bind to multiple blobs (either via a container; a blob directory, an IEnumerable<T>)
rule.BindToInput<CloudBlobDirectory>(this);
rule.BindToInput<CloudBlobContainer>(this);
rule.BindToInput<MultiBlobContext>(this); // Intermediate private context to capture state
rule.AddOpenConverter<MultiBlobContext, IEnumerable<BlobCollectionType>>(typeof(BlobCollectionConverter<>), this);
// BindToStream will also handle the custom Stream-->T converters.
rule.SetPostResolveHook(ToBlobDescr).
BindToStream(CreateStreamAsync, FileAccess.ReadWrite); // Precedence, must beat CloudBlobStream
// Normal blob
// These are not converters because Blob/Page/Append affects how we *create* the blob.
rule.SetPostResolveHook(ToBlobDescr).
BindToInput<CloudBlockBlob>((attr, cts) => CreateBlobReference<CloudBlockBlob>(attr, cts));
rule.SetPostResolveHook(ToBlobDescr).
BindToInput<CloudPageBlob>((attr, cts) => CreateBlobReference<CloudPageBlob>(attr, cts));
rule.SetPostResolveHook(ToBlobDescr).
BindToInput<CloudAppendBlob>((attr, cts) => CreateBlobReference<CloudAppendBlob>(attr, cts));
rule.SetPostResolveHook(ToBlobDescr).
BindToInput<ICloudBlob>((attr, cts) => CreateBlobReference<ICloudBlob>(attr, cts));
// CloudBlobStream's derived functionality is only relevant to writing.
rule.When("Access", FileAccess.Write).
SetPostResolveHook(ToBlobDescr).
BindToInput<CloudBlobStream>(ConvertToCloudBlobStreamAsync);
}
private void InitializeBlobTriggerBindings(ExtensionConfigContext context)
{
var rule = context.AddBindingRule<BlobTriggerAttribute>();
rule.BindToTrigger<ICloudBlob>(_triggerBinder);
rule.AddConverter<ICloudBlob, DirectInvokeString>(blob => new DirectInvokeString(blob.GetBlobPath()));
rule.AddConverter<DirectInvokeString, ICloudBlob>(ConvertFromInvokeString);
// Common converters shared between [Blob] and [BlobTrigger]
// Trigger already has the IStorageBlob. Whereas BindToInput defines: Attr-->Stream.
// Converter manager already has Stream-->Byte[],String,TextReader
context.AddConverter<ICloudBlob, Stream>(ConvertToStreamAsync);
// Blob type is a property of an existing blob.
// $$$ did we lose CloudBlob. That's a base class for Cloud*Blob, but does not implement ICloudBlob?
context.AddConverter(new StorageBlobConverter<CloudAppendBlob>());
context.AddConverter(new StorageBlobConverter<CloudBlockBlob>());
context.AddConverter(new StorageBlobConverter<CloudPageBlob>());
}
#region Container rules
CloudBlobContainer IConverter<BlobAttribute, CloudBlobContainer>.Convert(
BlobAttribute blobAttribute)
{
return GetContainer(blobAttribute);
}
// Write-only rule.
CloudBlobDirectory IConverter<BlobAttribute, CloudBlobDirectory>.Convert(
BlobAttribute blobAttribute)
{
var client = GetClient(blobAttribute);
BlobPath boundPath = BlobPath.ParseAndValidate(blobAttribute.BlobPath, isContainerBinding: false);
var container = client.GetContainerReference(boundPath.ContainerName);
CloudBlobDirectory directory = container.GetDirectoryReference(
boundPath.BlobName);
return directory;
}
#endregion
#region CloudBlob rules
// Produce a write only stream.
private async Task<CloudBlobStream> ConvertToCloudBlobStreamAsync(
BlobAttribute blobAttribute, ValueBindingContext context)
{
var stream = await CreateStreamAsync(blobAttribute, context);
return (CloudBlobStream)stream;
}
private async Task<T> CreateBlobReference<T>(BlobAttribute blobAttribute, CancellationToken cancellationToken)
{
var blob = await GetBlobAsync(blobAttribute, cancellationToken, typeof(T));
return (T)(blob);
}
#endregion
#region Support for binding to Multiple blobs
// Open type matching types that can bind to an IEnumerable<T> blob collection.
private class BlobCollectionType : OpenType
{
private static readonly Type[] _types = new Type[]
{
typeof(ICloudBlob),
typeof(CloudBlockBlob),
typeof(CloudPageBlob),
typeof(CloudAppendBlob),
typeof(TextReader),
typeof(Stream),
typeof(string)
};
public override bool IsMatch(Type type, OpenTypeMatchContext ctx)
{
bool match = _types.Contains(type);
return match;
}
}
// Converter to produce an IEnumerable<T> for binding to multiple blobs.
// T must have been matched by MultiBlobType
private class BlobCollectionConverter<T> : IAsyncConverter<MultiBlobContext, IEnumerable<T>>
{
private readonly FuncAsyncConverter<ICloudBlob, T> _converter;
public BlobCollectionConverter(BlobsExtensionConfigProvider parent)
{
IConverterManager cm = parent._converterManager;
_converter = cm.GetConverter<ICloudBlob, T, BlobAttribute>();
if (_converter == null)
{
throw new InvalidOperationException($"Can't convert blob to {typeof(T).FullName}.");
}
}
public async Task<IEnumerable<T>> ConvertAsync(MultiBlobContext context, CancellationToken cancellationToken)
{
// Query the blob container using the blob prefix (if specified)
// Note that we're explicitly using useFlatBlobListing=true to collapse
// sub directories. If users want to bind to a sub directory, they can
// bind to CloudBlobDirectory.
string prefix = context.Prefix;
var container = context.Container;
IEnumerable<IListBlobItem> blobItems = await container.ListBlobsAsync(prefix, true, cancellationToken);
// create an IEnumerable<T> of the correct type, performing any required conversions on the blobs
var list = await ConvertBlobs(blobItems);
return list;
}
private async Task<IEnumerable<T>> ConvertBlobs(IEnumerable<IListBlobItem> blobItems)
{
var list = new List<T>();
foreach (var blobItem in blobItems)
{
var src = (ICloudBlob)blobItem;
var funcCtx = new FunctionBindingContext(Guid.Empty, CancellationToken.None);
var valueCtx = new ValueBindingContext(funcCtx, CancellationToken.None);
var converted = await _converter(src, null, valueCtx);
list.Add(converted);
}
return list;
}
}
// Internal context object to aide in binding to multiple blobs.
private class MultiBlobContext
{
public string Prefix;
public CloudBlobContainer Container;
}
// Initial rule that captures the muti-blob context.
// Then a converter morphs this to the user type
MultiBlobContext IConverter<BlobAttribute, MultiBlobContext>.Convert(BlobAttribute attr)
{
var path = BlobPath.ParseAndValidate(attr.BlobPath, isContainerBinding: true);
return new MultiBlobContext
{
Prefix = path.BlobName,
Container = GetContainer(attr)
};
}
#endregion
private async Task<Stream> ConvertToStreamAsync(ICloudBlob input, CancellationToken cancellationToken)
{
WatchableReadStream watchableStream = await ReadBlobArgumentBinding.TryBindStreamAsync(input, cancellationToken);
return watchableStream;
}
// For describing InvokeStrings.
private async Task<ICloudBlob> ConvertFromInvokeString(DirectInvokeString input, Attribute attr, ValueBindingContext context)
{
var attrResolved = (BlobTriggerAttribute)attr;
var account = _accountProvider.Get(attrResolved.Connection);
var client = account.CreateCloudBlobClient();
BlobPath path = BlobPath.ParseAndValidate(input.Value);
var container = client.GetContainerReference(path.ContainerName);
var blob = await container.GetBlobReferenceFromServerAsync(path.BlobName);
return blob;
}
private async Task<Stream> CreateStreamAsync(
BlobAttribute blobAttribute,
ValueBindingContext context)
{
var cancellationToken = context.CancellationToken;
var blob = await GetBlobAsync(blobAttribute, cancellationToken);
switch (blobAttribute.Access)
{
case FileAccess.Read:
var readStream = await ReadBlobArgumentBinding.TryBindStreamAsync(blob, context);
return readStream;
case FileAccess.Write:
var writeStream = await WriteBlobArgumentBinding.BindStreamAsync(blob,
context, _blobWrittenWatcherGetter.Value);
return writeStream;
default:
throw new InvalidOperationException("Cannot bind blob to Stream using FileAccess ReadWrite.");
}
}
private CloudBlobClient GetClient(
BlobAttribute blobAttribute)
{
var account = _accountProvider.Get(blobAttribute.Connection, _nameResolver);
return account.CreateCloudBlobClient();
}
private CloudBlobContainer GetContainer(
BlobAttribute blobAttribute)
{
var client = GetClient(blobAttribute);
BlobPath boundPath = BlobPath.ParseAndValidate(blobAttribute.BlobPath, isContainerBinding: true);
var container = client.GetContainerReference(boundPath.ContainerName);
return container;
}
private async Task<ICloudBlob> GetBlobAsync(
BlobAttribute blobAttribute,
CancellationToken cancellationToken,
Type requestedType = null)
{
var client = GetClient(blobAttribute);
BlobPath boundPath = BlobPath.ParseAndValidate(blobAttribute.BlobPath);
var container = client.GetContainerReference(boundPath.ContainerName);
// Call ExistsAsync before attempting to create. This reduces the number of
// 40x calls that App Insights may be tracking automatically
if (blobAttribute.Access != FileAccess.Read && !await container.ExistsAsync())
{
await container.CreateIfNotExistsAsync(cancellationToken);
}
var blob = await container.GetBlobReferenceForArgumentTypeAsync(
boundPath.BlobName, requestedType, cancellationToken);
return blob;
}
private ParameterDescriptor ToBlobDescr(BlobAttribute attr, ParameterInfo parameter, INameResolver nameResolver)
{
// Resolve the connection string to get an account name.
var client = GetClient(attr);
var accountName = client.Credentials.AccountName;
var resolved = nameResolver.ResolveWholeString(attr.BlobPath);
string containerName = resolved;
string blobName = null;
int split = resolved.IndexOf('/');
if (split > 0)
{
containerName = resolved.Substring(0, split);
blobName = resolved.Substring(split + 1);
}
FileAccess access = FileAccess.ReadWrite;
if (attr.Access.HasValue)
{
access = attr.Access.Value;
}
else
{
var type = parameter.ParameterType;
if (type.IsByRef || type == typeof(TextWriter))
{
access = FileAccess.Write;
}
if (type == typeof(TextReader) || type == typeof(string) || type == typeof(byte[]))
{
access = FileAccess.Read;
}
}
return new BlobParameterDescriptor
{
Name = parameter.Name,
AccountName = accountName,
ContainerName = containerName,
BlobName = blobName,
Access = access
};
}
}
}