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. 小程序wx:for循环列表数量的限制

    数据有100条,我们只要页面显示一部分,就要通过index来限制.index<3,就是显示序列0,1,2这三条数据.具体写法: <block wx:for='{{list}}' wx:ke ...

  2. SQL、索引

    (二)数据库索引 数据库索引是用于提高数据库表的数据访问速度的. 数据库索引的特点: a)避免进行数据库全表的扫描,大多数情况,只需要扫描较少的索引页和数据页,而不是查询所有数据页.而且对于非聚集索引 ...

  3. Spring Cloud 微服务架构全链路实践

    阅读目录: 1. 网关请求流程 2. Eureka 服务治理 3. Config 配置中心 4. Hystrix 监控 5. 服务调用链路 6. ELK 日志链路 7. 统一格式返回 Java 微服务 ...

  4. [IOT] 自制蓝牙工牌办公室定位系统 (二)—— 基于ESP32的蓝牙信号扫描系统

      前面章节: 自制蓝牙工牌办公室定位系统 (一)-- 阿里物联网平台概览及打通端到云(硬核·干货)   目录: 1.蓝牙广播简介 2.蓝牙扫描简介 3.基于蓝牙广播和蓝牙扫描常见应用 4.ESP32 ...

  5. [Zephyr] 1、在linux上安装Zephyr-OS并跑DEMO

    星期五, 14. 九月 2018 02:18上午 - BEAUTIFULZZZZ 0) 前言 Zephyr™项目是一个采用Apache 2.0协议许可,Linux基金会托管的协作项目.为所有资源受限设 ...

  6. vue+mescroll=VScrollFull

    VScrollFull 介绍 这个组件是什么? 是为了方便的使用下拉刷新,上拉加载而去封装的一个依赖于 mescroll.js 的 vue 组件(未发布,文末代码~) 封装这个组件使用了什么? mes ...

  7. emWin实现ATM机界面设计,含uCOS-III和FreeRTOS两个版本

    第1期:ATM机配套例子:V6-900_STemWin提高篇实验_ATM机(uCOS-III)V6-901_STemWin提高篇实验_ATM机(FreeRTOS) 例程下载地址:http://foru ...

  8. PHP workMan webSocket 转发器

    PHP WorkerMan webSocket 功能演示===================================== 基本功能:实现页面websocket之间互相通讯 start_deb ...

  9. [SQL]LeetCode178. 分数排名 | Rank Scores

    Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...

  10. [Swift]LeetCode433. 最小基因变化 | Minimum Genetic Mutation

    A gene string can be represented by an 8-character long string, with choices from "A", &qu ...