public string UploadFiletoBlobStorage()
        {
            try
            {
                BlobRequestOptions optionsWithRetryPolicy = new BlobRequestOptions()
                {
                    ParallelOperationThreadCount = 1, DisableContentMD5Validation = true, StoreBlobContentMD5 = false
                };

                CloudBlobContainer containerReference = new CloudStorageAccount(new StorageCredentials(AccountName, AccountKey), true).CreateCloudBlobClient().GetContainerReference(ContainerName);
                containerReference.CreateIfNotExists((BlobRequestOptions)null, (OperationContext)null);
                CloudBlockBlob cloudBlockBlob = (CloudBlockBlob)null;
                if (string.IsNullOrEmpty(FolderName) && string.IsNullOrEmpty(SubFolderName))
                {
                    cloudBlockBlob = containerReference.GetBlockBlobReference(BlobFileName);
                }
                if (!string.IsNullOrEmpty(FolderName) && string.IsNullOrEmpty(SubFolderName))
                {
                    cloudBlockBlob = containerReference.GetBlockBlobReference(FolderName + "/" + BlobFileName);
                }
                if (!string.IsNullOrEmpty(FolderName) && !string.IsNullOrEmpty(SubFolderName))
                {
                    cloudBlockBlob = containerReference.GetBlockBlobReference(FolderName + "/" + SubFolderName + "/" + BlobFileName);
                }
                if (isStream)
                {
                    Stream blobFile = BlobFile;
                    if (EncryptBlob)
                    {
                        cloudBlockBlob.UploadFromStream(blobFile, blobFile.Length, (AccessCondition)null, BlobOptions, (OperationContext)null);
                    }
                    else
                    {
                        cloudBlockBlob.UploadFromStream(blobFile, blobFile.Length, (AccessCondition)null, (BlobRequestOptions)optionsWithRetryPolicy, (OperationContext)null);
                    }
                }
                else
                {
                    Stream source = (Stream)File.OpenRead(Filepath);
                    if (EncryptBlob)
                    {
                        cloudBlockBlob.UploadFromStream(source, source.Length, (AccessCondition)null, BlobOptions, (OperationContext)null);
                    }
                    else
                    {
                        cloudBlockBlob.UploadFromStream(source, source.Length, (AccessCondition)null, (BlobRequestOptions)null, (OperationContext)null);
                    }
                }
                return("Success");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);
                return("Failed;Reason" + ex.Message);
            }
        }
Пример #2
0
        private async Task <CloudBlockBlob> GetBlockBlobAsync(string containerName, string blockName)
        {
            var blobContainer = new CloudStorageAccount(new StorageCredentials(_accountName, _storageKey), true).CreateCloudBlobClient().GetContainerReference(containerName);
            await blobContainer.CreateIfNotExistsAsync();

            return(blobContainer.GetBlockBlobReference(blockName));
        }