public async Task <IBinding> TryCreateAsync(BindingProviderContext context) { ParameterInfo parameter = context.Parameter; BlobAttribute blob = parameter.GetCustomAttribute <BlobAttribute>(inherit: false); if (blob == null) { return(null); } string resolvedCombinedPath = Resolve(blob.BlobPath); IBindableBlobPath path = BindableBlobPath.Create(resolvedCombinedPath); path.ValidateContractCompatibility(context.BindingDataContract); IBlobArgumentBinding argumentBinding = _provider.TryCreate(parameter, blob.Access); if (argumentBinding == null) { throw new InvalidOperationException("Can't bind Blob to type '" + parameter.ParameterType + "'."); } IStorageAccount account = await _accountProvider.GetStorageAccountAsync(context.CancellationToken); IStorageBlobClient client = account.CreateBlobClient(); IBinding binding = new BlobBinding(parameter.Name, argumentBinding, client, path); return(binding); }
public void CreateBlobWithValidPattern() { // one parameter Assert.Equal("{url}", BindableBlobPath.Create("{url}").ToString()); // one parameter with dot operator Assert.Equal("{a.b.c}", BindableBlobPath.Create("{a.b.c}").ToString()); // full blob url Assert.Equal("archivecontainershun/canaryeh/test/0/2017/08/01/00/09/27.avro", BindableBlobPath.Create("https://shunsouthcentralus.blob.core.windows.net/archivecontainershun/canaryeh/test/0/2017/08/01/00/09/27.avro").ToString()); // invalid partial blob url string ExceptionAssert.ThrowsFormat(() => BindableBlobPath.Create("shunsouthcentralus.blob.core.windows.net/container/blob"), "Invalid container name: shunsouthcentralus.blob.core.windows.net"); // more than one parameter ExceptionAssert.ThrowsFormat(() => BindableBlobPath.Create("{hostName}+{Container}+{Blob}"), "Invalid blob path '{hostName}+{Container}+{Blob}'. Paths must be in the format 'container/blob' or 'blob Url'."); }
public async Task <IBinding> TryCreateAsync(BindingProviderContext context) { ParameterInfo parameter = context.Parameter; BlobAttribute blobAttribute = parameter.GetCustomAttribute <BlobAttribute>(inherit: false); if (blobAttribute == null) { return(null); } string resolvedPath = Resolve(blobAttribute.BlobPath); IBindableBlobPath path = null; IStorageAccount account = await _accountProvider.GetStorageAccountAsync(context.Parameter, context.CancellationToken); StorageClientFactoryContext clientFactoryContext = new StorageClientFactoryContext { Parameter = context.Parameter }; IStorageBlobClient client = account.CreateBlobClient(clientFactoryContext); // first try to bind to the Container IArgumentBinding <IStorageBlobContainer> containerArgumentBinding = _blobContainerArgumentProvider.TryCreate(parameter); if (containerArgumentBinding == null) { // if this isn't a Container binding, try a Blob binding IBlobArgumentBinding blobArgumentBinding = _blobArgumentProvider.TryCreate(parameter, blobAttribute.Access); if (blobArgumentBinding == null) { throw new InvalidOperationException("Can't bind Blob to type '" + parameter.ParameterType + "'."); } path = BindableBlobPath.Create(resolvedPath); path.ValidateContractCompatibility(context.BindingDataContract); return(new BlobBinding(parameter.Name, blobArgumentBinding, client, path)); } path = BindableBlobPath.Create(resolvedPath, isContainerBinding: true); path.ValidateContractCompatibility(context.BindingDataContract); BlobContainerBinding.ValidateContainerBinding(blobAttribute, parameter.ParameterType, path); return(new BlobContainerBinding(parameter.Name, containerArgumentBinding, client, path)); }