public static void DoEvery(CloudBlob blob, TimeSpan interval, Action action) { while (true) { var lastPerformed = DateTimeOffset.MinValue; using (var arl = new AutoRenewLease(blob)) { if (arl.HasLease) { blob.FetchAttributes(); DateTimeOffset.TryParseExact(blob.Metadata["lastPerformed"], "R", CultureInfo.CurrentCulture, DateTimeStyles.AdjustToUniversal, out lastPerformed); if (DateTimeOffset.UtcNow >= lastPerformed + interval) { action(); lastPerformed = DateTimeOffset.UtcNow; blob.Metadata["lastPerformed"] = lastPerformed.ToString("R"); blob.SetMetadata(arl.leaseId); } } } var timeLeft = (lastPerformed + interval) - DateTimeOffset.UtcNow; var minimum = TimeSpan.FromSeconds(5); // so we're not polling the leased blob too fast Thread.Sleep( timeLeft > minimum ? timeLeft : minimum); } }
/// <summary> /// Check whether the specified blob exists /// </summary> /// <param name="blob">Blob to check</param> /// <returns>true if specified blob exists, and false if it doesn't</returns> private static bool BlobExists(CloudBlob blob) { try { // try fetching Attributes blob.FetchAttributes(); return true; } catch (StorageClientException e) { if (e.ErrorCode == StorageErrorCode.ResourceNotFound) { // if failed with ResourceNotFound error code, then the blob doesn't exist return false; } else { // something else went wrong //throw; Console.Write(e); return false; } } catch (Exception e) { Console.Write(e); return false; } }
public static void Read(BlobRequestOptions mapped, CloudBlob blob, ReaderDelegate reader) { blob.FetchAttributes(mapped); var props = MapFetchedAttrbitues(blob); var compression = blob.Properties.ContentEncoding ?? ""; var md5 = blob.Metadata[LokadHashFieldName]; switch (compression) { case "gzip": using (var stream = blob.OpenRead(mapped)) { ReadAndVerifyHash(stream, s => { // important is not to flush the decompression stream using (var decompress = new GZipStream(s, CompressionMode.Decompress, true)) { reader(props, decompress); } }, md5); } break; case "": using (var stream = blob.OpenRead(mapped)) { ReadAndVerifyHash(stream, s => reader(props, s), md5); } break; default: var error = string.Format("Unsupported ContentEncoding '{0}'", compression); throw new InvalidOperationException(error); } }
/// <summary> /// Copies the full binary contents of the given blob to the given HTTP response. /// </summary> private static void CopyContents(CloudBlob blob, HttpResponseBase response, long offset = 0) { blob.FetchAttributes(); response.BufferOutput = false; response.AddHeader("Content-Length", blob.Attributes.Properties.Length.ToString()); response.Flush(); using (var reader = blob.OpenRead()) { reader.Seek(offset, System.IO.SeekOrigin.Begin); byte[] buffer = new byte[1024 * 4]; // 4KB buffer while (reader.CanRead) { int numBytes = reader.Read(buffer, 0, buffer.Length); if (numBytes <= 0) break; response.BinaryWrite(buffer); response.Flush(); } } }
// The mutex must already exists public Mutex(CloudBlobContainer container, string mutexName, Exception e) { blob = container.GetBlobReference(mutexName); byte[] b1 = { 1 }; BlobRequestOptions requestOpt = new BlobRequestOptions(); bool keepGoing = true; string oldEtag = ""; int lastChange = 0; do { byte[] b; string eTag; try { blob.FetchAttributes(); eTag = blob.Attributes.Properties.ETag; if (eTag != oldEtag) { lastChange = Environment.TickCount; oldEtag = eTag; } b = blob.DownloadByteArray(); } catch (Exception) { throw e; } requestOpt.AccessCondition = AccessCondition.IfMatch(eTag); if (b[0] == 0 || Environment.TickCount - lastChange > 3000) // on ne peut garder un lock plus de 3 s { try { blob.UploadByteArray(b1, requestOpt); keepGoing = false; } catch (StorageClientException ex) { if (ex.ErrorCode != StorageErrorCode.ConditionFailed) throw; } } else Thread.Sleep(50); // constante arbitraire } while (keepGoing); }
public bool Exists(CloudBlob blob) { try { blob.FetchAttributes(); return true; } catch (StorageClientException e) { if (e.ErrorCode == StorageErrorCode.ResourceNotFound) { return false; } else { throw; } } }
private void DownloadBlobInParallel(CloudBlob blob, Stream stream) { try { blob.FetchAttributes(); var order = new List<string>(); var blocksToDownload = new Queue<Block>(); CalculateBlocks(blocksToDownload, (int)blob.Properties.Length, order); ExecuteInParallel(() => AsLongAsThereAre(blocksToDownload, block => { var s = DownloadBlockFromBlob(blob, block, blocksToDownload); if (s == null) return; var buffer = new byte[BlockSize]; ExtractBytesFromBlockIntoBuffer(buffer, s, block); lock (stream) { stream.Position = block.Offset; stream.Write(buffer, 0,block.Length); } })); stream.Seek(0, SeekOrigin.Begin); } catch (StorageClientException ex) // to handle race conditions between multiple active instances. { logger.Warn(ex.Message); } catch (StorageServerException ex) // prevent azure hickups from hurting us. { logger.Warn(ex.Message); } }
private void DownloadBlobInParallel(CloudBlob blob, Stream stream) { blob.FetchAttributes(); var order = new List<string>(); var blocksToDownload = new Queue<Block>(); CalculateBlocks(blocksToDownload, (int)blob.Properties.Length, order); ExecuteInParallel(() => AsLongAsThereAre(blocksToDownload, block => { var s = DownloadBlockFromBlob(blob, block, blocksToDownload); if (s == null) return; var buffer = new byte[BlockSize]; ExtractBytesFromBlockIntoBuffer(buffer, s, block); lock (stream) { stream.Position = block.Offset; stream.Write(buffer, 0,block.Length); } })); stream.Seek(0, SeekOrigin.Begin); }
private static void FetchMetadataIfMissing(CloudBlob blob) { if(blob.Metadata.Count == 0) { try { blob.FetchAttributes(); } catch(StorageClientException stEx) { if (stEx.ErrorCode == StorageErrorCode.BlobNotFound || stEx.ErrorCode == StorageErrorCode.ResourceNotFound) return; throw; } } }
static bool Exists(CloudBlob blob) { try { blob.FetchAttributes(); return true; } catch (StorageClientException e) { switch (e.ErrorCode) { case StorageErrorCode.ContainerNotFound: case StorageErrorCode.ResourceNotFound: return false; } throw; } }
private static void HandlePublicBlobRequestWithCacheSupport(HttpContext context, CloudBlob blob, HttpResponse response) { // Set the cache request properties as IIS will include them regardless for now // even when we wouldn't want them on 304 response... response.Cache.SetMaxAge(TimeSpan.FromMinutes(0)); response.Cache.SetCacheability(HttpCacheability.Private); var request = context.Request; blob.FetchAttributes(); string ifNoneMatch = request.Headers["If-None-Match"]; string ifModifiedSince = request.Headers["If-Modified-Since"]; if (ifNoneMatch != null) { if (ifNoneMatch == blob.Properties.ETag) { response.ClearContent(); response.StatusCode = 304; return; } } else if (ifModifiedSince != null) { DateTime ifModifiedSinceValue; if (DateTime.TryParse(ifModifiedSince, out ifModifiedSinceValue)) { ifModifiedSinceValue = ifModifiedSinceValue.ToUniversalTime(); if (blob.Properties.LastModifiedUtc <= ifModifiedSinceValue) { response.ClearContent(); response.StatusCode = 304; return; } } } var fileName = blob.Name.Contains("/MediaContent/") ? request.Path : blob.Name; response.ContentType = StorageSupport.GetMimeType(fileName); //response.Cache.SetETag(blob.Properties.ETag); response.Headers.Add("ETag", blob.Properties.ETag); response.Cache.SetLastModified(blob.Properties.LastModifiedUtc); blob.DownloadToStream(response.OutputStream); }
private static void BreakTestLeaseIfExists(CloudBlob blob) { blob.FetchAttributes(); if (blob.Metadata.AllKeys.Contains(leasedForTestTag)) BreakLease(blob); }
public static List<CloudBlob> ListBlobs() { List<IListBlobItem> blobItemList = null; List<CloudBlob> blobList = null; try { string storageAccountConnection = string.Empty; storageAccountConnection = ConfigurationManager.AppSettings["StorageAccount.ConnectionString"].ToString(); Logger.Write("Storage Connection: " + storageAccountConnection); // If you want to use Windows Azure cloud storage account, use the following // code (after uncommenting) instead of the code above. cloudStorageAccount = CloudStorageAccount.Parse(storageAccountConnection); Logger.Write("Blob Uri: " + cloudStorageAccount.BlobEndpoint.AbsoluteUri); // Create the blob client, which provides // authenticated access to the Blob service. blobClient = cloudStorageAccount.CreateCloudBlobClient(); string deploymentPackageFolderString = string.Empty; deploymentPackageFolderString = ConfigurationManager.AppSettings["DeploymentPackageFolder"].ToString(); Logger.Write("Deployment Package Folder: " + deploymentPackageFolderString); // Get the container reference. blobContainer = blobClient.GetContainerReference(deploymentPackageFolderString); // Create the container if it does not exist. blobContainer.CreateIfNotExist(); // Set permissions on the container. containerPermissions = new BlobContainerPermissions(); // This sample sets the container to have public blobs. Your application // needs may be different. See the documentation for BlobContainerPermissions // for more information about blob container permissions. containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; blobContainer.SetPermissions(containerPermissions); BlobRequestOptions blobReqOptions = new BlobRequestOptions(); blobReqOptions.BlobListingDetails = BlobListingDetails.All; blobReqOptions.Timeout = new TimeSpan(0, 5, 0); blobReqOptions.UseFlatBlobListing = true; blobItemList = blobContainer.ListBlobs(blobReqOptions).ToList(); if (blobList == null) { blobList = new List<CloudBlob>(); } foreach (IListBlobItem blobItem in blobItemList) { Logger.Write("Blob Uri: " + blobItem.Uri.ToString()); CloudBlob blobEntry = new CloudBlob(blobItem.Uri.ToString()); blobEntry.FetchAttributes(); blobList.Add(blobEntry); } } catch (System.Exception ex) { Logger.Write(string.Format("Error in List<IListBlobItem> ListBlobs() Error: {0}", ex.Message)); blobList = null; } return blobList; }