Пример #1
0
        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);
        }
Пример #2
0
 public BlobContainerBinding(string parameterName, IArgumentBinding <IStorageBlobContainer> argumentBinding, IStorageBlobClient client,
                             IBindableBlobPath path)
 {
     _parameterName   = parameterName;
     _argumentBinding = argumentBinding;
     _client          = client;
     _path            = path;
 }
 public BlobContainerBinding(string parameterName, IArgumentBinding<IStorageBlobContainer> argumentBinding, IStorageBlobClient client,
     IBindableBlobPath path)
 {
     _parameterName = parameterName;
     _argumentBinding = argumentBinding;
     _client = client;
     _path = path;
 }
Пример #4
0
 public BlobBinding(string parameterName, IBlobArgumentBinding argumentBinding, IStorageBlobClient client, IBindableBlobPath path)
 {
     _parameterName   = parameterName;
     _argumentBinding = argumentBinding;
     _client          = client;
     _accountName     = BlobClient.GetAccountName(client);
     _path            = path;
     _converter       = CreateConverter(_client, path, argumentBinding.ValueType);
 }
 public BlobBinding(string parameterName, IBlobArgumentBinding argumentBinding, IStorageBlobClient client, IBindableBlobPath path)
 {
     _parameterName = parameterName;
     _argumentBinding = argumentBinding;
     _client = client;
     _accountName = BlobClient.GetAccountName(client);
     _path = path;
     _converter = CreateConverter(_client, path, argumentBinding.ValueType);
 }
Пример #6
0
 private static IAsyncObjectToTypeConverter <IStorageBlob> CreateConverter(IStorageBlobClient client,
                                                                           IBindableBlobPath path, Type argumentType)
 {
     return(new CompositeAsyncObjectToTypeConverter <IStorageBlob>(
                new OutputConverter <IStorageBlob>(new AsyncConverter <IStorageBlob, IStorageBlob>(
                                                       new IdentityConverter <IStorageBlob>())),
                new OutputConverter <ICloudBlob>(new AsyncConverter <ICloudBlob, IStorageBlob>(
                                                     new CloudBlobToStorageBlobConverter())),
                new OutputConverter <string>(new StringToStorageBlobConverter(client, path, argumentType))));
 }
 private static IAsyncObjectToTypeConverter<IStorageBlob> CreateConverter(IStorageBlobClient client,
     IBindableBlobPath path, Type argumentType)
 {
     return new CompositeAsyncObjectToTypeConverter<IStorageBlob>(
         new BlobOutputConverter<IStorageBlob>(new AsyncConverter<IStorageBlob, IStorageBlob>(
             new IdentityConverter<IStorageBlob>())),
         new BlobOutputConverter<ICloudBlob>(new AsyncConverter<ICloudBlob, IStorageBlob>(
             new CloudBlobToStorageBlobConverter())),
         new BlobOutputConverter<string>(new StringToStorageBlobConverter(client, path, argumentType)));
 }
Пример #8
0
        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));
        }
Пример #9
0
        internal static void ValidateContainerBinding(BlobAttribute attribute, Type parameterType, IBindableBlobPath path)
        {
            if (attribute.Access != null && attribute.Access != FileAccess.Read)
            {
                throw new InvalidOperationException("Only the 'Read' FileAccess mode is supported for blob container bindings.");
            }

            if (parameterType == typeof(CloudBlobContainer) && !string.IsNullOrEmpty(path.BlobNamePattern))
            {
                throw new InvalidOperationException("Only a container name can be specified when binding to CloudBlobContainer.");
            }
        }
        internal static void ValidateContainerBinding(BlobAttribute attribute, Type parameterType, IBindableBlobPath path)
        {
            if (attribute.Access != null && attribute.Access != FileAccess.Read)
            {
                throw new InvalidOperationException("Only the 'Read' FileAccess mode is supported for blob container bindings.");
            }

            if (parameterType == typeof(CloudBlobContainer) && !string.IsNullOrEmpty(path.BlobNamePattern))
            {
                throw new InvalidOperationException("Only a container name can be specified when binding to CloudBlobContainer.");
            }
        }
 public StringToStorageBlobConverter(IStorageBlobClient client, IBindableBlobPath defaultPath, Type argumentType)
 {
     _client       = client;
     _defaultPath  = defaultPath;
     _argumentType = argumentType;
 }