Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.77 KB | None | 0 0
  1.  
  2. using Microsoft.Azure.WebJobs;
  3. using Microsoft.Azure.WebJobs.Extensions.Http;
  4. using Microsoft.Azure.WebJobs.Host;
  5. using Microsoft.WindowsAzure.Storage;
  6. using Microsoft.WindowsAzure.Storage.Blob;
  7. using System;
  8. using System.IO;
  9. using System.Net;
  10. using System.Net.Http;
  11. using System.Net.Http.Headers;
  12. using System.Threading.Tasks;
  13.  
  14. namespace ServerlessImageManagement
  15. {
  16.     public static class GetImage
  17.     {
  18.         [FunctionName("GetImage")]
  19.         public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]HttpRequestMessage req,
  20.             TraceWriter log)
  21.         {
  22.             Stream photoStream;
  23.             try
  24.             {
  25.                 string imagePath = req.GetQueryStrings()["path"];
  26.                 var photoBlob = await Utils.BlobClient.GetBlobReferenceFromServerAsync(new Uri(imagePath, UriKind.Absolute));
  27.                 photoStream = await photoBlob.OpenReadAsync(AccessCondition.GenerateEmptyCondition(),
  28.                     new BlobRequestOptions(), new OperationContext());
  29.             }
  30.             catch (Exception e)
  31.             {
  32.                 Console.WriteLine(e.Message);
  33.                 return new HttpResponseMessage(HttpStatusCode.NotFound);
  34.             }
  35.  
  36.             var result = new HttpResponseMessage(HttpStatusCode.OK);
  37.             result.Content = new StreamContent(photoStream);
  38.             result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
  39.             return result;
  40.         }
  41.     }
  42. }
  43.  
  44.  
  45.  
  46. using Microsoft.Azure.WebJobs;
  47. using Microsoft.Azure.WebJobs.Extensions.Http;
  48. using Microsoft.Azure.WebJobs.Host;
  49. using Microsoft.WindowsAzure.Storage;
  50. using Microsoft.WindowsAzure.Storage.Blob;
  51. using System;
  52. using System.IO;
  53. using System.Net;
  54. using System.Net.Http;
  55. using System.Net.Http.Headers;
  56. using System.Threading.Tasks;
  57.  
  58. namespace ServerlessImageManagement
  59. {
  60.     public static class GetImageThumbnail
  61.     {
  62.         [FunctionName("GetImageThumbnail")]
  63.         public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]HttpRequestMessage req,
  64.             [Queue("thumbnails", Connection = "ImageStorageAccount")] ICollector<string> thumbnailsQueue, TraceWriter log)
  65.         {
  66.             Stream photoStream;
  67.             var imagePath = req.GetQueryStrings()["path"];
  68.             string thumbnailPath = Utils.GetThumbnailPath(imagePath);
  69.             try
  70.             {
  71.                 var photoBlob = await Utils.BlobClient.GetBlobReferenceFromServerAsync(new Uri(thumbnailPath, UriKind.Absolute));
  72.                 photoStream = await photoBlob.OpenReadAsync(AccessCondition.GenerateEmptyCondition(),
  73.                     new BlobRequestOptions(), new OperationContext());
  74.             }
  75.             catch (Exception e)
  76.             {
  77.                 Console.WriteLine(e.Message);
  78.                 thumbnailsQueue.Add(thumbnailPath);
  79.                 try
  80.                 {
  81.                     var photoBlob = await Utils.BlobClient.GetBlobReferenceFromServerAsync(new Uri(imagePath, UriKind.Absolute));
  82.                     photoStream = await photoBlob.OpenReadAsync(AccessCondition.GenerateEmptyCondition(),
  83.                         new BlobRequestOptions(), new OperationContext());
  84.                 }
  85.                 catch (Exception exception)
  86.                 {
  87.                     Console.WriteLine(exception);
  88.                     return new HttpResponseMessage(HttpStatusCode.NotFound);
  89.                 }
  90.             }
  91.             var result = new HttpResponseMessage(HttpStatusCode.OK);
  92.             result.Content = new StreamContent(photoStream);
  93.             result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
  94.             return result;
  95.         }
  96.  
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement