網易雲免費OSS服務用做Markdown圖床或博客圖片外鏈


我使用據說是Windows下最好用的Markdown編輯器“MarkdownPad2”(個人感覺還是Visual Code+Markdown插件666)寫Markdown,在貼圖方面遇到一個問題,於是找到了網易雲的免費OSS服務,並編寫了一個小工具來管理圖床(網易雲OSS)。

Markdown寫文檔很爽,這里不多說了;網易雲OSS免費服務的介紹去官網看就可以了,https://www.163yun.com

50GB免費存儲空間
每月100萬次免費GET請求
每月20GB免費下行流量
每月10萬次免費PUT請求

這個免費額度還是很大的,業界良心,比七牛雲要強多了。

————————————————————————————————————————————————————————————————————————————

分割線,下邊說正題

————————————————————————————————————————————————————————————————————————————

上圖是我寫的小工具的截圖,起初我使用QQ截圖后,需要:

  1. 保存到磁盤
  2. MarkdownPad上傳圖片(當然,后來也不能用了)

MarkdownPad使用一國外網站做圖床,每個MarkdownPad賬戶僅能上傳有限個數的圖片(據說破解版可以無限制上傳,可能我用的版本破解的不夠徹底吧),因此用了一陣子后再也不能貼圖了(其實這個國外網站貼圖很慢的)。

於是開始尋覓免費圖床,很多圖床的免費服務都有限制,比如圖片個數、保存時限,更重要的是一些小圖床不知道過多久就關門大吉了。

有朋友使用GitHub做圖床,我沒有試過,我使用了開源中國(OSChina)的碼雲做圖床,但圖片存儲的是Base64編碼,太占地方了。

后來我找到了網易雲的OSS服務,免費注冊認證過后,按照SDK編寫了接口程序(網易雲的C#示例代碼竟然有錯誤語法,好粗心。。。)。

有了這個小工具后,我使用QQ截圖后,直接在Markdown中Ctrl+V即可,方便多了。

貼個小圖,求打賞。

下載鏈接(百度雲盤):https://pan.baidu.com/s/1jKcQl5W 密碼:hxad

使用時,配置注冊的網易雲OSS的endpoint和accessKeyId、accessKeySecret,以及創建的bucket名稱。

另外兩個參數對應於程序窗體的復選框。

——————————————————————————————————————————————————————————————————

 沒代碼的工具,不應該貼在博客園里,下邊附上關鍵代碼

——————————————————————————————————————————————————————————————————

阿里雲OSS輔助類,實現阿里雲OSS接口,摘抄自阿里雲幫助文檔。

  1 using Netease.Cloud.NOS;
  2 using System;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 
  8 namespace Eyuan._163Yun.Lib
  9 {
 10     public class YunHelper
 11     {
 12         #region 配置
 13         public static string endpoint = "nos-eastchina1.126.net";
 14         public static string accessKeyId = "";
 15         public static string accessKeySecret = "";
 16         public static NosClient nosClient = null;
 17         //
 18         public static string bucket = "dump";
 19         /// <summary>
 20         /// 初始化 NosClient
 21         /// </summary> 
 22         public static void InitClient()
 23         {
 24             //
 25             InitConfig();
 26             //
 27             ClientConfiguration conf = new ClientConfiguration();
 28             // 設置NosClient連接超時時間,單位:毫秒
 29             conf.ConnectionTimeout = 50000;
 30             // 設置NosClient使用的最大連接數
 31             //conf.MaxConnections(200);
 32             // 設置socket超時時間,單位:毫秒
 33             //conf.SocketTimeout(10000);
 34             // 設置失敗請求重試次數
 35             //conf.MaxErrorRetry(2);
 36             //nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
 37             if (string.IsNullOrEmpty(endpoint) || string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(accessKeySecret))
 38             { 
 39                 return;
 40             }
 41             nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret, conf);
 42         }
 43         private static void InitConfig()
 44         {
 45             endpoint = ConfigHelper.Endpoint;
 46             accessKeyId = System.Configuration.ConfigurationManager.AppSettings["accessKeyId"];
 47             accessKeySecret = System.Configuration.ConfigurationManager.AppSettings["accessKeySecret"];
 48             bucket = ConfigHelper.Bucket;
 49         }
 50         #endregion
 51 
 52         #region 上傳文件
 53         /// <summary>
 54         /// 上傳文件
 55         /// </summary>
 56         /// <param name="bucket">桶名</param>
 57         /// <param name="key">對象名</param>
 58         /// <param name="fileToUpload">上傳的文件</param>
 59         public static void PutObject(string bucket, string key, string fileToUpload)
 60         {
 61             try
 62             {
 63                 nosClient.PutObject(bucket, key, fileToUpload);
 64                 Console.WriteLine("Put object:{0} succeeded", key);
 65             }
 66             catch (NosException ex)
 67             {
 68                 Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
 69                 ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
 70             }
 71             catch (Exception ex)
 72             {
 73                 Console.WriteLine("Failed with error info: {0}", ex.Message);
 74             }
 75         }
 76         #endregion
 77 
 78         #region 下載文件
 79         /// <summary>
 80         /// 下載文件
 81         /// </summary>
 82         /// <param name="bucket">桶名</param>
 83         /// <param name="key">對象名</param>
 84         /// <param name="dirToDownload">下載文件存放的目錄</param>
 85         public static void GetObject(string bucket, string key, string dirToDownload)
 86         {
 87             try
 88             {
 89                 nosClient.GetObject(bucket, key, dirToDownload + "/sample.data");
 90                 Console.WriteLine("Get object succeeded");
 91             }
 92             catch (NosException ex)
 93             {
 94                 Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
 95                 ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
 96             }
 97             catch (Exception ex)
 98             {
 99                 Console.WriteLine("Failed with error info: {0}", ex.Message);
100             }
101         }
102         #endregion
103 
104         #region 列舉桶內文件
105         /// <summary>
106         /// 列舉桶內文件
107         /// </summary>
108         /// <param name="bucket">桶名</param>
109         public static void ListObjects(string bucket)
110         {
111             try
112             {
113                 var keys = new List<string>();
114                 var listObjectsRequest = new ListObjectsRequest(bucket);
115                 ObjectListing result = nosClient.ListObjects(listObjectsRequest);
116                 foreach (var summary in result.ObjectSummarise)
117                 {
118                     Console.WriteLine(summary.Key);
119                     keys.Add(summary.Key);
120                 }
121 
122                 Console.WriteLine("List objects of bucket:{0} succeeded ", bucket);
123             }
124             catch (NosException ex)
125             {
126                 Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
127                 ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
128             }
129             catch (Exception ex)
130             {
131                 Console.WriteLine("Failed with error info: {0}", ex.Message);
132             }
133         }
134         #endregion
135 
136         #region 刪除單個文件
137         /// <summary>
138         /// 刪除單個文件
139         /// </summary>
140         /// <param name="bucket">桶名</param>
141         /// <param name="key">對象名</param>
142         public static void DeleteObject(string bucket, string key)
143         {
144             try
145             {
146                 nosClient.DeleteObject(bucket, key);
147                 Console.WriteLine("Delete object succeeded");
148             }
149             catch (NosException ex)
150             {
151                 Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
152                 ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
153             }
154             catch (Exception ex)
155             {
156                 Console.WriteLine("Failed with error info: {0}", ex.Message);
157             }
158         }
159         #endregion
160 
161     }
162 }

 我的博客即將搬運同步至騰訊雲+社區,邀請大家一同入駐:https://cloud.tencent.com/developer/support-plan?invite_code=2ui9qt2awpwkg


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM