Azure Storage用法:使用Blob Storage
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的更多相关文章
- Windows Azure入门教学:使用Blob Storage
对于.net开发人员,这是一个新的领域,但是并不困难.本文将会介绍如何使用Blob Storage.Blob Storage可以看做是云端的文件系统.与桌面操作系统上不同,我们是通过REST API来 ...
- [转]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 ...
- Azure Blob Storage从入门到精通
今天推荐的是一个系列文章,让读者阅读完成后可以对Azure Blob Storage的开发有一个全面的了解,可谓是从入门到精通. Azure在最初的版本里面就提供了非结构化数据的存储服务,也即Blob ...
- Azure Functions(二)集成 Azure Blob Storage 存储文件
一,引言 上一篇文章有介绍到什么是 SeverLess ,ServerLess 都有哪些特点,以及多云环境下 ServerLess 都有哪些解决方案.在这众多解决方案中就包括 Function App ...
- Azure Blob Storage 基本用法 -- Azure Storage 之 Blob
Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure Table storage ...
- Python 操作 Azure Blob Storage
笔者在<Azure 基础:Blob Storage>一文中介绍了 Azure Blob Storage 的基本概念,并通过 C# 代码展示了如何进行基本的操作.最近笔者需要在 Linux ...
- Azure 基础:Blob Storage
Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在前文中介绍了 Table Storage 的基本 ...
- presto访问 Azure blob storage
当集群使用Azure Blog Storage时,prestoDB无法获取返回结果,在此记录下 如下,hive里面的两个表,一个使用的是本地的hdfs,一个是使用 azure blob storage ...
- DW(六):polybase访问Azure Blob Storage
目录: 连接hadoop配置语法 配置hadoop连接 Pushdown配置 Create external tables for Azure blob storage 连接hadoop配置语法: g ...
随机推荐
- 如何安装并且使用jmeter进行简单的性能测试
Jmeter 介绍 Jmeter 是一款使用Java开发的,开源免费的,测试工具, 主要用来做功能测试和性能测试(压力测试/负载测试). 而且用Jmeter 来测试 Restful API, 非常 ...
- 中间人攻击——ARP欺骗的原理、实战及防御
1.1 什么是网关 首先来简单解释一下什么是网关,网关工作在OSI七层模型中的传输层或者应用层,用于高层协议的不同网络之间的连接,简单地说,网关就好比是一个房间通向另一个房间的一扇门. 1.2 A ...
- [Swift]LeetCode692. 前K个高频单词 | Top K Frequent Words
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...
- [Swift]LeetCode697. 数组的度 | Degree of an Array
Given a non-empty array of non-negative integers nums, the degreeof this array is defined as the max ...
- 优化之Joiner组件
Joiner组件在运行时需要额外的内存空间处理中间结果,因此会影响性能 可通过查看Joiner performance计数器来决定Joiner组件是否需要优化 通过如下方式优化Joiner组件 将Ma ...
- SpringBoot配置 druid 数据源配置 慢SQL记录
spring: datasource: url: jdbc:mysql://127.0.0.12:3306/test?autoReconnect=true&useUnicode=true&am ...
- 深入解析 H.265 编码模式,带你了解Apple全面推进H.265的原因
今天我们聊聊视频编码.视频文件亘古以来存在一个矛盾:高清画质和视频体积的冲突,相同编码标准下,视频更高清,视频体积更大.因此,应用更先进的视频编码标准,降低视频体积,可以大幅降低网站的流量消耗. 目前 ...
- Nancy in .Net Core学习笔记 - 视图引擎
前文中我们介绍了Nancy中的路由,这一篇我们来介绍一下Nancy中的视图引擎. Nancy中如何返回一个视图(View) 在ASP.NET Mvc中,我们使用ViewResult类来返回一个视图.N ...
- 使用Spring Cloud搭建高可用服务注册中心
我们需要的,不仅仅是一个服务注册中心而已,而是一个高可用服务注册中心. 上篇博客[使用Spring Cloud搭建服务注册中心]中我们介绍了如何使用Spring Cloud搭建一个服务注册中心,但是搭 ...
- 行为驱动:Cucumber + Selenium + Java(二) - 第一个测试
在上一篇中,我们搭建好了Selenium + Cucumber + Java的自动化测试环境,这一篇我们就赶紧开始编写我们的第一个BDD测试用例. 2.1 创建features 我们在新建的java项 ...