Azure Blob存储对象引用未设置为对象的实例

时间:2016-10-03 09:28:21

标签: c# asp.net azure asp.net-mvc-5 azure-storage-blobs

  public class StorageService
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=leepiostorage;AccountKey=removed for this post");

        public async Task Upload(string id, Stream data)
        {
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            // Retrieve a reference to a container.
            CloudBlobContainer container = blobClient.GetContainerReference("images");

            await container.CreateIfNotExistsAsync();

            container.SetPermissions(
                new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                }); 


            CloudBlockBlob blockBlob = container.GetBlockBlobReference(id);

            await blockBlob.UploadFromStreamAsync(data, data.Length);
        }

        public async Task UploadBlogPhoto(string id, Stream data)
        {
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            // Retrieve a reference to a container.
            CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

            await container.CreateIfNotExistsAsync();

            container.SetPermissions(
                new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });


            CloudBlockBlob blockBlob = container.GetBlockBlobReference(id);

            await blockBlob.UploadFromStreamAsync(data, data.Length);
        }
    }

这是我的StorageServices类,我正在尝试使用第二种方法“UploadBlogPhoto”。第一个有效。

以下是博客控制器的方法:

[HttpPost]
        public async Task<ActionResult> UploadPhoto(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                var fileExt = Path.GetExtension(file.FileName);
                if (fileExt.ToLower().EndsWith(".png") || fileExt.ToLower().EndsWith(".jpg") ||
                    fileExt.ToLower().EndsWith(".gif"))
                {
                    var str = "example";
                    await service.UploadBlogPhoto(str, file.InputStream);
                }
            }
            return RedirectToAction("Index");
        }

脚本视图:

 @using (Html.BeginForm("UploadPhoto", "Blog", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                <div class="browseimg">
                    <input type="file" class="display-none" name="file" id="files" onchange="this.form.submit()" />
                </div>
            }
            <button class="btn btn-primary width-100p main-bg round-border-bot" id="falseFiles">
                Upload billede
            </button>

 $(document)
       .ready(function () {
           $('#falseFiles')
               .click(function () {
                   $("#files").click();
               });
       });

所以第一个方法上传到一个名为“images”的容器就好了。当我尝试简单地添加另一个上传到第二个容器的方法时,我得到了

Object reference not set to an instance of an object.

await service.UploadBlogPhoto(str, file.InputStream);

我已经三重检查过所有想法?提前致谢

1 个答案:

答案 0 :(得分:0)

问题是BlogController中没有初始化服务。

只需添加

public BlogController()
        {
            service = new StorageService();
        }

解决了我的问题