未将 Azure Blob 存储对象引用设置为对象的实例
Azure Blob Storage Object reference not set to an instance of an object
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 class,我在其中尝试使用第二种方法 "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);
我已经检查了所有内容,有什么想法吗?提前致谢
问题是服务没有在 BlogController 中初始化。
只需添加一个
public BlogController()
{
service = new StorageService();
}
解决了我的问题
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 class,我在其中尝试使用第二种方法 "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);
我已经检查了所有内容,有什么想法吗?提前致谢
问题是服务没有在 BlogController 中初始化。
只需添加一个
public BlogController()
{
service = new StorageService();
}
解决了我的问题