简体   繁体   English

使用Azure保存上传的图像

[英]Saving uploaded images with azure

Im looking at migrating a website to windows azure from a standard web server (first time using cloud). 我正在考虑将网站从标准Web服务器迁移到Windows Azure(第一次使用云)。 Currently theres a few places on my site that saves images to the local server path using HttpContext.Current.Server.MapPath. 当前,在我的网站上有一些地方可以使用HttpContext.Current.Server.MapPath将图像保存到本地服务器路径。 Reading into cloud hosting any assets not included in the publish will be lost in future publishes. 在将来的发布中,读入托管不包含在发布中的任何资产的云将丢失。 How do I save these kinds of assets so that they are never lost on future publishes? 我该如何保存这些资产,以使它们在以后的发行中永远不会丢失?

You need to store them not on the server. 您不需要将它们存储在服务器上。

Azure has a few storage options . Azure有一些存储选项 I would suggest that you store the images in blob storage in azure. 我建议您将图像以天蓝色存储在Blob存储中。

There is a tutorial in the examples of how to use blob storage to store files 示例中有一个教程, 介绍如何使用Blob存储来存储文件

Using blob storage is pretty straight forward, but it needs a little bit of setup first. 使用Blob存储非常简单,但是首先需要进行一些设置。 The link above shows you all of the steps for creating a storage account, then a container to store the blobs in. Once you have a container for your blobs it works pretty much like a file system. 上面的链接向您展示了创建存储帐户的所有步骤,然后介绍了用于存储Blob的容器。一旦有了Blob的容器,它的工作原理几乎类似于文件系统。

From the linked tutorial , here is how to upload a file to blob storage: 链接的教程中 ,这是如何将文件上传到Blob存储:

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

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

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
    blockBlob.UploadFromStream(fileStream);
} 

when you then want to download them again you need to do this: 当您想再次下载它们时,您需要执行以下操作:

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

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

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Retrieve reference to a blob named "photo1.jpg".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("photo1.jpg");

// Save blob contents to a file.
using (var fileStream = System.IO.File.OpenWrite(@"path\myfile"))
{
    blockBlob.DownloadToStream(fileStream);
} 

to get your connection string details you need to select the storage account in azure and then click the 'Manage access keys' button, whichpops up a window with your storage account name and access key 要获取您的连接字符串详细信息,您需要以天蓝色选择存储帐户,然后单击“管理访问密钥”按钮,这会弹出一个带有您的存储帐户名称和访问密钥的窗口

此外,您可以看一下透明的Azure文件服务,其行为类似于标准文件系统(可以使用标准IO读/写文件功能) http://blogs.msdn.com/b/windowsazurestorage/archive/2014/ 5月12日/导入-微软天青-文件service.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM