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. cadence PCB板级设计

    总结PCB板框设计,定位孔的放置,以及布线区域和元件放置区域的放置,最重要的是层叠结构的设计.

  2. Java作业八(2017-10-30)

    public class TAutoPerson { public static void main(String args[]) { new Person(); new Person(); new ...

  3. CS20SI-tensorflow for research笔记: Lecture2

    本文整理自知乎专栏深度炼丹,转载请征求原作者同意. 本文的全部代码都在原作者GitHub仓库github CS20SI是Stanford大学开设的基于Tensorflow的深度学习研究课程. Tens ...

  4. [Swift]LeetCode779. 第K个语法符号 | K-th Symbol in Grammar

    On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace ...

  5. ubuntu16.04安装lnmp环境

     1.安装mysql sudo apt install mysql-server 2.安装nginx和php #添加nginx和php的ppa源 sudo apt-add-repository ppa ...

  6. spark能传递外部命名参数给main函数吗?

    查了资料好像都没有办法.只能通过: def main(args: Array[String]): Unit = { // 读取参数 var city = args(0) var input = arg ...

  7. java多线程(6)---ThreadLocal

    ThreadLocal 什么是ThreadLocal? 顾名思义它是local variable(线程局部变量).它的功用非常简单,就是为每一个使用该变量的线程都提供一个变量值的副本,是每一个线程都可 ...

  8. C++中int与string的转化

    C++中int与string的转化 int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释.缺省情况下,是当成10进制(dec)来解释,如果想用8进制,16进制,怎么办?加上前缀, ...

  9. linux内核中听过就能记住的概念

    打算给我们部门弄个内部分享.发现大家对一些底层知识的认知停留在一句一句的,比如听说JVM使用-XX:-UseBiasedLocking取消偏向锁可以提高性能,因为它只适用于非多线程高并发应用.使用数字 ...

  10. leetcode — palindrome-partitioning

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...