Exemple #1
0
        public virtual async Task <Blob> GetBlobReferenceAsync(string containerName, string blobId)
        {
            Utility.AssertNotNullOrEmpty(nameof(blobId), blobId);
            var retrieveBlobApiCallresult = await this.BlobsApiClient.GetBlobByIdAsync(blobId);

            if (false == retrieveBlobApiCallresult.ResponseMessage.IsSuccessStatusCode)
            {
                if (retrieveBlobApiCallresult.ResponseMessage.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    throw ExceptionGenerator.BlobNotFoundException(blobId);
                }
                else
                {
                    throw ExceptionGenerator.GeneralFailureException(retrieveBlobApiCallresult.StringContent);
                }
            }

            var blobDTO = retrieveBlobApiCallresult.GetContent();

            var container = await GetContainerReferenceAsync(containerName);

            var blob = new Blob(this, container, blobDTO.Id, blobDTO.OrigFileName,
                                blobDTO.MimeType, blobDTO.SizeInBytes, blobDTO.DownloadRelativeUrl);

            return(blob);
        }
        public virtual async Task DeleteMetaDataAsync(string key, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var serviceResponse = await _metaDataApi.DeleteMetaDataAsync(this.Id, key);

            if (false == serviceResponse.IsSuccessStatusCode)
            {
                throw ExceptionGenerator.GeneralFailureException(serviceResponse.Content.ToString());
            }
        }
Exemple #3
0
        public virtual async Task DeleteAsync(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var serviceResponse = await _containersApi.DeleteContainerAsync(this.Id);

            if (false == serviceResponse.IsSuccessStatusCode)
            {
                throw ExceptionGenerator.GeneralFailureException(serviceResponse.Content?.ToString());
            }
        }
Exemple #4
0
        public virtual async Task <IEnumerable <BlobContainer> > ListContainersAsync()
        {
            var containersResponse = await ContainersApiClient.ListContainersAsync();

            if (!containersResponse.ResponseMessage.IsSuccessStatusCode)
            {
                throw ExceptionGenerator.GeneralFailureException(containersResponse.StringContent);
            }
            var conainersDTO = containersResponse.GetContent();

            return(conainersDTO.Select(c => new BlobContainer(c.Name, c.Id, this)));
        }
        public virtual async Task SetMetaDataAsync(CancellationToken cancellationToken, string key, string value)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var serviceResponse = await _metaDataApi.SetMetaDataAsync(this.Id, new DTO.BlobMetaDataDTO
            {
                Key   = key,
                Value = value
            });

            if (false == serviceResponse.ResponseMessage.IsSuccessStatusCode)
            {
                throw ExceptionGenerator.GeneralFailureException(serviceResponse.ResponseMessage.Content.ToString());
            }
        }
Exemple #6
0
        public virtual async Task <BlobContainer> CreateContainerAsync(string containerName)
        {
            var serviceResponse = await ContainersApiClient.CreateContainerAsync(
                new ContainerDTO()
            {
                Name = containerName
            });

            if (false == serviceResponse.ResponseMessage.IsSuccessStatusCode)
            {
                throw ExceptionGenerator.GeneralFailureException(serviceResponse.StringContent);
            }

            var container = serviceResponse.GetContent();

            return(new BlobContainer(container.Name, container.Id, this));
        }
Exemple #7
0
        public virtual async Task <Blob> UploadBlobAsync(Stream file, string fileName, string contentType, CancellationToken cancellationToken)
        {
            var contentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                FileName = $"\"{fileName}\"", Name = "file"
            };
            var contentMimeType = new MediaTypeHeaderValue(contentType);
            var serviceResponse = await _containersApi.AddBlobAsync(this.Id, contentDisposition, contentMimeType, file);

            if (false == serviceResponse.ResponseMessage.IsSuccessStatusCode)
            {
                throw ExceptionGenerator.GeneralFailureException(serviceResponse.StringContent);
            }
            var newBlob = serviceResponse.GetContent();

            return(new Blob(this.ServiceClient, this, newBlob.Id, newBlob.OrigFileName,
                            newBlob.MimeType, newBlob.SizeInBytes, newBlob.DownloadRelativeUrl));
        }
Exemple #8
0
        public virtual async Task <BlobContainer> GetContainerReferenceAsync(string containerName)
        {
            Utility.AssertNotNullOrEmpty(nameof(containerName), containerName);
            var response = await ContainersApiClient.GetContainerByNameAsync(containerName);

            if (response.ResponseMessage.IsSuccessStatusCode == false)
            {
                if (response.ResponseMessage.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    throw ExceptionGenerator.ContainerNotFoundException($"Container {containerName} not found.");
                }

                throw ExceptionGenerator.GeneralFailureException(response.StringContent);
            }

            var containerDTO = response.GetContent();

            return(new BlobContainer(containerDTO.Name, containerDTO.Id, this));
        }
        public virtual async Task <Stream> OpenReadAsync(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var serviceResponse = await _blobApi.RawBlobAsync(this.Id);

            if (false == serviceResponse.ResponseMessage.IsSuccessStatusCode)
            {
                if (serviceResponse.ResponseMessage.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    throw ExceptionGenerator.BlobNotFoundException(this.Id);
                }
                else
                {
                    throw ExceptionGenerator.GeneralFailureException(serviceResponse.StringContent);
                }
            }

            return(await serviceResponse.ResponseMessage.Content.ReadAsStreamAsync());
        }
Exemple #10
0
        public virtual async Task <IEnumerable <Blob> > ListBlobsAsync(CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(Id))
            {
                throw ExceptionGenerator.InvalidOperationException("ListBlobs", this.Name, "Container");
            }

            var serviceResponse = await _containersApi.ListContainerBlobsAsync(this.Id);

            cancellationToken.ThrowIfCancellationRequested();

            if (false == serviceResponse.ResponseMessage.IsSuccessStatusCode)
            {
                throw ExceptionGenerator.GeneralFailureException(serviceResponse.StringContent.ToString());
            }

            var containerBlobDTOs = serviceResponse.GetContent();

            return(containerBlobDTOs.Select(b => new Blob(this.ServiceClient, this, b.Id, b.OrigFileName, b.MimeType, b.SizeInBytes, b.DownloadRelativeUrl)));
        }
Exemple #11
0
        public virtual async Task RenameAsync(string newName, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var containerDto = new ContainerDTO()
            {
                Id   = Id,
                Name = newName
            };
            var serviceResponse = await _containersApi.UpdateContainerAsync(containerDto);

            if (false == serviceResponse.ResponseMessage.IsSuccessStatusCode)
            {
                throw ExceptionGenerator.GeneralFailureException(serviceResponse.StringContent?.ToString());
            }

            var updatedContainer = serviceResponse.GetContent();

            this.Id   = updatedContainer.Id;
            this.Name = updatedContainer.Name;
        }
        public virtual async Task UpdateBlobAsync(Stream file, string fileName, string contentType, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var contentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                FileName = $"\"{fileName}\"", Name = "file"
            };
            var contentMimeType = new MediaTypeHeaderValue(contentType);
            var serviceResponse = await _blobApi.UpdateBlobAsync(this.Id, contentDisposition, contentMimeType, file);

            var updatedBlob = serviceResponse.GetContent();

            if (false == serviceResponse.ResponseMessage.IsSuccessStatusCode)
            {
                throw ExceptionGenerator.GeneralFailureException(serviceResponse.StringContent);
            }

            FileName    = updatedBlob.OrigFileName;
            Id          = updatedBlob.Id;
            MimeType    = updatedBlob.MimeType;
            SizeInBytes = updatedBlob.SizeInBytes;
        }
        public virtual async Task <Dictionary <string, string> > GetMetaDataAsync(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var serviceResponse = await _metaDataApi.GetMetaDataAsync(this.Id);

            if (false == serviceResponse.ResponseMessage.IsSuccessStatusCode)
            {
                throw ExceptionGenerator.GeneralFailureException(serviceResponse.StringContent);
            }

            var mds = serviceResponse.GetContent().ToList();

            var mdDictionary = new Dictionary <string, string>();

            foreach (var md in mds)
            {
                mdDictionary.Add(md.Key, md.Value);
            }

            return(mdDictionary);
        }