internal static async Task ReplicateBlobAsync(PhotoModel model, TextWriter log) { //The source connection string needs to be in AppSettings using the //storage account name. var sourceConnectionString = ConfigurationManager.AppSettings[model.StorageAccountName]; //The target connection string is the local region's storage account var targetConnectionString = SettingsHelper.LocalStorageConnectionString; //Copy from the upload container to the photos container, // potentially across storage accounts await log.WriteLineAsync("sourceConnectionString: " + sourceConnectionString); await log.WriteLineAsync("targetConnectionString: " + targetConnectionString); var storageRepo = new StorageRepository(sourceConnectionString); var container = await storageRepo.ReplicateBlobAsync( targetConnectionString, StorageConfig.UserUploadBlobContainerName, StorageConfig.PhotosBlobContainerName, model.ServerFileName, log); //Monitor the copy operation and wait for it to finish //before proceeding await storageRepo.MonitorCopy(container, model.ServerFileName, log); }
internal static async Task SaveToRedisAsync(PhotoModel p, TextWriter log) { //Use repository to add to all caches IDatabase cache = RedisCache.Connection.GetDatabase(); RedisRepository repo = new RedisRepository(cache); await repo.AddPhotoToCachesAsync(p); }
internal static async Task SaveToTableStorageAsync(PhotoModel p, TextWriter log) { var storageConnectionString = SettingsHelper.LocalStorageConnectionString; var repo = new StorageRepository(storageConnectionString); var result = await repo.SaveToTableStorageAsync(DAL.Azure.StorageConfig.TableName, p); await log.WriteLineAsync("Save to table HTTP result: " + result); }
// This function will get triggered/executed when a new message is written // on an Azure Queue called queue. public static async Task ProcessQueueMessage( [QueueTrigger("uploadqueue")] string message, TextWriter log) { log.WriteLineAsync(message).Wait(); var m = message.Split(','); await log.WriteAsync("Message received: " + m); var model = new PhotoModel { ID = m[0], ServerFileName = m[1], StorageAccountName = m[2], Owner = m[3], OwnerName = m[4], BlobURL = m[5], OriginRegion = m[6] }; //Copy blob from source to destination await log.WriteAsync("Replicating blob..."); await Helpers.ReplicateBlobAsync(model, log); try { //Change the blob URL to point to the new location! await log.WriteAsync("Getting blob URIs"); string storageConnectionString = SettingsHelper.LocalStorageConnectionString; var repo = new StorageRepository(storageConnectionString); model.BlobURL = repo.GetBlobURI(model.ServerFileName, StorageConfig.PhotosBlobContainerName).ToString(); model.ThumbnailURL = repo.GetBlobURI(model.ServerFileName, StorageConfig.ThumbnailsBlobContainerName).ToString(); //Create thumbnail await log.WriteAsync("Creating thumbnail"); await Helpers.CreateThumbnailAsync(repo, model.ServerFileName, log); //Store in table storage await log.WriteAsync("Saving to table storage"); await Helpers.SaveToTableStorageAsync(model, log); //Add to Redis cache await log.WriteAsync("Saving to Redis"); await Helpers.SaveToRedisAsync(model, log); } catch (Exception oops) { await log.WriteLineAsync(oops.Message); } }
/// <summary> /// Saves an entity to Azure table storage /// </summary> /// <param name="tableName">The name of the table to save to</param> /// <param name="model">The PhotoModel object to be saved</param> /// <returns>System.String - the HTTP status code of the save operation</returns> public async Task<string> SaveToTableStorageAsync( string tableName, PhotoModel model) { //We use the DateAdded field for cross-partition queries DateTime now = System.DateTime.Now; model.DateAdded = now; PhotoEntity entity = new PhotoEntity(model); //These properties are used in a full table scan to populate //all photos for all users. Needed as some way to get the //items for a single day for all users. entity.DayAdded = now.Day; entity.MonthAdded = now.Month; entity.YearAdded = now.Year; var client = _account.CreateCloudTableClient(); var table = client.GetTableReference(tableName); var operation = TableOperation.InsertOrReplace(entity); var result = await table.ExecuteAsync(operation); //TODO: Do we need to check the HTTP status code here? return result.HttpStatusCode.ToString(); }