简体   繁体   中英

Saving uploaded images with azure

Im looking at migrating a website to windows azure from a standard web server (first time using cloud). Currently theres a few places on my site that saves images to the local server path using 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 . I would suggest that you store the images in blob storage in azure.

There is a tutorial in the examples of how to use blob storage to store files

Using blob storage is pretty straight forward, but it needs a little bit of setup first. 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.

From the linked tutorial , here is how to upload a file to blob storage:

// 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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