Azure Blob Storage Uploads Fine, but Blob Doesn't Exist

时间:2017-07-10 15:23:42

标签: azure .net-core azure-storage azure-storage-blobs

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 个答案:

答案 0 :(得分:1)

在您的代码中更新以下行,因为您正在调用异步方法。

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

这可以解决您的问题。