设为首页 收藏本站
查看: 847|回复: 0

[经验分享] Azure Storage用法:使用Blob Storage

[复制链接]

尚未签到

发表于 2017-6-30 08:38:24 | 显示全部楼层 |阅读模式
  Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob、Queue、File 和 Table。
  笔者在C# 消息队列-Microsoft Azure service bus 服务总线中介绍了 Queue Storage 的基本用法,本文将介绍 Blob Storage 的主要使用方法。
  Blob Storage可以看做是云端的文件系统。与桌面操作系统上不同,我们是通过REST API来进行对文件的操作。有关REST API的详细信息,请参见Blob 服务 API。
  本文以邮件中的附件示例:



using DBI.SaaS.MessageService.FileStore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DBI.SaaS.MessageService.Controller
{
public class FileUploadController
{

public string Upload(Stream fileData, string extension)
{
//保存图片
var store = new AzureStore()
{
FileData = fileData,
StoreType = typeof(AzureFileStoreProvider),
ExtensionName = extension
};
//var data = (fileData as MemoryStream).ToArray();
//var shortCut = data.MakeThumbnail(214, 166, "M");
var storeProvider = StoreFactory.Create(store);
storeProvider.SaveFile();
return store.OutFileName;
}
public string Upload(Stream fileData, string extension, byte[] arr)
{
//保存图片
var store = new AzureStore()
{
FileData = fileData,
FileDataByteArray = arr,
StoreType = typeof(AzureFileStoreProvider),
ExtensionName = extension
};
//var data = (fileData as MemoryStream).ToArray();
//var shortCut = data.MakeThumbnail(214, 166, "M");
var storeProvider = StoreFactory.Create(store);
storeProvider.SaveFile();
return store.OutFileName;
}

/// <summary>
/// 下载文件
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
public Stream Download(string filepath, string type)
{
var store = new AzureStore()
{
FileData = new MemoryStream(),
StoreType = typeof(AzureFileStoreProvider),
OutFileName = filepath
};
var storeProvider = StoreFactory.Create(store);
storeProvider.GetFile(type);
return store.FileData;
}
}
}


using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DBI.SaaS.MessageService.FileStore
{
public class AzureFileStoreProvider : IFileStoreProvider
{
static StorageCredentials credentials = new StorageCredentials(ConfigurationManager.AppSettings["StorageAccount"], ConfigurationManager.AppSettings["StorageKey"]);
static CloudStorageAccount storageAccount = new CloudStorageAccount(credentials,
new Uri(ConfigurationManager.AppSettings["BlobUri"]),
null,
null, null);
/// <summary>
/// 文件存储信息
/// </summary>
public IStore Store
{
get; set;
}
/// <summary>
/// 获取文件
/// </summary>
public void GetFile(string type)
{
string fileinfo = Store.OutFileName;
string[] pars = fileinfo.Split('-');
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
if (type == "s")
{
CloudBlobContainer container = blobClient.GetContainerReference(pars[0] + "sc");
var blockBlob = container.GetBlobReference(pars[1]);
blockBlob.DownloadToStream(Store.FileData);
}
else
{
CloudBlobContainer container = blobClient.GetContainerReference(pars[0]);
var blockBlob = container.GetBlobReference(pars[1]);
blockBlob.DownloadToStream(Store.FileData);
}
}
/// <summary>
/// 保存文件
/// </summary>
public void SaveFile()
{
// Retrieve storage account from connection string.
//CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
//    CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
try
{
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
var containerName = "files" + DateTime.Now.ToString("yyyyMM");
var filename = Guid.NewGuid().ToString("N") + this.Store.ExtensionName;
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExists();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
//blockBlob.UploadFromStream(this.Store.FileData);
blockBlob.UploadFromByteArray(this.Store.FileDataByteArray, 0, this.Store.FileDataByteArray.Length);
this.Store.OutFileName = containerName + "-" + filename;
}
catch (Exception e)
{
throw e;
}
finally
{
this.Store.FileData.Close();
this.Store.FileData.Dispose();
}
}
public void SaveFile(string containername, string filename)
{
try
{
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(containername);
container.CreateIfNotExists();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
blockBlob.UploadFromByteArray(this.Store.BytData, 0, this.Store.BytData.Length);
}
catch (Exception e)
{
throw e;
}
}
public void SaveFileNoImg()
{
// Retrieve storage account from connection string.
//CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
//    CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
var containerName = "files" + DateTime.Now.ToString("yyyyMM");
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExists();
var filename = Guid.NewGuid().ToString("N") + this.Store.ExtensionName;
CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
// Retrieve reference to a blob named "myblob".

// Create or overwrite the "myblob" blob with contents from a local file.

blockBlob.UploadFromStream(this.Store.FileData);
this.Store.FileData.Dispose();
this.Store.OutFileName = containerName + "-" + filename;
}
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-389587-1-1.html 上篇帖子: Azure service bus Topic基本用法 下篇帖子: Azure Event Hub 技术研究系列2-发送事件到Event Hub
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表