private static async Task InitializeAsync(string settingsFile = null) { using (Stream settingsStream = settingsFile == null ? Assembly.GetExecutingAssembly().GetManifestResourceStream("TestCLI.settings.json") : new FileStream(settingsFile, FileMode.Open, FileAccess.Read)) using (var settingsReader = new StreamReader(settingsStream)) using (var textReader = new JsonTextReader(settingsReader)) { dynamic settings = new JsonSerializer().Deserialize(textReader); VisionServiceHelper.Url = settings.CognitiveServicesKeys.Url; VisionServiceHelper.ApiKey = settings.CognitiveServicesKeys.Key; BlobStorageHelper.ConnectionString = settings.AzureStorage.ConnectionString; BlobStorageHelper.ContainerName = settings.AzureStorage.BlobContainer; blobStorage = await BlobStorageHelper.BuildAsync(); CosmosDBHelper <ImageMetadata> .AccessKey = settings.CosmosDB.Key; CosmosDBHelper <ImageMetadata> .EndpointUri = settings.CosmosDB.EndpointURI; CosmosDBHelper <ImageMetadata> .DatabaseName = settings.CosmosDB.DatabaseName; CosmosDBHelper <ImageMetadata> .CollectionName = settings.CosmosDB.CollectionName; cosmosDb = CosmosDBHelper <ImageMetadata> .GetInstance; } }
private static async Task InitializeAsync(string settingsFile = null) { using (Stream settingsStream = settingsFile == null ? Assembly.GetExecutingAssembly().GetManifestResourceStream("TestCLI.settings.json") : new FileStream(settingsFile, FileMode.Open, FileAccess.Read)) using (var settingsReader = new StreamReader(settingsStream)) using (var textReader = new JsonTextReader(settingsReader)) { dynamic settings = new JsonSerializer().Deserialize(textReader); FaceServiceHelper.ApiKey = settings.CognitiveServicesKeys.Face; EmotionServiceHelper.ApiKey = settings.CognitiveServicesKeys.Emotion; VisionServiceHelper.ApiKey = settings.CognitiveServicesKeys.Vision; BlobStorageHelper.ConnectionString = settings.AzureStorage.ConnectionString; BlobStorageHelper.ContainerName = settings.AzureStorage.BlobContainer; blobStorage = await BlobStorageHelper.BuildAsync(); DocumentDBHelper.AccessKey = settings.DocumentDB.Key; DocumentDBHelper.EndpointUri = settings.DocumentDB.EndpointURI; DocumentDBHelper.DatabaseName = settings.DocumentDB.DatabaseName; DocumentDBHelper.CollectionName = settings.DocumentDB.CollectionName; documentDb = await DocumentDBHelper.BuildAsync(); } }
private async void btnUploadImage_OnClick(object sender, RoutedEventArgs e) { if (_storageFile != null) { if (!string.IsNullOrEmpty(txtName.Text) && !string.IsNullOrEmpty(txtContainerName.Text)) { BlobStorageHelper blobStorageHelper = new BlobStorageHelper(); await blobStorageHelper.UploadImageAsync(txtContainerName.Text, $"{txtName.Text}.jpg", _storageFile); MessageDialog messageDialog = new MessageDialog("La imagen se ha subido con éxito."); await messageDialog.ShowAsync(); } else { MessageDialog messageDialog = new MessageDialog("Necesitas asignar un nombre a la imagen y al contenedor"); await messageDialog.ShowAsync(); } } else { MessageDialog messageDialog = new MessageDialog("Debes tener una imagen para subir al blob"); await messageDialog.ShowAsync(); } }
/// <summary> /// Process the image. The image is given to the model ML and if a /// valid recognition is found then the image is uploaded to storage. /// Processing times are tracked. /// </summary> /// <param name="body"> /// An ImageBody object containing the image from the camera. /// </param> public static void Process(BlobStorageHelper blobHelper, ModuleClient client, IProcessImage imageProc, ImageBody body) { ImageProcessor proc = new ImageProcessor(blobHelper, client, imageProc.ProcessorType, body); if (!body.SkipMlProcessing) { // Perform and measure elapsed time for the ML model work DateTime startTime = DateTime.Now; proc.features = imageProc.Process(body.SmallImageRGB); proc.recognitionDuration = DateTime.Now - startTime; // Loop to the next recognition task without waiting for the report to process if (proc.features != null) { Task reportTask = new Task(() => proc.Report()); reportTask.Start(); } } else { // Don't process ML or send any messages; just upload to BLOB try { proc.UploadToBlob(); } catch (Exception ex) { Console.WriteLine($" Failed to upload image to BLOB store: {ex.Message}"); } } }
/// <summary> /// Setup custom rules overriding autowired ones, for example in cases /// where an interface has multiple implementations, and cases where /// a singleton is preferred to new instances. /// </summary> private static void SetupCustomRules(ContainerBuilder builder) { // Make sure the configuration is read only once. IConfig config = new Config(new ConfigData(new Logger(Uptime.ProcessId))); builder.RegisterInstance(config).As <IConfig>().SingleInstance(); // Service configuration is generated by the entry point, so we // prepare the instance here. builder.RegisterInstance(config.LoggingConfig).As <ILoggingConfig>().SingleInstance(); builder.RegisterInstance(config.ServicesConfig).As <IServicesConfig>().SingleInstance(); builder.RegisterInstance(config.BlobStorageConfig).As <IBlobStorageConfig>().SingleInstance(); // Instantiate only one logger var logger = new Logger(Uptime.ProcessId, config.LoggingConfig); builder.RegisterInstance(logger).As <ILogger>().SingleInstance(); var threadWrapper = new ThreadWrapper(); // Auth and CORS setup Auth.Startup.SetupDependencies(builder, config); // By default the DI container create new objects when injecting // dependencies. To improve performance we reuse some instances, // for example to reuse IoT Hub connections, as opposed to creating // a new connection every time. //builder.RegisterType<Foo>().As<IFoo>().SingleInstance(); ICloudStorageWrapper cloudStorageWrapper = new CloudStoragWrapper(); IBlobStorageHelper blobStorageHelper = new BlobStorageHelper(config.BlobStorageConfig, cloudStorageWrapper, logger); IFileWrapper fileWrapper = new FileWrapper(); IDeviceGroupsWriter deviceGroupsWriter = new DeviceGroupsWriter(config.BlobStorageConfig, blobStorageHelper, fileWrapper, logger); builder.RegisterInstance(deviceGroupsWriter).As <IDeviceGroupsWriter>().SingleInstance(); IHttpClient httpClient = new HttpClient(logger); IDevicesClient devicesClient = new DevicesClient(httpClient, config.ServicesConfig, logger); builder.RegisterInstance(devicesClient).As <IDevicesClient>().SingleInstance(); IDeviceGroupsClient deviceGroupsClient = new DeviceGroupsClient( httpClient, devicesClient, config.ServicesConfig, logger, threadWrapper); builder.RegisterInstance(deviceGroupsClient).As <IDeviceGroupsClient>().SingleInstance(); // Event Hub Classes IEventProcessorHostWrapper eventProcessorHostWrapper = new EventProcessorHostWrapper(); builder.RegisterInstance(eventProcessorHostWrapper).As <IEventProcessorHostWrapper>().SingleInstance(); IEventHubStatus eventHubStatus = new EventHubStatus(); builder.RegisterInstance(eventHubStatus).As <IEventHubStatus>().SingleInstance(); IEventProcessorFactory eventProcessorFactory = new DeviceEventProcessorFactory(eventHubStatus, config.ServicesConfig, logger); builder.RegisterInstance(eventProcessorFactory).As <IEventProcessorFactory>().SingleInstance(); }
/// <summary> /// Retrieve an image and its metadata from a camera. /// </summary> /// <returns>The image and its metadata</returns> public ImageBody GetImage() { DateTime now = BlobStorageHelper.GetImageUtcTime(); string nowString = BlobStorageHelper.FormatImageUtcTime(now); byte[] imageBytes = this.ImageSource.RequestImage(); if (imageBytes != null) { ByteString image = ByteString.CopyFrom(imageBytes); ByteString smallImage = Camera.ShrinkJpegTo300x300(imageBytes); ImageBody result = new ImageBody { CameraId = CameraId, Time = nowString, Type = "jpg", Image = image, SmallImageRGB = smallImage, SkipMlProcessing = this.SkipMLProcessing }; return(result); } else { return(null); } }
public ActionResult Create(AuctionItemCreateViewModel v) { DateTime minDate = DateTime.Now; DateTime maxDate = minDate.AddDays(7); bool isEndDateValid = v.EndDateTime > minDate && v.EndDateTime < maxDate; if (ModelState.IsValid && isEndDateValid) { List <ItemImage> images = new List <ItemImage>(); for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i]; if (file.ContentLength > 0 && file.ContentLength <= 4194304 && (file.ContentType == "image/gif" || file.ContentType == "image/jpeg" || file.ContentType == "image/png")) { images.Add(new ItemImage(BlobStorageHelper.UploadBlob(User.Identity.GetUserId(), Guid.NewGuid().ToString(), file))); } } var a = new AuctionItem(v, CategoryDB.GetCategoryByID(db, v.SelectedCategory), ApplicationUserDB.GetUserByID(db, User.Identity.GetUserId()), images); AuctionItemDB.Create(db, a); return(RedirectToAction("Index", "AuctionItem")); } if (!isEndDateValid) { ModelState.AddModelError("EndDateTime", $"Auction item end time must be between {minDate} and {maxDate}"); } return(View(v)); }
private async Task <IEnumerable <IListBlobItem> > LoadApplicableListBlobItemsAsync() { CloudBlobContainer container = await BlobStorageHelper.BuildBlobContainerAsync( this.alertsContainerConnectionString, this.alertsStoreContainerName); IEnumerable <IListBlobItem> blobs = await BlobStorageHelper.LoadBlobItemsAsync( async (token) => { return(await container.ListBlobsSegmentedAsync( this.deviceAlertsDataPrefix, true, BlobListingDetails.None, null, token, null, null)); }); if (blobs != null) { blobs = blobs.OrderByDescending(t => BlobStorageHelper.ExtractBlobItemDate(t)); } return(blobs); }
private ImageProcessor(BlobStorageHelper blobHelper, ModuleClient client, string processorType, ImageBody body) { this.moduleClient = client; this.blobHelper = blobHelper; this.processorType = processorType; this.body = body; }
public void TestBackupsToBlobStorageAccount() { string hostName = string.Concat("Host-", Guid.NewGuid().ToString()); var tasks = new List <Task>(); // 1. Trigger the data generator to insert sample documents into the test collection IDataGenerator dataGenerator = new EmployeeSampleDataGenerator(this.client); tasks.Add(Task.Factory.StartNew(() => { dataGenerator.GenerateSampleData().Wait(); })); // 2. Trigger the backup executor to fetch all changes to the source collection and backup the changes to the specified Blob Storage Account BackupExecutor backupExecutor = new BackupExecutor(client, hostName); backupExecutor.ExecuteBackup().Wait(); // 3. Wait for both (1) DataGenerator and (2) BackupExecutor to finish execution Task.WaitAll(tasks.ToArray()); Thread.Sleep(60 * 2 * 1000); //// 4. Validate the number of documents backed up to Blob Storage int numberOfSampleDocumentsGenerated = int.Parse(ConfigurationManager.AppSettings["NumDocumentsToGenerate"]); Console.WriteLine("Count retrieved = {0}", BlobStorageHelper.GetListOfDocumentsBackedUpInContainer(this.CloudBlobClient)); Assert.AreEqual(BlobStorageHelper.GetListOfDocumentsBackedUpInContainer(this.CloudBlobClient), numberOfSampleDocumentsGenerated); }
private async Task <ActionMappingBlobResults> GetActionsAndEtagAsync() { CloudBlobContainer container = await BlobStorageHelper.BuildBlobContainerAsync(_connectionString, _containerName); CloudBlockBlob blob = container.GetBlockBlobReference(_blobName); bool exists = await blob.ExistsAsync(); var mappings = new List <ActionMapping>(); if (exists) { await blob.FetchAttributesAsync(); long blobLength = blob.Properties.Length; if (blobLength > 0) { byte[] existingBytes = new byte[blobLength]; await blob.DownloadToByteArrayAsync(existingBytes, 0); // get the existing mappings in object form string existingJsonData = Encoding.UTF8.GetString(existingBytes); mappings = JsonConvert.DeserializeObject <List <ActionMapping> >(existingJsonData); } return(new ActionMappingBlobResults(mappings, blob.Properties.ETag)); } // if it doesn't exist, return the empty list and an empty string for the ETag return(new ActionMappingBlobResults(mappings, "")); }
private void WriteDataToBlobStorage(JArray jArrayOfChangedDocs, DateTime minTimeStamp, DateTime maxTimeStamp, string partitionId) { // Set the filename for the destination file in Blob Storage to be a combination of the min and max timestamps // across all documents returned by ChangeFeedProcessor string fileName = string.Concat( minTimeStamp.ToString("yyyyMMddTHH:mm:ss"), "-", maxTimeStamp.ToString("yyyyMMddTHH:mm:ss"), "-", jArrayOfChangedDocs.Count); string textToBackup = jArrayOfChangedDocs.ToString(); string containerName = string.Concat(this.SourceContainerName.ToLower().Replace("_", "-"), "-backup-", partitionId); CloudBlobContainer cloudBlobContainer = this.CloudBlobClient.GetContainerReference(containerName); cloudBlobContainer.CreateIfNotExists(); CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName); if (blockBlob.Exists()) { try { fileName = string.Concat(fileName, "-", Guid.NewGuid()); blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName); BlobStorageHelper.WriteStringToBlobStorage(blockBlob, textToBackup, this.MaxRetriesOnRateLimitedWritesToBlobAccount); this.Logger.WriteMessage(string.Format("Sev 3: ActivityId: {0} - Successfully wrote data to blob storage with file name: {1}", this.GuidForLogAnalytics.ToString(), fileName)); this.TrackSuccessfulBatchesOfBackups(containerName, jArrayOfChangedDocs.Count, maxTimeStamp); } catch (Exception ex) { byte[] compressedByteArray = CompressDocumentsInJArray(jArrayOfChangedDocs); this.TrackFailedBatchesOfBackups(containerName, fileName, jArrayOfChangedDocs.Count, compressedByteArray, ex); } } else { try { BlobStorageHelper.WriteStringToBlobStorage(blockBlob, textToBackup, this.MaxRetriesOnRateLimitedWritesToBlobAccount); this.Logger.WriteMessage(string.Format("Sev 3: ActivityId: {0} - Successfully wrote uncompressed data to blob storage with file name: {1}", this.GuidForLogAnalytics.ToString(), fileName)); this.TrackSuccessfulBatchesOfBackups(containerName, jArrayOfChangedDocs.Count, maxTimeStamp); } catch (Exception ex) { byte[] compressedByteArray = CompressDocumentsInJArray(jArrayOfChangedDocs); this.TrackFailedBatchesOfBackups(containerName, fileName, jArrayOfChangedDocs.Count, compressedByteArray, ex); } } }
//protected void btnUploadFile_Click(object sender, EventArgs e) //{ // if (FileUpload1.HasFile) // { // filePath = Server.MapPath("~/Uploads/"); // fileName = FileUpload1.PostedFile.FileName; // fileSize = FileUpload1.PostedFile.ContentLength; // fileExtension = Path.GetExtension(FileUpload1.FileName); // if (fileExtension.ToLower() != ".pdf") // { // lblMessage.Text = "Only files with .pdf extension are allowed."; // } // else if (FileUpload1.PostedFile.ContentLength > 50000000) // { // lblMessage.Text = "The maximum size of 50 MB was exceeded."; // } // else // { // FileUpload1.SaveAs(filePath + FileUpload1.FileName); // lblMessage.Text = "File uploaded successfully!"; // lblMessage.CssClass = "text-success"; // pnlViewer.Visible = true; // } // } // else // { // lblMessage.Text = "Please select a file to upload."; // } // this.lblMessage.Visible = true; //} protected void UploadFront(object sender, EventArgs e) { lblMessage.Text = ""; const string CONTAINER = "manuscript"; string frontPath = Request.PhysicalApplicationPath + "Uploads\\"; frontName = FileUploadFront.PostedFile.FileName; int frontSize = FileUploadFront.PostedFile.ContentLength; string frontExtension = Path.GetExtension(FileUploadFront.FileName); if (frontExtension.ToLower() != ".pdf") { lblMessageFront.Text = "Only files with .pdf extension are allowed."; lblMessageFront.CssClass = "text-danger"; } else if (frontSize > 26214400) { lblMessageFront.Text = "The maximum size of 25 MB was exceeded."; lblMessageFront.CssClass = "text-danger"; } else if (frontName.Contains(" ")) { lblMessageFront.Text = "The name of the file must have no spaces."; lblMessageFront.CssClass = "text-danger"; } else { FileUploadFront.SaveAs(frontPath + FileUploadFront.FileName); BlobStorageHelper.CheckContainer(CONTAINER); BlobStorageHelper.UploadBlockBlob(CONTAINER, frontName, frontPath + frontName); hdFrontName.Value = frontName; hdFrontName.Value = frontName; hdFrontPath.Value = frontPath; hdFrontSize.Value = frontSize.ToString(); hdFrontExtension.Value = frontExtension; lblMessageFront.Text = "File uploaded successfully!"; lblMessageFront.CssClass = "text-success"; } if (frontName.Length <= 15) { lblFrontName.Text = frontName; } else { lblFrontName.Text = frontName.Substring(0, 10) + "[...]" + frontExtension; } ClientScript.RegisterStartupScript(this.GetType(), "HideLabel", "<script type=\"text/javascript\">setTimeout(\"document.getElementById('" + lblMessageFront.ClientID + "').style.display='none'\",5000)</script>"); lblMessageFront.Visible = true; tabs.Visible = true; }
/// <summary>Setup Custom rules overriding autowired ones.</summary> private static void SetupCustomRules(ContainerBuilder builder) { // Make sure the configuration is read only once. IConfig config = new Config(new ConfigData(new Logger(Uptime.ProcessId, LogLevel.Info))); builder.RegisterInstance(config).As <IConfig>().SingleInstance(); // Service configuration is generated by the entry point, so we // prepare the instance here. builder.RegisterInstance(config.ServicesConfig).As <IServicesConfig>().SingleInstance(); // Instantiate only one logger // TODO: read log level from configuration var logger = new Logger(Uptime.ProcessId, LogLevel.Debug); builder.RegisterInstance(logger).As <ILogger>().SingleInstance(); // Set up storage client for Cosmos DB var storageClient = new StorageClient(config.ServicesConfig, logger); builder.RegisterInstance(storageClient).As <IStorageClient>().SingleInstance(); // Set up BLOB storage client ICloudStorageWrapper cloudStorageWrapper = new CloudStoragWrapper(); IBlobStorageHelper blobStorageHelper = new BlobStorageHelper(config.BlobStorageConfig, cloudStorageWrapper, logger); builder.RegisterInstance(blobStorageHelper).As <IBlobStorageHelper>().SingleInstance(); // Http IHttpClient httpClient = new HttpClient(logger); builder.RegisterInstance(httpClient).As <IHttpClient>(); // Setup Time Series Insights Client var timeSeriesClient = new TimeSeriesClient(httpClient, config.ServicesConfig, logger); builder.RegisterInstance(timeSeriesClient).As <ITimeSeriesClient>().SingleInstance(); // Auth and CORS setup Auth.Startup.SetupDependencies(builder, config); // By default Autofac uses a request lifetime, creating new objects // for each request, which is good to reduce the risk of memory // leaks, but not so good for the overall performance. //builder.RegisterType<CLASS_NAME>().As<INTERFACE_NAME>().SingleInstance(); builder.RegisterType <UserManagementClient>().As <IUserManagementClient>().SingleInstance(); builder.RegisterType <DiagnosticsClient>().As <IDiagnosticsClient>().SingleInstance(); // Event Hub Classes IEventProcessorHostWrapper eventProcessorHostWrapper = new EventProcessorHostWrapper(); builder.RegisterInstance(eventProcessorHostWrapper).As <IEventProcessorHostWrapper>().SingleInstance(); IEventProcessorFactory eventProcessorFactory = new ActionsEventProcessorFactory(logger, config.ServicesConfig, httpClient); builder.RegisterInstance(eventProcessorFactory).As <IEventProcessorFactory>().SingleInstance(); }
private void DeleteCvFromStorage(string uri) { if (uri != null && uri != "") { BlobStorageHelper objBlobService = new BlobStorageHelper(); objBlobService.DeleteBlobData(uri); } return; }
public void GenerateFileName_ShouldReturnNullIfNoExtension() { // Arrange var helper = new BlobStorageHelper(); //Act var res = helper.GenerateFileName("ddddd"); //Assert res.Should().BeNull(); }
public void TestRestoreToBlobStorageAccount() { string hostName = string.Concat("Host-", Guid.NewGuid().ToString()); string restoreAccountName = ConfigurationManager.AppSettings["RestoreAccountUri"]; string restoreAccountKey = ConfigurationManager.AppSettings["RestoreAccountSecretKey"]; DocumentClient documentClientForRestore = Utilities.CreateDocumentClient(restoreAccountName, restoreAccountKey); var tasks = new List <Task>(); // 1. Trigger the data generator to insert sample documents into the test collection IDataGenerator dataGenerator = new EmployeeSampleDataGenerator(documentClientForRestore); tasks.Add(Task.Factory.StartNew(() => { dataGenerator.GenerateSampleData().Wait(); })); // 2. Trigger the backup executor to fetch all changes to the source collection and backup the changes to the specified Blob Storage Account BackupExecutor backupExecutor = new BackupExecutor(client, hostName); backupExecutor.ExecuteBackup().Wait(); // 3. Wait for both (1) DataGenerator and (2) BackupExecutor to finish execution Task.WaitAll(tasks.ToArray()); Thread.Sleep(60 * 2 * 1000); // 4. Validate the number of documents backed up to Blob Storage int numberOfSampleDocumentsGenerated = int.Parse(ConfigurationManager.AppSettings["NumDocumentsToGenerate"]); Assert.AreEqual(BlobStorageHelper.GetListOfDocumentsBackedUpInContainer(this.CloudBlobClient), numberOfSampleDocumentsGenerated); tasks = new List <Task>(); RestoreExecutor restoreExecutor = new RestoreExecutor(client, hostName); tasks.Add(Task.Factory.StartNew(() => { restoreExecutor.ExecuteRestore().Wait(); })); Task.WaitAll(tasks.ToArray()); string monitoredDatabaseName = ConfigurationManager.AppSettings["DatabaseName"]; string monitoredCollectionName = ConfigurationManager.AppSettings["CollectionName"]; string restoreDatabaseName = ConfigurationManager.AppSettings["RestoreDatabaseName"]; string restoreCollectionName = ConfigurationManager.AppSettings["RestoreCollectionName"]; long documentCountInSourceCollection = CosmosDBHelper.FetchDocumentCountInCollection(this.client, monitoredDatabaseName, monitoredCollectionName).Result; long documentCountInRestoredCollection = CosmosDBHelper.FetchDocumentCountInCollection(documentClientForRestore, restoreDatabaseName, restoreCollectionName).Result; Assert.AreEqual(documentCountInSourceCollection, documentCountInRestoredCollection); }
public void GenerateFileName_ShoudRecognizeExtensionWithManyPointsInName() { // Arrange var helper = new BlobStorageHelper(); //Act var res = helper.GenerateFileName("djwoajdowa.dwo.dwi.dpp.dwd..pdf"); //Assert res.Should().EndWith(".pdf"); }
private async Task PersistRulesToBlobStorageAsync(List <DeviceRuleBlobEntity> blobList) { CloudBlobContainer container = await BlobStorageHelper.BuildBlobContainerAsync(_storageAccountConnectionString, _deviceRulesBlobStoreContainerName); string updatedJson = JsonConvert.SerializeObject(blobList); DateTime saveDate = DateTime.UtcNow.AddMinutes(blobSaveMinutesInTheFuture); string dateString = saveDate.ToString("d", _formatInfo); string timeString = saveDate.ToString("t", _formatInfo); CloudBlockBlob blockBlob = container.GetBlockBlobReference(string.Format(@"{0}\{1}\{2}", dateString, timeString, _blobName)); await blockBlob.UploadTextAsync(updatedJson); }
/// <summary> /// Fetches the Blob container from the Storage account and create it if it is unavailable. /// </summary> /// <param name="partitionId">The partition key range id to generate the blob container name</param> /// <param name="fileName">The file name, which is just the timestamp at which the data is being written with the doc count appended to it</param> /// <param name="compressedByteArray">The compressed byte array, which is a JArray of at most 100 documents, with the Json string Gzip compressed for /// efficient storage into Blob Storage</param> private void WriteCompressedDataToBlob(string partitionId, string fileName, int docCount, byte[] compressedByteArray, DateTime maxTimestampOfBackedUpDocuments) { string containerName = string.Concat(this.SourceContainerName.ToLower().Replace("_", "-"), "-backup-", partitionId); CloudBlobContainer cloudBlobContainer = this.CloudBlobClient.GetContainerReference(containerName); cloudBlobContainer.CreateIfNotExists(); CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName); if (blockBlob.Exists()) { if (!VerifyIfBlobPreviouslyBackedUp(blockBlob, compressedByteArray)) { try { fileName = string.Concat(fileName, "-", Guid.NewGuid()); blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName); BlobStorageHelper.WriteToBlobStorage(blockBlob, compressedByteArray, this.MaxRetriesOnRateLimitedWritesToBlobAccount); this.Logger.WriteMessage(string.Format("Sev 3: ActivityId: {0} - Successfully wrote compressed data to blob storage with file name: {1}", this.GuidForLogAnalytics.ToString(), fileName)); this.TrackSuccessfulBatchesOfBackups(containerName, docCount, maxTimestampOfBackedUpDocuments); } catch (Exception ex) { this.TrackFailedBatchesOfBackups(containerName, fileName, docCount, compressedByteArray, ex); } } else { this.TrackFailedBatchesOfBackups(containerName, fileName, docCount, compressedByteArray, null); } } else { try { BlobStorageHelper.WriteToBlobStorage(blockBlob, compressedByteArray, this.MaxRetriesOnRateLimitedWritesToBlobAccount); this.Logger.WriteMessage(string.Format("Sev 3: ActivityId: {0} - Successfully wrote compressed data to blob storage with file name: {1}", this.GuidForLogAnalytics.ToString(), fileName)); this.TrackSuccessfulBatchesOfBackups(containerName, docCount, maxTimestampOfBackedUpDocuments); } catch (Exception ex) { this.TrackFailedBatchesOfBackups(containerName, fileName, docCount, compressedByteArray, ex); } } }
public async Task <IActionResult> Delete(int Id) { Item item = await ItemDb.GetItemAsync(_context, Id); BlobStorageHelper blobStorageHelper = new BlobStorageHelper(_config); await blobStorageHelper.DeletePhotoBlob(item.PhotoUrl); await blobStorageHelper.DeleteModelBlob(item.ModelUrl); await ItemDb.DeleteItemAsync(_context, item); return(RedirectToAction("Index", "Home")); }
public DeviceGroupComponentTests() { Mock <IBlobStorageConfig> blobStorageConfigMock = new Mock <IBlobStorageConfig>(); this.SetupBlobStorageConfigMock(blobStorageConfigMock); Mock <IServicesConfig> servicesConfigMock = new Mock <IServicesConfig>(); servicesConfigMock.Setup(x => x.ConfigServiceUrl).Returns("serviceurl"); servicesConfigMock.Setup(x => x.InitialIotHubManagerRetryIntervalMs).Returns(0); servicesConfigMock.Setup(x => x.IotHubManagerRetryCount).Returns(5); servicesConfigMock.Setup(x => x.IotHubManagerRetryIntervalIncreaseFactor).Returns(2); this.logMock = new Mock <ILogger>(); this.logMock.Setup(x => x.Warn(It.IsAny <string>(), It.IsAny <Action>())); this.logMock.Setup(x => x.Error(It.IsAny <string>(), It.IsAny <Action>())); var httpClientMock = new Mock <IHttpClient>(); this.devicesClientMock = new Mock <IDevicesClient>(); this.fileWrapperMock = new Mock <IFileWrapper>(); this.fileWrapperMock.Setup(x => x.GetTempFileName()).Returns(TEMP_FILE_NAME); this.fileWrapperMock.Setup(x => x.Exists(It.IsAny <string>())).Returns(true); this.cloudStorageWrapperMock = new Mock <ICloudStorageWrapper>(); this.deviceGroupsClient = new DeviceGroupsClient( httpClientMock.Object, this.devicesClientMock.Object, servicesConfigMock.Object, this.logMock.Object, new ThreadWrapper()); this.timestamp = new DateTime(2018, 4, 20, 10, 0, 0); this.expectedBlobName = $"{this.timestamp.ToString(DATE_FORMAT)}/{this.timestamp.ToString(TIME_FORMAT)}/{FILE_NAME}"; this.expectedConnectionString = $"DefaultEndpointsProtocol=https;AccountName={ACCOUNT_NAME};AccountKey={ACCOUNT_KEY};EndpointSuffix={ENDPOINT_SUFFIX}"; var blobStorageHelper = new BlobStorageHelper( blobStorageConfigMock.Object, this.cloudStorageWrapperMock.Object, this.logMock.Object); this.deviceGroupsWriter = new DeviceGroupsWriter( blobStorageConfigMock.Object, blobStorageHelper, this.fileWrapperMock.Object, this.logMock.Object); }
public void GenerateFileName_ShouldThrowIfNull() { // Arrange var helper = new BlobStorageHelper(); //Act try { helper.GenerateFileName(null); } catch (Exception e) { //Assert e.Should().NotBeNull(); } }
/// <summary> /// Retrieve an image and its metadata from a camera. /// </summary> /// <returns>The image and its metadata</returns> public ImageBody GetImage() { DateTime now = BlobStorageHelper.GetImageUtcTime(); string nowString = BlobStorageHelper.FormatImageUtcTime(now); ImageBody result = this.ImageSource.RequestImages(); if (result != null) { result.CameraId = CameraId; result.Time = nowString; result.Type = "jpg"; } return(result); }
private async Task <bool> InitializeServices() { if (blobStorage == null || cosmosDb == null) { BlobStorageHelper.ContainerName = ConfigurationManager.AppSettings["ContainerName"]; BlobStorageHelper.ConnectionString = ConfigurationManager.AppSettings["StorageConnectionString"]; blobStorage = await BlobStorageHelper.BuildAsync(); CosmosDBHelper.AccessKey = ConfigurationManager.AppSettings["CosmosDBKey"]; CosmosDBHelper.EndpointUri = ConfigurationManager.AppSettings["CosmosDBEndpointURI"]; CosmosDBHelper.DatabaseName = ConfigurationManager.AppSettings["DatabaseName"]; CosmosDBHelper.CollectionName = ConfigurationManager.AppSettings["CollectionName"]; cosmosDb = await CosmosDBHelper.BuildAsync(); } return(true); }
/// <summary> /// Process the image. The image is given to the model ML and if a /// valid recognition is found then the image is uploaded to storage. /// Processing times are tracked. /// </summary> /// <param name="body"> /// An ImageBody object containing the image from the camera. /// </param> public static void Process(BlobStorageHelper blobHelper, ModuleClient client, IProcessImage imageProc, ImageBody body) { ImageProcessor proc = new ImageProcessor(blobHelper, client, imageProc.ProcessorType, body); // Perform and measure elapsed time for the ML model work DateTime startTime = DateTime.Now; proc.features = imageProc.Process(body.SmallImage); proc.recognitionDuration = DateTime.Now - startTime; // Loop to the next recognition task without waiting for the report to process if (proc.features != null) { Task reportTask = new Task(() => proc.Report()); reportTask.Start(); } }
private async Task <bool> ProcessFile(string downloadLocation, string documentId, string fileFormat, string downloadType) { var recordingDownloadUri = new Uri(downloadLocation); var response = callingServerClient.DownloadStreamingAsync(recordingDownloadUri); Logger.LogInformation($"Download {downloadType} response -- >" + response.Result.GetRawResponse()); Logger.LogInformation($"Save downloaded {downloadType} -- >"); string filePath = ".\\" + documentId + "." + fileFormat; using (Stream streamToReadFrom = response.Result.Value) { using (Stream streamToWriteTo = System.IO.File.Open(filePath, FileMode.Create)) { await streamToReadFrom.CopyToAsync(streamToWriteTo); await streamToWriteTo.FlushAsync(); } } if (string.Equals(downloadType, FileDownloadType.Metadata, StringComparison.InvariantCultureIgnoreCase) && System.IO.File.Exists(filePath)) { Root deserializedFilePath = JsonConvert.DeserializeObject <Root>(System.IO.File.ReadAllText(filePath)); recFileFormat = deserializedFilePath.recordingInfo.format; Logger.LogInformation($"Recording File Format is -- > {recFileFormat}"); } Logger.LogInformation($"Starting to upload {downloadType} to BlobStorage into container -- > {containerName}"); var blobStorageHelperInfo = await BlobStorageHelper.UploadFileAsync(blobStorageConnectionString, containerName, filePath, filePath); if (blobStorageHelperInfo.Status) { Logger.LogInformation(blobStorageHelperInfo.Message); Logger.LogInformation($"Deleting temporary {downloadType} file being created"); System.IO.File.Delete(filePath); } else { Logger.LogError($"{downloadType} file was not uploaded,{blobStorageHelperInfo.Message}"); } return(true); }
private async Task <ImageMetadata> SaveData(AnalysisResultViewModel data) { var fileName = Path.GetFileName(data.ImageLocalPath); Func <Task <Stream> > imageCB; try { if (blobStorage == null || cosmosDb == null) { BlobStorageHelper.ContainerName = ConfigurationManager.AppSettings["ContainerName"]; BlobStorageHelper.ConnectionString = ConfigurationManager.AppSettings["StorageConnectionString"]; blobStorage = await BlobStorageHelper.BuildAsync(); CosmosDBHelper.AccessKey = ConfigurationManager.AppSettings["CosmosDBKey"]; CosmosDBHelper.EndpointUri = ConfigurationManager.AppSettings["CosmosDBEndpointURI"]; CosmosDBHelper.DatabaseName = ConfigurationManager.AppSettings["DatabaseName"]; CosmosDBHelper.CollectionName = ConfigurationManager.AppSettings["CollectionName"]; cosmosDb = await CosmosDBHelper.BuildAsync(); } var existing = await cosmosDb.FindDocumentByIdAsync <ImageMetadata>(fileName); var resized = ImageResizer.ResizeIfRequired(Server.MapPath(data.ImageLocalPath), 750); imageCB = async() => System.IO.File.OpenRead(resized.Item2); var imageBlob = await blobStorage.UploadImageAsync(imageCB, fileName); var metadata = new ImageMetadata(data.ImageLocalPath); metadata.AddInsights(data.Insights); metadata.BlobUri = imageBlob.Uri; metadata.TaggedBy = User.Identity.Name; if (existing == null) { metadata = (await cosmosDb.CreateDocumentIfNotExistsAsync(metadata, metadata.Id)).Item2; } else { metadata = await cosmosDb.UpdateDocumentAsync(metadata, metadata.Id); } return(metadata); } catch (Exception ex) { return(null); } }
public async void InitializeApplicationAsync() { _connectionStatusLED.Fill = _greyColorBrush; _cameraHelper = new CameraHelper(_captureElement); _blobStorageHepler = new BlobStorageHelper(); _iothubclient = new DeviceClientHelper(DeviceId, String.Format($"HostName=home-automation-iothub.azure-devices.net;DeviceId={DeviceId};SharedAccessKey=ekgleg2ZT7hICYSY5wJCCZzOgp3RhdnIPt0W35Kojw0=")); _iothubclient._client.SetConnectionStatusChangesHandler(OnConnectionStatusChagned); await _iothubclient._client.SetMethodHandlerAsync("SetDeviceElementSwitchStatus", OnSetDeviceElementSwitchStatus, null); await _iothubclient._client.SetMethodHandlerAsync("SetDeviceElementValue", OnSetDeviceElementValue, null); await _iothubclient._client.SetMethodHandlerAsync("CaptureImage", OnImageCapture, null); await _iothubclient._client.SetMethodHandlerAsync("OnTemerature", OnReadTemperature, null); await _iothubclient._client.SetMethodHandlerAsync("OnHumidity", OnReadHumidity, null); await _iothubclient._client.SetMethodHandlerAsync("OnWeatherForecast", OnReadWeatherForecast, null); await _iothubclient._client.SetMethodHandlerAsync("OnDeviceStatus", OnDeviceStatusAsync, null); await _iothubclient.OpenConnectionAsync(); await _cameraHelper.StartCameraPreviewAsync(); _timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 5) }; _timer.Tick += OnTimerTicked; _timer.Start(); _cooler = new VirtualDeviceElement(300, ElementType.Cooler); _heater = new VirtualDeviceElement(1800, ElementType.Heater); _light = new VirtualDeviceElement(100, ElementType.Light); _virtualElements = new List <VirtualDeviceElement>() { _cooler, _heater, _light, }; }
private string UploadCvToStorage(JobApplicationCreateView model) { var uploads = Path.Combine(env.WebRootPath, "uploads"); bool exists = Directory.Exists(uploads); if (!exists) { Directory.CreateDirectory(uploads); } var fileName = Path.GetFileName(model.CvFile.FileName); var fileStream = new FileStream(Path.Combine(uploads, model.CvFile.FileName), FileMode.Create); string mimeType = model.CvFile.ContentType; byte[] fileData = new byte[model.CvFile.Length]; BlobStorageHelper objBlobService = new BlobStorageHelper(); return(objBlobService.UploadFileToBlob(model.CvFile.FileName, fileData, mimeType)); }