Esempio n. 1
0
        /// <summary>
        /// Starts async copy of source to target block blob.
        /// ********** NOTE that block blob copy operations are ASYNCHRONOUS - just returning from this method does NOT mean the copy has already finished.
        /// Returns the target block blob so you can optionally check or wait on targetBlob.CopyState.Status to transition from Pending.
        /// Source and target storage accounts and containers can be the same, or different for greater flexibility.
        /// </summary>
        /// <param name="sourceStorageAccount"></param>
        /// <param name="sourceContainerName"></param>
        /// <param name="sourceBlobPathInContainer"></param>
        /// <param name="targetStorageAccount"></param>
        /// <param name="targetContainerName"></param>
        /// <param name="targetBlobPathInContainer"></param>
        /// <returns>CloudBlockBlob on OpResult.Output - cast Output to CloudBlockBlob to use</returns>
        public async Task <OpResult> CopyBlockBlobAsync(CloudStorageAccount sourceStorageAccount, string sourceContainerName, string sourceBlobPathInContainer, CloudStorageAccount targetStorageAccount, string targetContainerName, string targetBlobPathInContainer)
        {
            OpResult opResult = new OpResult()
            {
                Succeeded = false
            };

            ValidationResult validationResult = new ValidationResult();

            validationResult.Validations.AddItems(Validator.ValidateStorageAccount(sourceStorageAccount).Validations);
            validationResult.Validations.AddItems(Validator.ValidateContainerName(sourceContainerName).Validations);
            validationResult.Validations.AddItems(Validator.ValidateBlobPath(sourceBlobPathInContainer).Validations);
            validationResult.Validations.AddItems(Validator.ValidateStorageAccount(targetStorageAccount).Validations);
            validationResult.Validations.AddItems(Validator.ValidateContainerName(targetContainerName).Validations);
            validationResult.Validations.AddItems(Validator.ValidateBlobPath(targetBlobPathInContainer).Validations);

            opResult.ValidationResult = validationResult;

            if (!validationResult.IsValid)
            {
                return(opResult);
            }

            // Cleanup
            // Blob paths should NOT have leading slashes
            if (sourceBlobPathInContainer.StartsWith("/"))
            {
                sourceBlobPathInContainer = sourceBlobPathInContainer.Substring(1);
            }
            if (targetBlobPathInContainer.StartsWith("/"))
            {
                targetBlobPathInContainer = targetBlobPathInContainer.Substring(1);
            }

            try
            {
                CloudBlobContainer sourceContainer = await GetContainerAsync(sourceStorageAccount, sourceContainerName, false);

                CloudBlobContainer targetContainer = await GetContainerAsync(targetStorageAccount, targetContainerName, true);

                CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(sourceBlobPathInContainer);
                CloudBlockBlob targetBlob = targetContainer.GetBlockBlobReference(targetBlobPathInContainer);

                await targetBlob.StartCopyAsync(sourceBlob);

                opResult.Succeeded = true;
                opResult.Output    = targetBlob;
            }
            catch (Exception ex)
            {
                opResult.Succeeded = false;
                opResult.Message   = ErrorUtil.GetText(ex);
                opResult.Exception = ex;
            }

            return(opResult);
        }
Esempio n. 2
0
        /// <summary>
        /// Deletes a blob asynchronously.
        /// </summary>
        /// <param name="storageAccount"></param>
        /// <param name="containerName"></param>
        /// <param name="targetBlobPath"></param>
        /// <returns></returns>
        public async Task <OpResult> DeleteBlobByPathAsync(CloudStorageAccount storageAccount, string containerName, string targetBlobPath)
        {
            OpResult opResult = new OpResult();

            ValidationResult validationResult = new ValidationResult();

            validationResult.Validations.AddItems(Validator.ValidateStorageAccount(storageAccount).Validations);
            validationResult.Validations.AddItems(Validator.ValidateContainerName(containerName).Validations);
            validationResult.Validations.AddItems(Validator.ValidateBlobPath(targetBlobPath).Validations);

            opResult.ValidationResult = validationResult;

            if (!validationResult.IsValid)
            {
                return(opResult);
            }

            // Cleanup
            // Blob paths should NOT have leading slashes
            if (targetBlobPath.StartsWith("/"))
            {
                targetBlobPath = targetBlobPath.Substring(1);
            }

            CloudBlob blob = await this.GetBlobFromPathAsync(storageAccount, containerName, targetBlobPath);

            if (blob != null)
            {
                try
                {
                    await blob.DeleteIfExistsAsync();

                    opResult.Succeeded = true;
                }
                catch (Exception ex)
                {
                    opResult.Succeeded = false;
                    opResult.Message   = ErrorUtil.GetText(ex);
                    opResult.Exception = ex;
                }
            }
            else
            {
                opResult.Succeeded = false;
                opResult.Message   = "Specified blob does not exist.";
            }

            return(opResult);
        }
Esempio n. 3
0
        public async Task <OpResult> UploadFileFromUrlAsync(CloudStorageAccount storageAccount, string containerName, string sourceFileUrl, string targetBlobPath)
        {
            OpResult opResult = new OpResult()
            {
                Succeeded = false
            };

            ValidationResult validationResult = new ValidationResult();

            validationResult.Validations.AddItems(Validator.ValidateStorageAccount(storageAccount).Validations);
            validationResult.Validations.AddItems(Validator.ValidateContainerName(containerName).Validations);
            validationResult.Validations.AddItems(Validator.ValidateBlobPath(targetBlobPath).Validations);

            opResult.ValidationResult = validationResult;

            if (!validationResult.IsValid)
            {
                return(opResult);
            }

            try
            {
                WebRequest request = WebRequest.Create(sourceFileUrl);

                request.Timeout = 120000;
                request.UseDefaultCredentials = true;
                request.Proxy.Credentials     = request.Credentials;

                using (WebResponse response = await request.GetResponseAsync())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        opResult = await this.UploadStreamAsync(storageAccount, containerName, stream, targetBlobPath);
                    }
                }
            }
            catch (Exception ex)
            {
                // TODO log exception

                opResult.Succeeded = false;
                opResult.Message   = ErrorUtil.GetText(ex);
                opResult.Exception = ex;
            }

            return(opResult);
        }
Esempio n. 4
0
        public async Task <OpResult> WriteBlobAsync(string storageAccountConnectionString, string containerName, string fileContents, string filePath)
        {
            OpResult opResult = new OpResult();

            CloudStorageAccount storageAccount = Common.GetStorageAccount(storageAccountConnectionString);

            ValidationResult validationResult = new ValidationResult();

            validationResult.Validations.AddItems(Validator.ValidateStorageAccount(storageAccount).Validations);
            validationResult.Validations.AddItems(Validator.ValidateContainerName(containerName).Validations);
            validationResult.Validations.AddItems(Validator.ValidateFilePath(filePath).Validations);

            opResult.ValidationResult = validationResult;

            if (!validationResult.IsValid)
            {
                return(opResult);
            }

            // Serialize file contents to byte array
            byte[] bytes = Encoding.UTF8.GetBytes(fileContents);

            try
            {
                CloudBlobContainer container = await this.GetContainerAsync(storageAccount, containerName, true);

                CloudBlockBlob blob = container.GetBlockBlobReference(filePath);

                // Upload the file
                await blob.UploadFromByteArrayAsync(bytes, 0, bytes.Length);

                // Result is success if the blob exists - the upload operation does not return a status so we check success after the upload
                opResult.Succeeded = await blob.ExistsAsync();
            }
            catch (Exception ex)
            {
                // TODO log error

                opResult.Succeeded = false;
                opResult.Message   = ErrorUtil.GetText(ex);
                opResult.Exception = ex;
            }

            return(opResult);
        }
Esempio n. 5
0
        public async Task <OpResult> UploadStreamAsync(CloudStorageAccount storageAccount, string containerName, Stream sourceStream, string targetBlobPath)
        {
            OpResult opResult = new OpResult()
            {
                Succeeded = false
            };

            ValidationResult validationResult = new ValidationResult();

            validationResult.Validations.AddItems(Validator.ValidateStorageAccount(storageAccount).Validations);
            validationResult.Validations.AddItems(Validator.ValidateContainerName(containerName).Validations);
            validationResult.Validations.AddItems(Validator.ValidateBlobPath(targetBlobPath).Validations);

            opResult.ValidationResult = validationResult;

            if (!validationResult.IsValid)
            {
                return(opResult);
            }

            try
            {
                byte[] bytes;

                using (var memoryStream = new MemoryStream())
                {
                    await sourceStream.CopyToAsync(memoryStream);

                    bytes = memoryStream.ToArray();
                }

                opResult = await this.UploadByteArrayAsync(storageAccount, containerName, bytes, targetBlobPath);
            }
            catch (Exception ex)
            {
                // TODO log exception

                opResult.Succeeded = false;
                opResult.Message   = ErrorUtil.GetText(ex);
                opResult.Exception = ex;
            }

            return(opResult);
        }
Esempio n. 6
0
        public async Task <OpResult> UploadByteArrayAsync(CloudStorageAccount storageAccount, string containerName, byte[] bytes, string targetBlobPath)
        {
            OpResult opResult = new OpResult()
            {
                Succeeded = false
            };

            ValidationResult validationResult = new ValidationResult();

            validationResult.Validations.AddItems(Validator.ValidateStorageAccount(storageAccount).Validations);
            validationResult.Validations.AddItems(Validator.ValidateContainerName(containerName).Validations);
            validationResult.Validations.AddItems(Validator.ValidateBlobPath(targetBlobPath).Validations);

            opResult.ValidationResult = validationResult;

            if (!validationResult.IsValid)
            {
                return(opResult);
            }

            try
            {
                CloudBlobContainer container = await this.GetContainerAsync(storageAccount, containerName, true);

                CloudBlockBlob blob = container.GetBlockBlobReference(targetBlobPath);

                // Upload the file
                await blob.UploadFromByteArrayAsync(bytes, 0, bytes.Length);

                // Result is success if the blob exists - the upload operation does not return a status so we check success after the upload
                opResult.Succeeded = await blob.ExistsAsync();
            }
            catch (Exception ex)
            {
                // TODO log exception

                opResult.Succeeded = false;
                opResult.Message   = ErrorUtil.GetText(ex);
                opResult.Exception = ex;
            }

            return(opResult);
        }
Esempio n. 7
0
        public async Task <OpResult> UploadFileFromLocalAsync(CloudStorageAccount storageAccount, string containerName, string sourceFilePath, string targetBlobPath)
        {
            OpResult opResult = new OpResult()
            {
                Succeeded = false
            };

            ValidationResult validationResult = new ValidationResult();

            validationResult.Validations.AddItems(Validator.ValidateStorageAccount(storageAccount).Validations);
            validationResult.Validations.AddItems(Validator.ValidateContainerName(containerName).Validations);
            validationResult.Validations.AddItems(Validator.ValidateBlobPath(targetBlobPath).Validations);
            validationResult.Validations.AddItems(Validator.ValidateFilePath(sourceFilePath).Validations);

            opResult.ValidationResult = validationResult;

            if (!validationResult.IsValid)
            {
                return(opResult);
            }

            try
            {
                using (FileStream stream = File.OpenRead(sourceFilePath))
                {
                    opResult = await this.UploadStreamAsync(storageAccount, containerName, stream, targetBlobPath);
                }
            }
            catch (Exception ex)
            {
                // TODO log exception

                opResult.Succeeded = false;
                opResult.Message   = ErrorUtil.GetText(ex);
                opResult.Exception = ex;
            }

            return(opResult);
        }