public AzureBlobStorageClient(AzureBlobStorageClientOptions options)
        {
            ArgCheck.NotNull(nameof(options), options);
            ArgCheck.NotNullOrEmpty(nameof(options.ContainerName), options.ContainerName);

            if (options.LeaseDuration.HasValue)
            {
                // As per Azure storage lease duration constraints.
                ArgCheck.GreaterThanOrEqualTo(nameof(options.LeaseDuration), options.LeaseDuration, TimeSpan.FromSeconds(15));
                ArgCheck.LessThanOrEqualTo(nameof(options.LeaseDuration), options.LeaseDuration, TimeSpan.FromSeconds(60));
            }

            if (options.NetworkTimeout.HasValue)
            {
                _clientOptions.Retry.NetworkTimeout = options.NetworkTimeout.Value;
            }

            if (!string.IsNullOrEmpty(options.ConnectionString))
            {
                _accountClient = new BlobServiceClient(options.ConnectionString, _clientOptions);
            }
            else if (!string.IsNullOrEmpty(options.AccountName))
            {
                var serviceUri = new Uri($"https://{options.AccountName}.{(options.EndpointSuffix ?? "blob.core.windows.net")}");

                if (!string.IsNullOrEmpty(options.AccountKey))
                {
                    _storageSharedKeyCredential = new StorageSharedKeyCredential(options.AccountName, options.AccountKey);
                    _accountClient = new BlobServiceClient(serviceUri, _storageSharedKeyCredential, _clientOptions);
                }
                else if (options.UseDefaultAzureCredential)
                {
                    _useManagedIdentity = true;
                    _accountClient      = new BlobServiceClient(serviceUri, new DefaultAzureCredential(), _clientOptions);
                }
                else if (!string.IsNullOrEmpty(options.SharedAccessSignature))
                {
                    throw new NotSupportedException($"{nameof(options.SharedAccessSignature)} is not supported.");
                }
                else
                {
                    throw new ArgumentException("Account key, SAS or managed identity is required.", nameof(options));
                }
            }
            else
            {
                throw new ArgumentException("Connection string or account name is required.", nameof(options));
            }

            _containerClient = _accountClient.GetBlobContainerClient(options.ContainerName);
            _defaultDirectoryRelativeAddress = options.DefaultDirectoryRelativeAddress;
            _leaseDuration = options.LeaseDuration;
            _useSnapshots  = options.UseSnapshots;
        }
示例#2
0
        internal static AzureBlobStorageClientOptions Verify(this AzureBlobStorageClientOptions options)
        {
            if (string.IsNullOrEmpty(options.ConnectionString))
            {
                if (string.IsNullOrEmpty(options.AccountName))
                {
                    throw new ArgumentException("Connection string or account name is required.", nameof(options));
                }

                if (string.IsNullOrEmpty(options.AccountKey) && string.IsNullOrEmpty(options.SharedAccessSignature) && !options.UseDefaultAzureCredential)
                {
                    throw new ArgumentException("Account key, SAS or managed identity is required.", nameof(options));
                }
            }

            if (options.LeaseDuration.HasValue)
            {
                // As per Azure storage lease duration constraints.
                ArgCheck.GreaterThanOrEqualTo(nameof(options.LeaseDuration), options.LeaseDuration, TimeSpan.FromSeconds(15));
                ArgCheck.LessThanOrEqualTo(nameof(options.LeaseDuration), options.LeaseDuration, TimeSpan.FromSeconds(60));
            }

            return(options);
        }