Example #1
0
        /// <inheritdoc/>
        public async Task <bool> ContainerCreateAsync(string storageAccountName, string containerName, StorageClientProviderContext context)
        {
            _ = StringHelpers.NullIfNullOrEmpty(storageAccountName) ?? throw new ArgumentNullException(nameof(storageAccountName));
            _ = StringHelpers.NullIfNullOrEmpty(containerName) ?? throw new ArgumentNullException(nameof(containerName));
            _ = context ?? throw new ArgumentNullException(nameof(context));

            BlobContainerInfo blobContainerInfo;
            var containerUri = StorageHelpers.BuildBlobStorageUri(storageAccountName, containerName);

            try
            {
                var containerSleeve = _blobContainerClientProvider.GetBlobContainerSleeveForUri(containerUri, context);
                blobContainerInfo = await containerSleeve.Client.CreateAsync().ConfigureAwait(false);
            }
            catch (Exception e)
            {
                throw new GridwichStorageServiceException(containerUri, "Failed to create the container.",
                                                          LogEventIds.FailedToCreateContainer, context.ClientRequestIdAsJObject, e);
            }

            if (blobContainerInfo != null)
            {
                _log.LogEventObject(LogEventIds.ContainerCreateProgress, blobContainerInfo);
                return(true);
            }

            throw new GridwichStorageServiceException(containerUri, "Failed to create the container.",
                                                      LogEventIds.FailedToCreateContainer, context.ClientRequestIdAsJObject);
        }
Example #2
0
        /// <inheritdoc/>
        public async Task <bool> ContainerDeleteAsync(string storageAccountName, string containerName, StorageClientProviderContext context)
        {
            _ = StringHelpers.NullIfNullOrWhiteSpace(storageAccountName) ?? throw new ArgumentNullException(nameof(storageAccountName));
            _ = StringHelpers.NullIfNullOrWhiteSpace(containerName) ?? throw new ArgumentNullException(nameof(containerName));
            _ = context ?? throw new ArgumentNullException(nameof(context));

            Response response;
            var      containerUri = StorageHelpers.BuildBlobStorageUri(storageAccountName, containerName);

            try
            {
                var containerSleeve = _blobContainerClientProvider.GetBlobContainerSleeveForUri(containerUri, context);
                response = await containerSleeve.Client.DeleteAsync().ConfigureAwait(false);
            }
            catch (Exception e)
            {
                _log.LogExceptionObject(LogEventIds.FailedToDeleteContainer, e, containerUri);
                throw new GridwichStorageServiceException(containerUri, "Could not delete blob container.",
                                                          LogEventIds.FailedToDeleteContainer, context.ClientRequestIdAsJObject, e);
            }

            if (response.Status == 200)
            {
                return(true);
            }

            _log.LogEventObject(LogEventIds.DeleteContainerUnexpectedResponse, new { containerUri, response });
            throw new GridwichStorageServiceException(containerUri, $"Unexpected response while deleting blob container: {response.Status} {response.ReasonPhrase}",
                                                      LogEventIds.DeleteContainerUnexpectedResponse, context.ClientRequestIdAsJObject);
        }
Example #3
0
        /// <summary>
        /// Creates a BlobContainerClient.  Called when one does not exist yet.
        /// </summary>
        /// <param name="uri">The target Uri.</param>
        /// <param name="context">The context.</param>
        /// <returns>
        /// A BlobContainerClient object.
        /// </returns>
        internal StorageContainerClientSleeve CreateBlobContainerClientForUri(
            Uri uri,
            StorageClientProviderContext context)
        {
            // set up a copy of the Context...
            var ctxCopy = new StorageClientProviderContext(context);

            BlobContainerClient blobContainerClient;
            BlobUriBuilder      containerUriBuilder;
            BlobClientOptions   clientOptions;

            try
            {
                containerUriBuilder = new BlobUriBuilder(uri);
                clientOptions       = new BlobClientOptions();
                var clientRequestIdPolicy = new BlobClientPipelinePolicy(ctxCopy);
                var uriC = StorageHelpers.BuildBlobStorageUri(containerUriBuilder.AccountName, containerUriBuilder.BlobContainerName);
                blobContainerClient = new BlobContainerClient(uriC, _identity, clientOptions);
            }
            catch (Exception fe) when(fe is ArgumentNullException || fe is UriFormatException)
            {
                var aex = new ArgumentException(LogEventIds.BlobContainerClientProviderUriMissingAccountName.Name, fe);

                _log.LogExceptionObject(LogEventIds.BlobContainerClientProviderUriMissingAccountName, aex, uri);
                throw aex;
            }
            catch (Exception e)
            {
                _log.LogExceptionObject(LogEventIds.BlobContainerClientProviderFailedToCreateBlobContainerClient, e, uri);
                throw;
            }


            try
            {
                var accountUri = StorageHelpers.BuildStorageAccountUri(containerUriBuilder.AccountName, buildForBlobService: true);
                var sleeve     = new StorageContainerClientSleeve(blobContainerClient,
                                                                  new BlobServiceClient(accountUri, _identity, clientOptions),
                                                                  ctxCopy);
                return(sleeve);
            }
            catch (ArgumentException aex)
            {
                _log.LogExceptionObject(LogEventIds.BlobContainerClientProviderUriMissingAccountName, aex, uri);
                throw;
            }
        }
Example #4
0
        public void BuildBlobStorageUri_Test(
            string accountName, string containerName, string blobName,
            string expectedResult, Type expectedExceptionType)
        {
            Uri res;

            try
            {
                res = StorageHelpers.BuildBlobStorageUri(accountName, containerName, blobName);
            }
            catch (Exception e)
            {
                expectedExceptionType.ShouldNotBeNull();
                e.ShouldBeOfType(expectedExceptionType);
                return;
            }

            expectedExceptionType.ShouldBeNull();
            res.ToString().ShouldBe(expectedResult);
        }