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); } }
/// <summary> /// Cast out blob instance and bind it's metadata to metadata repeater /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void OnBlobDataBound(object sender, ListViewItemEventArgs e) { if (e.Item.ItemType == ListViewItemType.DataItem) { var metadataRepeater = e.Item.FindControl("blobMetadata") as Repeater; var blob = ((ListViewDataItem)e.Item).DataItem as CloudBlob; // If this blob is a snapshot, rename button to "Delete Snapshot" if (blob != null) { if (blob.SnapshotTime.HasValue) { var delBtn = e.Item.FindControl("deleteBlob") as LinkButton; if (delBtn != null) { delBtn.Text = "Delete Snapshot"; var snapshotRequest = BlobRequest.Get(new Uri(delBtn.CommandArgument), 0, blob.SnapshotTime.Value, null); delBtn.CommandArgument = snapshotRequest.RequestUri.AbsoluteUri; } var snapshotBtn = e.Item.FindControl("SnapshotBlob") as LinkButton; if (snapshotBtn != null) { snapshotBtn.Visible = false; } } if (metadataRepeater != null) { // bind to metadata metadataRepeater.DataSource = from key in blob.Metadata.AllKeys select new { Name = key, Value = blob.Metadata[key] }; metadataRepeater.DataBind(); } } } }
private Stream DownloadBlockFromBlob(CloudBlob blob, Block block, Queue <Block> blocksToDownload) { try { var blobGetRequest = BlobRequest.Get(blob.Uri, 60, null, null); blobGetRequest.Headers.Add("x-ms-range", string.Format(CultureInfo.InvariantCulture, "bytes={0}-{1}", block.Offset, block.Offset + block.Length - 1)); var credentials = blob.ServiceClient.Credentials; credentials.SignRequest(blobGetRequest); var response = blobGetRequest.GetResponse() as HttpWebResponse; return(response != null?response.GetResponseStream() : null); } catch { if (++block.Attempt > MaxRetries) { throw; } lock (blocksToDownload) blocksToDownload.Enqueue(block); } return(null); }
/// <summary> /// Returns the snapshot's *full* URI including the snapshot date/time. /// <param name="blob">The blob to get the URL of</param> /// </summary> public static Uri GetSnapshotUri(CloudBlob blob) { return(BlobRequest.Get(blob.Uri, 0, blob.SnapshotTime.Value, null).Address); }
public byte[] DownloadChunk(string blobName, List <ChunkInfo> blobMetadata, int chunkIndex) { long offset = 0; int size = 0; foreach (ChunkInfo chunk in blobMetadata) { if (chunk.chunkIndex == chunkIndex) { if (chunkCompressionType == CompressionType.None) { size = chunk.rsize; } else { size = chunk.csize; } break; } else { if (chunkCompressionType == CompressionType.None) { offset += chunk.rsize; } else { offset += chunk.csize; } } } byte[] buffer = new byte[size]; try { CloudBlockBlob blockBlob = GetBlockBlobReference(blobName, false); HttpWebRequest blobGetRequest = BlobRequest.Get(blockBlob.Uri, 60, null, null); // Add header to specify the range blobGetRequest.Headers.Add("x-ms-range", string.Format(System.Globalization.CultureInfo.InvariantCulture, "bytes={0}-{1}", offset, offset + size - 1)); blobGetRequest.Headers.Add("x-ms-range-get-content-md5", "true"); // this tell azure to compute md5 of the byte range and return in the Content-MD5 header // Note: it only works for byte ranges that are <= 4MB. So will not work for chunk sizes more than that // Sign request. StorageCredentials credentials = blockBlob.ServiceClient.Credentials; credentials.SignRequest(blobGetRequest); // Read chunk. while (true) { using (HttpWebResponse response = blobGetRequest.GetResponse() as HttpWebResponse) { using (Stream stream = response.GetResponseStream()) { int offsetInBuffer = 0; int remaining = size; while (remaining > 0) { int read = stream.Read(buffer, offsetInBuffer, remaining); offsetInBuffer += read; remaining -= read; } } string contentMD5 = response.GetResponseHeader("Content-MD5"); if (!GetMD5FromStream(buffer).Equals(contentMD5, StringComparison.CurrentCultureIgnoreCase)) { structuredLog("ER", "Content md5 does not match in download of chunk. retrying"); } else { break; } } } if (logger != null) { logger.Log("Start Synchronizer Decrypt Chunk"); } Byte[] ret = Decrypt(buffer); if (logger != null) { logger.Log("End Synchronizer Decrypt Chunk"); } if (logger != null) { logger.Log("Start Synchronizer Decompress Chunk"); } ret = Decompress(ret); if (logger != null) { logger.Log("End Synchronizer Decompress Chunk"); } return(ret); } catch (Exception e) { structuredLog("E", " Exception in DownloadChunk: " + e); return(null); } }
public static bool DownloadConfig(string downloadedZipPath, string AzureAccountName, string AzureAccountKey, string orgID, string studyID, string homeID, string configFilename, NLog.Logger logger = null) { Microsoft.WindowsAzure.CloudStorageAccount storageAccount = null; Microsoft.WindowsAzure.StorageClient.CloudBlobClient blobClient = null; Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container = null; Microsoft.WindowsAzure.StorageClient.CloudBlockBlob blockBlob = null; string leaseId = null; try { storageAccount = new Microsoft.WindowsAzure.CloudStorageAccount(new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(AzureAccountName, AzureAccountKey), true); blobClient = storageAccount.CreateCloudBlobClient(); container = blobClient.GetContainerReference(AzureConfigContainerName); if (configFilename == PackagerHelper.ConfigPackagerHelper.actualConfigFileName) { blockBlob = container.GetBlockBlobReference(ActualConfigBlobName(orgID, studyID, homeID, configFilename)); } else if (configFilename == PackagerHelper.ConfigPackagerHelper.desiredConfigFileName) { blockBlob = container.GetBlockBlobReference(DesiredConfigBlobName(orgID, studyID, homeID, configFilename)); } bool blobExists = BlockBlobExists(blockBlob); if (blobExists) { leaseId = AcquireLease(blockBlob, logger); // Acquire Lease on Blob } else { return(false); } if (blobExists && leaseId == null) { if (null != logger) { logger.Error("AcquireLease on Blob: " + ActualConfigBlobName(orgID, studyID, homeID, configFilename) + " 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(); ReleaseLease(blockBlob, leaseId); // Release Lease on Blob return(true); } catch (Exception e) { if (null != logger) { logger.ErrorException("DownloadConfig_Azure, downloadZipPath: " + downloadedZipPath, e); } ReleaseLease(blockBlob, leaseId); return(false); } }