public static async Task Run(
            [EventGridTrigger] EventGridEvent eventGridEvent,
            [Blob("{data.url}", FileAccess.Read)] Stream input,
            ILogger log)
        {
            try
            {
                if (input != null)
                {
                    var createdEvent = ((JObject)eventGridEvent.Data).ToObject <StorageBlobCreatedEventData>();
                    var extension    = Path.GetExtension(createdEvent.Url);
                    var encoder      = SupportFuncs.GetEncoder(extension);

                    if (encoder != null)
                    {
                        Task <string> storeImgInfoTask = SupportFuncs.StoreImgInfo(createdEvent.Url, input);
                        await         storeImgInfoTask;
                        if (storeImgInfoTask.Result == "")
                        {
                            log.LogInformation($"Something went wrong trying to create ImgInfo.");
                        }
                    }
                    else
                    {
                        log.LogInformation($"No encoder support for: {createdEvent.Url}");
                    }
                }
            }
            catch (Exception ex)
            {
                log.LogInformation(ex.Message);
                throw;
            }
        }
Example #2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string imgname = req.Query["imgname"];

            string output = "";

            string connectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
            CloudBlobClient     blobClient          = cloudStorageAccount.CreateCloudBlobClient();
            string             containerName        = Environment.GetEnvironmentVariable("IMGINFO_CONTAINER_NAME");
            CloudBlobContainer container            = blobClient.GetContainerReference(containerName);
            CloudBlockBlob     blob = container.GetBlockBlobReference(imgname);

            Task <bool> existsTask = blob.ExistsAsync();

            existsTask.Wait();
            if (existsTask.Result)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    await blob.DownloadToStreamAsync(stream);

                    stream.Position = 0;
                    StreamReader reader = new StreamReader(stream);
                    output = reader.ReadToEnd();
                }
            }
            else
            {
                //Generate info for image, if possible.
                string             imgContainerName = Environment.GetEnvironmentVariable("IMAGES_CONTAINER_NAME");
                CloudBlobContainer imgContainer     = blobClient.GetContainerReference(imgContainerName);
                CloudBlockBlob     imgBlob          = imgContainer.GetBlockBlobReference(imgname);
                if (await imgBlob.ExistsAsync())
                {
                    var input = new MemoryStream();
                    await imgBlob.DownloadToStreamAsync(input);

                    input.Position = 0;
                    string res = await SupportFuncs.StoreImgInfo(imgBlob.Uri.ToString(), input);

                    if (res == "")
                    {
                        output = "no info for image (tried to create it but failed)";
                    }
                    else
                    {
                        output = res;
                    }
                }
                else
                {
                    output = "no info for image";
                }
            }

            return
                (output != null
                ? (ActionResult) new OkObjectResult(output)
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body")
                );
        }