马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

实现一个基于码云的Storage

[复制链接]
查看1971 | 回复0 | 2019-9-12 16:22:21 | 显示全部楼层 |阅读模式

实现一个简单的基于码云(Gitee) 的 Storage

Intro

上次在 asp.net core 从单机到集群 一文中提到存储还不支持分布式,并立了一个 flag

基于 github 大概 开源中国的码云实现一个 storage

于是这两天就来填坑了。。

实现了一个简单的基于开源中国的码云的 storage

准备工作

码云官方有 API 接口列表 https://gitee.com/api/v5/swagger

上传文件API: https://gitee.com/api/v5/swagger#/postV5ReposOwnerRepoContentsPath

162543o0z10vdqqll00az7.png

新建一个仓库来存我们要生存的文件,新建的时间分支模型选择默认的单分支模型即可,只要master分支即可 ,最好直接创建 readme 大概新加一个文件以创建分支:

然后必要创建一个 accessToken,在 设置里的私人令牌设置中新建一个token,必要 projects 权限

162544y5fhesz3ss4fz0f3.png

Storage 简单实现

参考上面 Gitee 提供的 API 接口,自己实现了一个简单的 GiteeStorageProvider ,Github 完整源码:https://github.com/WeihanLi/ActivityReservation/blob/dev/ActivityReservation.Common/StorageProvider.cs

  1. <code>/// <summary>
  2. /// 码云存储
  3. /// </summary>
  4. public class GiteeStorageProvider : IStorageProvider
  5. {
  6. private const string PostFileApiUrlFormat = "https://gitee.com/api/v5/repos/{0}/{1}/contents{2}";
  7. private const string RawFileUrlFormat = "https://gitee.com/{0}/{1}/raw/master{2}";
  8. private readonly HttpClient _httpClient;
  9. private readonly ILogger _logger;
  10. private readonly GiteeStorageOptions _options;
  11. public GiteeStorageProvider(HttpClient httpClient, ILogger<GiteeStorageProvider> logger, IOptions<GiteeStorageOptions> options)
  12. {
  13. _logger = logger;
  14. _httpClient = httpClient;
  15. _options = options.Value;
  16. }
  17. public async Task<string> SaveBytes(byte[] bytes, string filePath)
  18. {
  19. var base64Str = Convert.ToBase64String(bytes);
  20. using (var response = await _httpClient.PostAsFormAsync(PostFileApiUrlFormat.FormatWith(_options.UserName, _options.RepositoryName, filePath),
  21. new Dictionary<string, string>
  22. {
  23. { "access_token", _options.AccessToken },
  24. { "content", base64Str },
  25. { "message" , $"add file" }
  26. }))
  27. {
  28. if (response.IsSuccessStatusCode)
  29. {
  30. return RawFileUrlFormat
  31. .FormatWith(_options.UserName, _options.RepositoryName, filePath);
  32. }
  33. var result = await response.Content.ReadAsStringAsync();
  34. _logger.LogWarning($"post file error, response: {result}");
  35. return null;
  36. }
  37. }
  38. }
  39. public class GiteeStorageOptions
  40. {
  41. public string UserName { get; set; }
  42. public string RepositoryName { get; set; }
  43. public string AccessToken { get; set; }
  44. }
  45. </code>
复制代码

服务注册,这里用了 HttpClientFactory 来利用 HttpClient,个人比力喜好用强范例的 HttpClient,假如喜好利用通过 IHttpClientFactory 来表现创建,也可以注入一个 IHttpClientFactory ,在内部创建 HttpClient

  1. <code>services.Configure<GiteeStorageOptions>(Configuration.GetSection("Storage:Gitee"));
  2. services.AddHttpClient<IStorageProvider, GiteeStorageProvider>();
  3. services.TryAddSingleton<IStorageProvider, GiteeStorageProvider>();</code>
复制代码

设置示例:

  1. <code>{
  2. "Storage":{
  3. "Gitee":{
  4. "UserName": "weihanli",
  5. "RepositoryName": "storage",
  6. "AccessToken": "xxx"
  7. }
  8. }
  9. }</code>
复制代码

利用结果

162545fcqc6s2207ii0mf6.png

可以看到上传的图片已经上传到我们新建的仓库了,到仓库里看一下:

162545k9vtgg9gn87gjzxx.png

More

只实现了上传,原来想也加一个列出某个目次下的所有文件及子目次,但是看似乎没有接口,假如要实现的话,大概只能基于 git 去实现,从 git 信息里获取,临时不怎么用到,先不管了,临时搁置吧,

Reference

  • https://www.jianshu.com/p/224954dadcaf
  • https://gitee.com/weihanli/storage
  • https://github.com/WeihanLi/ActivityReservation






来源:https://www.cnblogs.com/weihanli/archive/2019/09/10/implement-a-storage-provider-based-on-gitee.html
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则