0
votes

Here is my code:

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using Microsoft.WindowsAzure;
using System.Net.Http;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //get the storage account from the connection string
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=[account name];AccountKey=[account key];EndpointSuffix=core.windows.net");

            //instantiate the client
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            //set the container
            CloudBlobContainer container = blobClient.GetContainerReference("images");

            //get the blob reference
            CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob.jpg");

            //get image from stream and upload
            using (var client = new HttpClient())
            {
                using (var stream = client.GetStreamAsync(some_url).GetAwaiter().GetResult())
                {
                    if (stream != null)
                    {  
                        blockBlob.UploadFromStreamAsync(stream);
                    }
                }
                client.Dispose();
            }
        }
    }
}

The storage account instantiation works fine. The container referencing works fine (it actually exists). The block blob referencing works, as well, with no errors. The stream has the image I am getting from the URL referenced. Finally, the upload returns no errors.

Except, there is no image when I navigate to the Blob URI.

I get the following error:

The specified blob does not exist. RequestId:7df0aadc-0001-007c-6b90-f95158000000 Time:2017-07-10T15:21:25.2984015Z

I have also uploaded an image via the Azure Portal and that exists and can be navigated to through a browser.

Am I missing something?

1
Please try by setting the stream's position to 0 just before uploading.Gaurav Mantri
Is the container public or private?David Makogon
You're calling the async methods with GetAwaiter().GetResult(), which is very bad practice.VMAtm

1 Answers

1
votes

Update below line in your code as you're calling async method.

blockBlob.UploadFromStreamAsync(stream).GetAwaiter().GetResult();

This should resolve your problem.