本文介绍了使用Azure的斑点与Azure的网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在作出的 MVC的Windows Azure 网站,涉及用户上传图片的过程。
我想图像存储在斑点。
我搜索了教程,但大多处理的的webapps 的,而不是MVC的网站。

I'm in the process of making an MVC Windows Azure website which involves users uploading images.I wanted to store the images in blobs.I searched for tutorials but most of them deal with Webapps rather than MVC websites.

唯一有用的教程,我发现是:的

The only useful tutorial I found was: http://www.codeproject.com/Articles/490178/How-to-Use-Azure-Blob-Storage-with-Azure-Web-Sites

我是新来的整个MVC / Windows Azure中/ Visual Studio中的场景和我在第7步,我必须连接到云存储账户相混淆。

I'm new to the whole MVC/Windows Azure/Visual Studio scene and I got confused at Step 7 where I had to connect to Cloud Storage Account.

var storageAccount = CloudStorageAccount.Parse(
    ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString);

我想不通的地方,我应该把一个code的那一点。

I can't figure out where I'm supposed to put that bit of a code.

同样在code步骤8:创建一个容器

Same for the code in Step 8: Creating a container

blobStorage = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobStorage.GetContainerReference("productimages");
if (container.CreateIfNotExist())
{
    // configure container for public access
    var permissions = container.GetPermissions();
    permissions.PublicAccess = BlobContainerPublicAccessType.Container;
    container.SetPermissions(permissions);
}

和第9步:将图像保存到一个BLOB

And Step 9: Save the Image to a BLOB

string uniqueBlobName = string.Format("productimages/image_{0}{1}",
   Guid.NewGuid().ToString(), Path.GetExtension(image.FileName));
CloudBlockBlob blob = blobStorage.GetBlockBlobReference(uniqueBlobName);
blob.Properties.ContentType = image.ContentType;
blob.UploadFromStream(image.InputStream);

任何帮助将是非常美联社preciated。任何其他链接到相关的教程也欢迎。

Any help will be really appreciated. Any other links to relevant tutorials are also welcome.

非常感谢!

推荐答案

我假设你已经添加了您的Web应用程序项目?这似乎并没有在你引用的参考明确的,但如果你没有你会看到一堆未解决的编译器错误的。

I assume you've added the Windows Azure Storage NuGet package to your Web App project? That didn't seem explicit in the reference you quoted, but if you hadn't you'd be seeing a bunch of unresolved compiler errors.

在的code去的地方而言,这是相当给你。分配到 storageAccount 就像是设置数据库连接字符串;没有什么是跨线去,所以你可以设置一次也许并使它可在整个应用程序的一些静态类实例的一部分。

In terms of where the code goes, it's rather up to you. The assignment to storageAccount is like setting a database connection string; nothing is going across the wire, so you could set it once perhaps and make it available throughout your application as part of some static class instance.

在创建容器方面,你只要你想存储数据的做到这一点。如果您的应用程序始终在同一个硬codeD存储信息的容器,你可以提前在门户网站设置你的容器并在code不能创造。

In terms of creating a container, you'd do this whenever you want to store data - consider it tantamount to creating a folder in a file system or maybe a table in a relational database. If your application is always storing information in the same "hard-coded" container, you could set your container up ahead of time in the portal and not create in code.

把code这样的,虽然提供万一有点弹性的容器得到了通过一些外部的过程中被删除;同样的方式,使用SQL Server应用程序的可能的检查一个Employee表的存在做更新/检索之前,因为它可能(但不太可能),有人通过其他SQL工具删除Employee表。

Putting code like this in though provides a bit of resiliency in case the container got deleted through some external process; the same way that an app using SQL Server could check for the existence of an Employee table before doing updates/retrieves, since it's possible (but not likely) someone deleted the Employee table via some other SQL tool.

图像保存到BLOB将直接把后面的任何控制器负责发起这一行动。这是你做的真实的工作。你的样品中的code专门引用了一个容器 productimages ,因此它可以立即$由code在第8步pceded,如果你想为p $某些有没有机会在容器被删除(通过门户网站,例如)。再次,这将是类似的检查在传统的客户机/服务器code,每次一个数据库表的存在,你在里面(我们大多数人可能会矫枉过正考虑访问的数据,我们会采取异常处理,以支付意外)。

Saving the image to the blob would be put directly behind whatever controller is responsible for initiating that action. This is where you're doing the 'real' work. The code in your sample specifically references a container productimages, so it could be immediately preceded by the code in Step 8 if you wanted to be certain that there no chance the container was deleted (through the portal, for instance). Again, that would be similar to checking for the existence of a database table in traditional client/server code everytime you accessed data in it (which most of us might consider overkill and we'd resort to exception handling to cover that contingency).

这篇关于使用Azure的斑点与Azure的网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 09:49