private bool DownloadConfig_Azure(string downloadedZipPath, string AzureAccountName, string AzureAccountKey) { CloudStorageAccount storageAccount = null; CloudBlobClient blobClient = null; CloudBlobContainer container = null; CloudBlockBlob blockBlob = null; string leaseId = null; try { storageAccount = new CloudStorageAccount(new StorageCredentialsAccountAndKey(AzureAccountName, AzureAccountKey), true); blobClient = storageAccount.CreateCloudBlobClient(); container = blobClient.GetContainerReference(AzureConfigContainerName); container.CreateIfNotExist(); blockBlob = container.GetBlockBlobReference(DesiredConfigBlobName()); bool blobExists = AzureUtils.BlockBlobExists(logger, blockBlob); if (blobExists) { leaseId = AzureUtils.AcquireLease(logger, blockBlob, AzureBlobLeaseTimeout); // Acquire Lease on Blob } else { return(false); } if (blobExists && leaseId == null) { Utils.structuredLog(logger, "ER", "AcquireLease on Blob: " + DesiredConfigBlobName() + " Failed"); return(false); } string url = blockBlob.Uri.ToString(); if (blockBlob.ServiceClient.Credentials.NeedsTransformUri) { url = blockBlob.ServiceClient.Credentials.TransformUri(url); } var req = BlobRequest.Get(new Uri(url), AzureBlobLeaseTimeout, null, leaseId); blockBlob.ServiceClient.Credentials.SignRequest(req); using (var reader = new BinaryReader(req.GetResponse().GetResponseStream())) { FileStream zipFile = new FileStream(downloadedZipPath, FileMode.OpenOrCreate); reader.BaseStream.CopyTo(zipFile); zipFile.Close(); } req.GetResponse().GetResponseStream().Close(); AzureUtils.ReleaseLease(logger, blockBlob, leaseId, AzureBlobLeaseTimeout); // Release Lease on Blob return(true); } catch (Exception e) { Utils.structuredLog(logger, "E", e.Message + ". DownloadConfig_Azure, downloadZipPath: " + downloadedZipPath + ". " + e); AzureUtils.ReleaseLease(logger, blockBlob, leaseId, AzureBlobLeaseTimeout); return(false); } }
private bool UploadConfig_Azure(string configZipPath, string AzureAccountName, string AzureAccountKey) { CloudStorageAccount storageAccount = null; CloudBlobClient blobClient = null; CloudBlobContainer container = null; CloudBlockBlob blockBlob = null; string leaseId = null; try { storageAccount = new CloudStorageAccount(new StorageCredentialsAccountAndKey(AzureAccountName, AzureAccountKey), true); blobClient = storageAccount.CreateCloudBlobClient(); container = blobClient.GetContainerReference(AzureConfigContainerName); container.CreateIfNotExist(); blockBlob = container.GetBlockBlobReference(ActualConfigBlobName()); bool blobExists = AzureUtils.BlockBlobExists(logger, blockBlob); if (blobExists) { leaseId = AzureUtils.AcquireLease(logger, blockBlob, AzureBlobLeaseTimeout); // Acquire Lease on Blob } else { blockBlob.Container.CreateIfNotExist(); } if (blobExists && leaseId == null) { Utils.structuredLog(logger, "ER", "AcquireLease on Blob: " + ActualConfigBlobName() + " Failed"); return(false); } string url = blockBlob.Uri.ToString(); if (blockBlob.ServiceClient.Credentials.NeedsTransformUri) { url = blockBlob.ServiceClient.Credentials.TransformUri(url); } var req = BlobRequest.Put(new Uri(url), AzureBlobLeaseTimeout, new BlobProperties(), BlobType.BlockBlob, leaseId, 0); using (var writer = new BinaryWriter(req.GetRequestStream())) { writer.Write(File.ReadAllBytes(configZipPath)); writer.Close(); } blockBlob.ServiceClient.Credentials.SignRequest(req); req.GetResponse().Close(); AzureUtils.ReleaseLease(logger, blockBlob, leaseId, AzureBlobLeaseTimeout); // Release Lease on Blob return(true); } catch (Exception e) { Utils.structuredLog(logger, "E", e.Message + ". UploadConfig_Azure, configZipPath: " + configZipPath + ". " + e); AzureUtils.ReleaseLease(logger, blockBlob, leaseId, AzureBlobLeaseTimeout); return(false); } }