public static async Task <String> GetImageUrl(FetchRapportModel model)
        {
            CloudStorageAccount storageAccount     = null;
            CloudBlobContainer  cloudBlobContainer = null;
            // string storageConnectionString = Environment.GetEnvironmentVariable("DefaultEndpointsProtocol=https;AccountName=eindopdracht;AccountKey=6mIYTFe2w7/roeqmOqdMo8jIFsyvnDZ3vZanDH1viL3uokZz7WBHazA2WnlKH8immWlc24P7MnhT9KvHdfrRNA==;EndpointSuffix=core.windows.net");
            //string storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=eindopdracht;AccountKey=6mIYTFe2w7/roeqmOqdMo8jIFsyvnDZ3vZanDH1viL3uokZz7WBHazA2WnlKH8immWlc24P7MnhT9KvHdfrRNA==;EndpointSuffix=core.windows.net";
            string connectionString = Environment.GetEnvironmentVariable("StorageConnectionString");

            // Check whether the connection string can be parsed.
            if (CloudStorageAccount.TryParse(connectionString, 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(model.imageName);
                    Boolean        exists         = await cloudBlockBlob.ExistsAsync();

                    if (exists)
                    {
                        return(cloudBlockBlob.Uri.AbsoluteUri);
                    }
                    else
                    {
                        return(null);
                    }
                }
                catch (StorageException ex)
                {
                    Console.WriteLine("Error returned from the service: {0}", ex.Message);
                    return(null);
                }
            }
            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(null);
            }
        }
Ejemplo n.º 2
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            FetchRapportModel model = await req.Content.ReadAsAsync <FetchRapportModel>();

            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 imageUrl = await BlobHelper.GetImageUrl(model);

            if (imageUrl == null)
            {
                ErrorModel error = new ErrorModel("Your image is not available at this moment");
                string     missingPropertiesJson = JsonConvert.SerializeObject(error);
                return(new HttpResponseMessage()
                {
                    Content = new StringContent(missingPropertiesJson, System.Text.Encoding.UTF8, "application/json")
                });
            }
            else
            {
                RapportModel rapport = new RapportModel(imageUrl);
                string       missingPropertiesJson = JsonConvert.SerializeObject(rapport);
                return(new HttpResponseMessage()
                {
                    Content = new StringContent(missingPropertiesJson, System.Text.Encoding.UTF8, "application/json")
                });
            }
        }