/// <summary> /// Insert image in blobs store /// </summary> /// <param name="blobHandle">Blob handle</param> /// <param name="stream">Blob stream</param> /// <param name="contentType">Content type</param> /// <param name="cacheTTL">Time to live in cache</param> /// <returns>Insert image task</returns> public async Task InsertImage(string blobHandle, Stream stream, string contentType, TimeSpan cacheTTL) { CBStore store = await this.blobStoreManager.GetStore(ContainerIdentifier.Images); string containerName = this.blobStoreManager.GetContainerName(ContainerIdentifier.Images); await store.CreateBlobAsync(containerName, blobHandle, stream, contentType, cacheTTL); }
/// <summary> /// Delete image from blobs store /// </summary> /// <param name="blobHandle">Blob handle</param> /// <returns>Delete image task</returns> public async Task DeleteImage(string blobHandle) { CBStore store = await this.blobStoreManager.GetStore(ContainerIdentifier.Images); string containerName = this.blobStoreManager.GetContainerName(ContainerIdentifier.Images); await store.DeleteBlobAsync(containerName, blobHandle); }
/// <summary> /// Get store from container identifier /// </summary> /// <param name="containerIdentifier">Container identifier</param> /// <returns>CB store</returns> public async Task <CBStore> GetStore(ContainerIdentifier containerIdentifier) { ContainerDescriptor containerDescriptor = ContainerDescriptorProvider.Containers[containerIdentifier]; string azureStorageConnectionString = await this.connectionStringProvider.GetBlobsAzureStorageConnectionString(containerDescriptor.AzureStorageInstanceType); string azureCdnUrl = await this.connectionStringProvider.GetAzureCdnUrl(containerDescriptor.AzureCdnInstanceType); string uniqueStoreIdentity = string.Join(":", azureStorageConnectionString, azureCdnUrl); // cachedStoreObjects is a thread-safe dictionary (ConcurrentDictionary). If uniqueStoreIdentity is not present // in cachedStoreObjects, try adding it. Since GetStore can be called concurrently by // different threads, it is possible for two (or more) threads to attempt inserting uniqueStoreIdentity // concurrently in the cachedStoreObjects. That's ok, because the call to TryAdd is guaranteed to be thread-safe. // One of the threads will not be able to insert (i.e., TryAdd will return false), but the code will happily execute // and fall through to the return statement. // This code makes no use of locking on the common path (i.e., reads of cachedStoreObjects). if (!cachedStoreObjects.ContainsKey(uniqueStoreIdentity)) { AzureBlobStorage azureBlobStorage = new AzureBlobStorage(azureStorageConnectionString); azureBlobStorage.BlobRequestOptions = AzureStorageConfiguration.GetBlobRequestOptions(); AzureCdn azureCdn = new AzureCdn(azureCdnUrl); CBStore store = new CBStore(azureBlobStorage, azureCdn); cachedStoreObjects.TryAdd(uniqueStoreIdentity, store); } return(cachedStoreObjects[uniqueStoreIdentity]); }
/// <summary> /// Check if image exists /// </summary> /// <param name="blobHandle">Blob handle</param> /// <returns>A value indicating whether the image exists</returns> public async Task <bool> ImageExists(string blobHandle) { CBStore store = await this.blobStoreManager.GetStore(ContainerIdentifier.Images); string containerName = this.blobStoreManager.GetContainerName(ContainerIdentifier.Images); return(await store.BlobExists(containerName, blobHandle)); }
/// <summary> /// Query blob /// </summary> /// <param name="blobHandle">Blob handle</param> /// <returns>Blob item</returns> public async Task <IBlobItem> QueryBlob(string blobHandle) { CBStore store = await this.blobStoreManager.GetStore(ContainerIdentifier.Blobs); string containerName = this.blobStoreManager.GetContainerName(ContainerIdentifier.Blobs); Blob blob = await store.QueryBlobAsync(containerName, blobHandle); return(new BlobItem() { Stream = blob.Stream, ContentType = blob.ContentType }); }
/// <summary> /// Query CDN url for image /// </summary> /// <param name="blobHandle">Image handle</param> /// <returns>CDN url</returns> public async Task <Uri> QueryImageCdnUrl(string blobHandle) { // return null if input is null if (string.IsNullOrWhiteSpace(blobHandle)) { return(null); } CBStore store = await this.blobStoreManager.GetStore(ContainerIdentifier.Images); string containerName = this.blobStoreManager.GetContainerName(ContainerIdentifier.Images); return(store.QueryCdnUrl(containerName, blobHandle)); }
/// <summary> /// deletes the blob container (and therefore all the blobs) /// </summary> /// <param name="blobStoreManager">cached blob store manager</param> /// <param name="azureBlobStorageConnectionString">connection string to azure blob storage</param> /// <returns>delete task</returns> private static async Task DeleteAzureBlobs(ICBStoreManager blobStoreManager, string azureBlobStorageConnectionString) { Console.WriteLine("Deleting all blobs from Azure Blob Store..."); AzureBlobStorage azureBlobStorage = new AzureBlobStorage(azureBlobStorageConnectionString); CBStore store = new CBStore(azureBlobStorage, null); foreach (ContainerIdentifier containerIdentifier in Enum.GetValues(typeof(ContainerIdentifier))) { if (!ContainerDescriptorProvider.Containers.ContainsKey(containerIdentifier)) { Console.WriteLine(" " + containerIdentifier.ToString() + " - Descriptor not found"); continue; } string containerName = blobStoreManager.GetContainerName(containerIdentifier); await store.DeleteContainerAsync(containerName); Console.WriteLine(" " + containerIdentifier.ToString() + " - Container Deleted"); } }