Azure Blob (三)参数设置说明
一,引言
上一篇将 Azure Blob 存储的时候,有使用到一个 .NET Core Web 项目,通过代码的方式进行操作 Azure Blob 的数据,接着上一篇的内容,今天继续看一下代码,具体看看 Azure.Storage 中的类,方法。
--------------------我是分割线--------------------
Azure Blob Storage 存储系列:
1,Azure Storage 系列(一)入门简介
2,Azure Storage 系列(二) .NET Core Web 项目中操作 Blob 存储
二,正文
1,配置 Blob 连接字符串
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"AzureBlobStorageConnectionString": "DefaultEndpointsProtocol=https;AccountName=cnbateblogaccount;AccountKey=FU01h022mn1JjONp+ta0DAXOO7ThK3diY891n9nycsTLGZF83nJpGVCVFhGOfV0wndOOQ==;EndpointSuffix=core.windows.net"
}
”AzureBlobStorageConnectionString“ 向此 Azure 存储帐户提出请求时,使用访问密钥对应用程序进行身份验证。请采用安全的方式(例如使用 Azure Key Vault)保存访问密钥,不要共享密钥。建议定期重新生成访问密钥。
Azure 会向我们提供两个访问密钥,这样,当重新生成其中一个时,可以使用另一个保持连接。
2,注入 BlobServiceClient,BlobService
BlobServiceClient
services.AddSingleton(x => new BlobServiceClient(Configuration.GetValue<string>("AzureBlobStorageConnectionString")));
初始化 创建一个BlobService类,并且在将 appsettings 中的 key 叫 ”AzureBlobStorageConnection“ 的 链接字符串的值当作参数放到构造函数中
BlobService
services.AddSingleton<IBlobSergvice, BlobService>();
3,BlobService 方法
3.1,获取 Blog 信息
#region 01,获取Blob,根据blob名称+async Task<BlobInfo> GetBlobAsync(string name)
/// <summary>
/// 获取Blob,根据blob名称
/// </summary>
/// <param name="name">blob名称</param>
/// <returns></returns>
public async Task<Azure.Storage.Models.BlobInfo> GetBlobAsync(string name)
{
var containerClient = _blobServiceClient.GetBlobContainerClient("picturecontainer"); var blobClient = containerClient.GetBlobClient(name);
var blobDownLoadInfo = await blobClient.DownloadAsync();
return new Azure.Storage.Models.BlobInfo(blobDownLoadInfo.Value.Content, blobDownLoadInfo.Value.ContentType);
}
#endregion
获取 Blob 存储信息具体实现方法,
1,首先我们可以看到先通过 "picturecontainer" 名称获取到 ContainerClient,再通过需要的 Blob 名称获取到 BlobClient
2,其次,通过异步的方法 “DownloadAsync” 进行下载 Blob 对象,其中包括 Blob 元数据,属性等信息
3,最后,我们将返回创建 BlobInfo 对象,在其构造函数中传入返回值的 Content 和 ContentType
3.2,获取 Blog 信息
#region 02,获取所有Blob名称+async Task<IEnumerable<string>> ListBlobsNameAsync()
/// <summary>
/// 获取所有Blob名称
/// </summary>
/// <returns></returns>
public async Task<IEnumerable<string>> ListBlobsNameAsync()
{
var containerClient = _blobServiceClient.GetBlobContainerClient("picturecontainer");
var items = new List<string>(); await foreach (var blobItem in containerClient.GetBlobsAsync())
{
items.Add(blobItem.Name);
}
return items;
}
#endregion
获取 Blob 存储信息具体实现方法,
1,首先我们可以看到先通过 "picturecontainer" 名称获取到 ContainerClient,再通过需要的 Blob 名称获取到 BlobClient
2,其次,通过异步 foreach 调用 ContainerClient 的 GetBlobsAsync“ 的方法,当前方法有多个默认参数
BlobTraits(Blob特性):默认获取包含所有特性的标识
BlobStates(Blob状态):指定应包含所有状态的Blob的标志
prefix(前缀):指定一个字符串,该字符串对结果进行过滤以仅返回其名称以指定的开头的 Blob 前缀
cancellationToken:传播有关应取消操作的通知
3,最后,将每一项的 Blob 的名称添加到集合中。
3.3,根据文件路径和文件名称上传文件
#region 03,上传文件,根据文件路径和文件名称+async Task UploadFileBlobAsync(string filePath, string filename)
/// <summary>
/// 上传文件,根据文件路径和文件名称
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="filename">文件名称</param>
/// <returns></returns>
public async Task UploadFileBlobAsync(string filePath, string filename)
{
var containerClient = _blobServiceClient.GetBlobContainerClient("picturecontainer");
var blobClient = containerClient.GetBlobClient(filename);
await blobClient.UploadAsync(filePath, new BlobHttpHeaders { ContentType = filePath.GetContentType() });
}
#endregion
上传Blob数据具体实现方法
1,首先我们可以看到先通过 "picturecontainer" 名称获取到 ContainerClient,再通过需要的 Blob 名称获取到 BlobClient
2,最后通过异步上传文件,此时需要指定文件的路径,以及在 BlobHttpHeaders 中指定文件内容的 ContentType
3.4,上传流
#region 04,上传文件流,根据文件内容和文件名称+async Task UploadContentBlobAsync(string content, string filename)
/// <summary>
/// 上传文件流,根据文件内容和文件名称
/// </summary>
/// <param name="content">文件内容</param>
/// <param name="filename">文件名称</param>
/// <returns></returns>
public async Task UploadContentBlobAsync(string content, string filename)
{
var containerClient = _blobServiceClient.GetBlobContainerClient("picturecontainer");
var blobClient = containerClient.GetBlobClient(filename);
var bytes = Encoding.UTF8.GetBytes(content);
await using var memoryStream = new MemoryStream(bytes);
await blobClient.UploadAsync(memoryStream, new BlobHttpHeaders() { ContentType = filename.GetContentType() });
}
#endregion
上传Blob数据具体实现方法
1,首先我们可以看到先通过 "picturecontainer" 名称获取到 ContainerClient,再通过需要的 Blob 名称获取到 BlobClient
2,其次,将上传的字符串转化成字节流
3,最后通过异步字节流上传,以及在 BlobHttpHeaders 中指定文件内容的 ContentType
3.5 删除 Blob 数据
#region 05,删除Blob+async Task DeleteBlobAsync(string blobName)
/// <summary>
/// 删除Blob
/// </summary>
/// <param name="blobName">blob名称</param>
/// <returns></returns>
public async Task DeleteBlobAsync(string blobName)
{
var containerClient = _blobServiceClient.GetBlobContainerClient("picturecontainer");
var blobClient = containerClient.GetBlobClient(blobName);
await blobClient.DeleteIfExistsAsync();
}
#endregion
删除blob 具体实现方法
1,首先我们可以看到先通过 "picturecontainer" 名称获取到 ContainerClient,再通过需要的 Blob 名称获取到 BlobClient
2,最后 调用异步 ”DeleteIfExistsAsync“ 方法,将 blob进行删除
ok,具体针对于Blob 的增 删 查 的方法就介绍完成
三,结尾
今天内容较少,只是把上一篇关于Blob操作的一些方法贴了出来,讲了一下对于 Blob 的操作,大家也可以通过微软官方文档:Azure Storage Blobs。下一篇将开始新的介绍 Storage 的新内容-----Azure Table Storage
github:https://github.com/yunqian44/Azure.Storage.git
作者:Allen
版权:转载请在文章明显位置注明作者及出处。如发现错误,欢迎批评指正。
Azure Blob (三)参数设置说明的更多相关文章
- MySQL Database on Azure 参数设置
在使用MySQL过程中,经常会根据需要对MySQL的参数进行一些设置和调整.作为PaaS版本的MySQL,MySQL Database on Azure在参数设置方面有一些限制,客户不能像使用on-p ...
- JVM系列三:JVM参数设置
JVM系列三:JVM参数设置.分析 不管是YGC还是Full GC,GC过程中都会对导致程序运行中中断,正确的选择不同的GC策略,调整JVM.GC的参数,可以极大的减少由于GC工作,而导致的程序运 ...
- Python 操作 Azure Blob Storage
笔者在<Azure 基础:Blob Storage>一文中介绍了 Azure Blob Storage 的基本概念,并通过 C# 代码展示了如何进行基本的操作.最近笔者需要在 Linux ...
- Azure Functions(二)集成 Azure Blob Storage 存储文件
一,引言 上一篇文章有介绍到什么是 SeverLess ,ServerLess 都有哪些特点,以及多云环境下 ServerLess 都有哪些解决方案.在这众多解决方案中就包括 Function App ...
- [New Portal]Windows Azure Storage (14) 使用Azure Blob的PutBlock方法,实现文件的分块、离线上传
<Windows Azure Platform 系列文章目录> 相关内容 Windows Azure Platform (二十二) Windows Azure Storage Servic ...
- hadoop(四): 本地 hbase 集群配置 Azure Blob Storage
基于 HDP2.4安装(五):集群及组件安装 创建的hadoop集群,修改默认配置,将hbase 存储配置为 Azure Blob Storage 目录: 简述 配置 验证 FAQ 简述: hadoo ...
- AzCopy – 上传/下载 Windows Azure Blob 文件
在我们收到的请求中,有一个频繁出现的请求是提供一种能在 Windows Azure Blob 存储与其本地文件系统之间轻松上传或下载文件的方法.一年半前, 我们很高兴地发布了 AzCopy, Wind ...
- [AWS vs Azure] 云计算里AWS和Azure的探究(6) - Amazon Simple Storage Service 和 Microsoft Azure Blob Storage
这几天Nasuni公司出了一份报告,分析了各个云厂商的云存储的性能,包括Amazon S3,Azure Blob Storage, Google Drive, HP以及Rackspace.其中性能上A ...
- Azure Blob Storage 基本用法 -- Azure Storage 之 Blob
Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure Table storage ...
随机推荐
- 铁大树洞与市面上现有APP对比
写在前面 铁大树洞这款APP严格来说并没有可以参照的对象,但如果非要说的话也可以有.这里我们选取百度贴吧进行对比. 百度贴吧 可以看到,百度贴吧的贴吧首页排版要更加好看,且在首页添加了各种分类.也许我 ...
- Springboot 在@Configuration注解的勒种 使用@Autowired或者@value注解 读取.yml属性失败
springboot中@value注解,读取yml属性失败 问题场景: 配置ShrioConfig时,想注入.yml的参数进行配置 解决办法: 如果注释掉shiroEhcacheManager 以下所 ...
- Pytorch_第八篇_深度学习 (DeepLearning) 基础 [4]---欠拟合、过拟合与正则化
深度学习 (DeepLearning) 基础 [4]---欠拟合.过拟合与正则化 Introduce 在上一篇"深度学习 (DeepLearning) 基础 [3]---梯度下降法" ...
- Python 错误 异常
8 错误,调试和测试 8.1错误处理 所有的异常来自 BaseException 记录错误 : # err_logging.py import logging def foo(s): return 1 ...
- one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [3, 1280, 28, 28]], which is output 0 of LeakyReluBackward1, is at version 2;
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace o ...
- [leetcode/lintcode 题解] 有效回文 II · Valid Palindrome II
[题目描述] 给一个非空字符串 s,你最多可以删除一个字符.判断是否可以把它变成回文串. 在线评测地址: https://www.lintcode.com/problem/valid-palindro ...
- C#LeetCode刷题-二叉搜索树
二叉搜索树篇 # 题名 刷题 通过率 难度 220 存在重复元素 III 19.3% 中等 315 计算右侧小于当前元素的个数 31.9% 困难 327 区间和的个数 29.5% 困难 3 ...
- Vue 父子组件表单同步校验
子组件代码 // 子组件 validateForm() { return new Promise((resolve, reject) => { this.$refs.contractBaseRe ...
- flask-sqlalchemy同字段多条件过滤
举例 from sqlalchemy import or_,and_# from operator import or_, and_ allapp = AppServer.query.filter(a ...
- 关于tomcat的一些基础知识
tomcat的启动环境是要需要配置jdk的,本次示例用的是jdk1.8和tomcat 8.5. jdk环境变量配置可以在网上随意找到,这里就不再作示范了. 什么是Tomcat Tomcat简单的说就是 ...