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[] + "sc");
var blockBlob = container.GetBlobReference(pars[]);
blockBlob.DownloadToStream(Store.FileData);
}
else
{
CloudBlobContainer container = blobClient.GetContainerReference(pars[]);
var blockBlob = container.GetBlobReference(pars[]);
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, , 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, , 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;
}
}
}

Azure Storage用法:使用Blob Storage的更多相关文章

  1. Windows Azure入门教学:使用Blob Storage

    对于.net开发人员,这是一个新的领域,但是并不困难.本文将会介绍如何使用Blob Storage.Blob Storage可以看做是云端的文件系统.与桌面操作系统上不同,我们是通过REST API来 ...

  2. [转]windows azure How to use Blob storage from .NET

    本文转自:http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/?rnd=1 ...

  3. Azure Blob Storage从入门到精通

    今天推荐的是一个系列文章,让读者阅读完成后可以对Azure Blob Storage的开发有一个全面的了解,可谓是从入门到精通. Azure在最初的版本里面就提供了非结构化数据的存储服务,也即Blob ...

  4. Azure Functions(二)集成 Azure Blob Storage 存储文件

    一,引言 上一篇文章有介绍到什么是 SeverLess ,ServerLess 都有哪些特点,以及多云环境下 ServerLess 都有哪些解决方案.在这众多解决方案中就包括 Function App ...

  5. Azure Blob Storage 基本用法 -- Azure Storage 之 Blob

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure Table storage ...

  6. Python 操作 Azure Blob Storage

    笔者在<Azure 基础:Blob Storage>一文中介绍了 Azure Blob Storage 的基本概念,并通过 C# 代码展示了如何进行基本的操作.最近笔者需要在 Linux ...

  7. Azure 基础:Blob Storage

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在前文中介绍了 Table Storage 的基本 ...

  8. presto访问 Azure blob storage

    当集群使用Azure Blog Storage时,prestoDB无法获取返回结果,在此记录下 如下,hive里面的两个表,一个使用的是本地的hdfs,一个是使用 azure blob storage ...

  9. DW(六):polybase访问Azure Blob Storage

    目录: 连接hadoop配置语法 配置hadoop连接 Pushdown配置 Create external tables for Azure blob storage 连接hadoop配置语法: g ...

随机推荐

  1. mysqldump 导出中文乱码

    命令:mysqldump -uroot -p test > /data/test.sql 导出后的数据库打开是乱码,如下: 开始以为打开的方式不对,就用记事本打开后,用utf-8的编码格式另保存 ...

  2. webpack 解决 semantic ui 中 google fonts 引用的问题

    semantic ui css 的第一行引用了 google web font api,由于不可告人而又众所周知的原因,这条链接在国内无法访问: @import url('https://fonts. ...

  3. SQL 查询当前时间

    Mysql: select date_format(now(),'%Y-%m-%d'); Oracle: Oracle中如何获取系统当前时间进行语句的筛选是SQL语句的常见功能 获取系统当前时间dat ...

  4. 【设计经验】5、Verilog对数据进行四舍五入(round)与饱和(saturation)截位

    一.软件平台与硬件平台 软件平台: 操作系统:Windows 8.1 64-bit 开发套件:Vivado2015.4.2  Matlab2016a 仿真工具:Vivado自带仿真器 二.引言 在利用 ...

  5. Android单元测试之一:基本概念

    Android单元测试之一:基本概念 简单介绍 单元测试是应用程序测试策略中的基本测试,通过对代码进行单元测试,一方面可以轻松地验证单个单元的逻辑是否正确,另一方面在每次构建之后运行单元测试,可以快读 ...

  6. linux 部署mysql

    参考:https://www.cnblogs.com/silentdoer/articles/7258232.html mysql中执行的语句需要在语句结尾使用分号 下载 MySql yum 包  w ...

  7. Python内置函数(22)——float

    英文文档: class float([x]) Return a floating point number constructed from a number or string x. If the ...

  8. 微信小程序实战--集阅读与电影于一体的小程序项目(一)

    1.首页欢迎界面 项目目录结构 新建项目ReaderMovie,然后新建文件,结构如下 welcome.wxml <view class='container'> <image cl ...

  9. 智能压缩,摆脱用 Gzip 还是 Brotli 的纠结

    近日,又拍云上线了“智能压缩”功能,同时支持 Gzip 和 Brotli 压缩算法,在节约流量的同时,进一步减少用户的等待时间. CDN 流量问题一直以来是大家关注的重点,又拍云针对流量节约上线了一系 ...

  10. Python链接Mssql之Python库pymssql

    连接数据库 pymssql连接数据库的方式和使用sqlite的方式基本相同: 使用connect创建连接对象 connect.cursor创建游标对象,SQL语句的执行基本都在游标上进行 cursor ...