public static async Task <Boolean> UploadImage(Stream imageStream, ImageQueueMessage queueMessage) { CloudStorageAccount storageAccount = null; CloudBlobContainer cloudBlobContainer = null; string storageConnectionString = Environment.GetEnvironmentVariable("StorageConnectionString"); // Check whether the connection string can be parsed. if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount)) { try { // Create the CloudBlobClient that represents the Blob storage endpoint for the storage account. CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); // Create a container called 'quickstartblobs' and append a GUID value to it to make the name unique. cloudBlobContainer = cloudBlobClient.GetContainerReference("bierrapporten"); bool created = await cloudBlobContainer.CreateIfNotExistsAsync(); if (created) { // Set the permissions so the blobs are public. BlobContainerPermissions permissions = new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }; await cloudBlobContainer.SetPermissionsAsync(permissions); } CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(queueMessage.imageName); await cloudBlockBlob.UploadFromStreamAsync(imageStream); return(true); } catch (StorageException ex) { Console.WriteLine("Error returned from the service: {0}", ex.Message); return(false); } } else { Console.WriteLine( "A connection string has not been defined in the system environment variables. " + "Add a environment variable named 'storageconnectionstring' with your storage " + "connection string as a value."); return(false); } }
public static async void Run([QueueTrigger("bierrapportmessages", Connection = "StorageConnectionString")] string queueItem, TraceWriter log) { ImageQueueMessage queueMessage = JsonConvert.DeserializeObject <ImageQueueMessage>(queueItem); Coordinates coordinates = RequestHelper.getCoordinates(queueMessage); if (coordinates == null) { return; } double temperature = await RequestHelper.TemperatureRequest(coordinates); Stream imageStream = await RequestHelper.ImageRequest(coordinates); Stream editedImageStream = ImageHelper.TextToImage(imageStream, temperature); Boolean status = await BlobHelper.UploadImage(editedImageStream, queueMessage); log.Info($"C# Queue trigger function processed: {queueItem}"); }
public static Coordinates getCoordinates(ImageQueueMessage message) { string address = message.street + " " + message.houseNumber + ", " + message.residence; string encodedAddress = HttpUtility.UrlEncode(address); string URL = "https://maps.googleapis.com/maps/api/geocode/json"; string addressParameter = "?address=" + encodedAddress; string geoCodeApiKey = Environment.GetEnvironmentVariable("GeoCodeApiKey"); string keyParameter = "&key=" + geoCodeApiKey; string completeURL = URL + addressParameter + keyParameter; HttpClient client = new HttpClient(); client.BaseAddress = new Uri(URL); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync(addressParameter + keyParameter).Result; if (response.IsSuccessStatusCode) { string res = null; using (HttpContent content = response.Content) { // ... Read the string. Task <string> result = content.ReadAsStringAsync(); res = @result.Result; } dynamic o = JsonConvert.DeserializeObject(res); string lng = o.results[0].geometry.location.lng; string lat = o.results[0].geometry.location.lat; return(new Coordinates(lng, lat)); } else { return(null); } }
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req, TraceWriter log) { BierRapportPostModel model = await req.Content.ReadAsAsync<BierRapportPostModel>(); List<String> missingProperties = model.missingProperties(); if (missingProperties.Count > 0) { MissingPropertiesModel missingPropertiesModel = new MissingPropertiesModel(missingProperties); string missingPropertiesJson = JsonConvert.SerializeObject(missingPropertiesModel); return new HttpResponseMessage() { Content = new StringContent(missingPropertiesJson, System.Text.Encoding.UTF8, "application/json") }; } string bierRapportName = "bierrapport" + Guid.NewGuid().ToString() + ".png"; ImageQueueMessage queueMessage = new ImageQueueMessage(model.street, model.houseNumber, model.residence, bierRapportName); string queueMessageJson = JsonConvert.SerializeObject(queueMessage); QueueHelper.PlaceQueueMessage(queueMessageJson); BierRapportNameModel bierRapportNameModel = new BierRapportNameModel(bierRapportName); string bierRapportNameJson = JsonConvert.SerializeObject(bierRapportNameModel); return new HttpResponseMessage() { Content = new StringContent(bierRapportNameJson, System.Text.Encoding.UTF8, "application/json") }; }