public async Task <Tuple <bool, CMPStorageError> > AddStreamToBlobAsync(Stream blobStream, string blobNameString,
                                                                                CMPBlobStorageOptions
                                                                                blobStorageOptions = null)
        {
            if (blobStream == null)
            {
                return(new Tuple <bool, CMPStorageError>(false, null));
            }

            if (string.IsNullOrEmpty(blobNameString) == true)
            {
                return(new Tuple <bool, CMPStorageError>(false, null));
            }

            try
            {
                var blobReference = PrepareBlockBlobReference(blobNameString);
                if (blobReference == null)
                {
                    return(new Tuple <bool, CMPStorageError>(false, null));
                }

                if (blobStorageOptions == null)
                {
                    await blobReference.UploadFromStreamAsync(blobStream, blobStream.Length);
                }
                else
                {
                    await blobReference.UploadFromStreamAsync(blobStream, blobStream.Length,
                                                              blobStorageOptions.AccessCondition,
                                                              blobStorageOptions.BlobRequestOptions,
                                                              blobStorageOptions.OperationContext, _tokenSource.Token);
                }

                return(new Tuple <bool, CMPStorageError>(true, null));
            }
            catch (StorageException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <bool, CMPStorageError>(false, error));
            }
            catch (ArgumentException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <bool, CMPStorageError>(false, error));
            }
        }
        public async Task <Tuple <byte[], CMPStorageError> > DownloadFromBlobAsync(string blobNameString,
                                                                                   CMPBlobStorageOptions
                                                                                   blobStorageOptions = null)
        {
            if (string.IsNullOrEmpty(blobNameString) == true)
            {
                return(new Tuple <byte[], CMPStorageError>(null, null));
            }

            try
            {
                var blobReference = PrepareBlockBlobReference(blobNameString);
                if (blobReference == null)
                {
                    return(new Tuple <byte[], CMPStorageError>(null, null));
                }

                var memoryStream = new MemoryStream();
                if (blobStorageOptions == null)
                {
                    await blobReference.DownloadToStreamAsync(memoryStream);
                }
                else
                {
                    await blobReference.DownloadToStreamAsync(memoryStream, blobStorageOptions.AccessCondition,
                                                              blobStorageOptions.BlobRequestOptions,
                                                              blobStorageOptions.OperationContext, _tokenSource.Token);
                }

                memoryStream.Close();
                var downloadedBytesArray = memoryStream.GetBuffer();
                return(new Tuple <byte[], CMPStorageError>(downloadedBytesArray, null));
            }
            catch (StorageException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <byte[], CMPStorageError>(null, null));
            }
            catch (ArgumentException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <byte[], CMPStorageError>(null, null));
            }
        }
        public async Task <Tuple <bool, CMPStorageError> > DeleteBlobAsync(string blobNameString,
                                                                           CMPBlobStorageOptions
                                                                           blobStorageOptions = null)
        {
            if (string.IsNullOrEmpty(blobNameString) == true)
            {
                return(new Tuple <bool, CMPStorageError>(false, null));
            }

            try
            {
                var blobReference = PrepareBlockBlobReference(blobNameString);
                if (blobReference == null)
                {
                    return(new Tuple <bool, CMPStorageError>(false, null));
                }

                var couldDelete = false;
                if (blobStorageOptions == null)
                {
                    couldDelete = await blobReference.DeleteIfExistsAsync();
                }
                else
                {
                    couldDelete = await blobReference.DeleteIfExistsAsync(blobStorageOptions.DeleteSnapshotsOption,
                                                                          blobStorageOptions.AccessCondition,
                                                                          blobStorageOptions.BlobRequestOptions,
                                                                          blobStorageOptions.OperationContext,
                                                                          _tokenSource.Token);
                }

                return(new Tuple <bool, CMPStorageError>(couldDelete, null));
            }
            catch (StorageException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <bool, CMPStorageError>(false, error));
            }
            catch (ArgumentException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <bool, CMPStorageError>(false, error));
            }
        }
        public async Task <Tuple <bool, CMPStorageError> > AddBytesToBlobAsync(byte[] blobBytesArray,
                                                                               string blobNameString,
                                                                               CMPBlobStorageOptions
                                                                               blobStorageOptions = null)
        {
            if ((blobBytesArray == null) || (blobBytesArray.Length == 0))
            {
                return(new Tuple <bool, CMPStorageError>(false, null));
            }

            try
            {
                var blobReference = PrepareBlockBlobReference(blobNameString);
                if (blobReference == null)
                {
                    return(new Tuple <bool, CMPStorageError>(false, null));
                }

                if (blobStorageOptions == null)
                {
                    await blobReference.UploadFromByteArrayAsync(blobBytesArray, 0, blobBytesArray.Length);
                }
                else
                {
                    await blobReference.UploadFromByteArrayAsync(blobBytesArray, 0, blobBytesArray.Length,
                                                                 blobStorageOptions.AccessCondition,
                                                                 blobStorageOptions.BlobRequestOptions,
                                                                 blobStorageOptions.OperationContext,
                                                                 _tokenSource.Token);
                }

                return(new Tuple <bool, CMPStorageError>(true, null));
            }
            catch (StorageException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <bool, CMPStorageError>(false, error));
            }
            catch (ArgumentException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <bool, CMPStorageError>(false, error));
            }
        }
        public async Task <Tuple <bool, CMPStorageError> > CreateBlobContainerAsync(CMPBlobStorageOptions
                                                                                    blobStorageOptions = null)
        {
            if (string.IsNullOrEmpty(_containerNameString) == true)
            {
                return(new Tuple <bool, CMPStorageError>(false, null));
            }

            try
            {
                var blobContainerReference = _cloudBlobClient.GetContainerReference(_containerNameString);
                if (blobContainerReference == null)
                {
                    return(new Tuple <bool, CMPStorageError>(false, null));
                }

                var couldCreate = false;
                if (blobStorageOptions == null)
                {
                    couldCreate = await blobContainerReference.CreateIfNotExistsAsync();
                }
                else
                {
                    couldCreate = await blobContainerReference.CreateIfNotExistsAsync(blobStorageOptions.
                                                                                      BlobContainerPublicAccessType,
                                                                                      blobStorageOptions.
                                                                                      BlobRequestOptions,
                                                                                      blobStorageOptions.
                                                                                      OperationContext,
                                                                                      _tokenSource.Token);
                }
                return(new Tuple <bool, CMPStorageError>(couldCreate, null));
            }
            catch (StorageException exception)
            {
                var error = CMPStorageError.CreateErrorFromException(exception);
                return(new Tuple <bool, CMPStorageError>(false, error));
            }
        }