网易云免费OSS服务用做Markdown图床或博客图片外链

我使用据说是Windows下最好用的Markdown编辑器“MarkdownPad2”(个人感觉还是Visual Code+Markdown插件666)写Markdown,在贴图方面遇到一个问题,于是找到了网易云的免费OSS服务,并编写了一个小工具来管理图床(网易云OSS)。

Markdown写文档很爽,这里不多说了;网易云OSS免费服务的介绍去官网看就可以了,https://www.163yun.com

50GB免费存储空间

每月100万次免费GET请求


每月20GB免费下行流量


每月10万次免费PUT请求

这个免费额度还是很大的,业界良心,比七牛云要强多了。

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

分割线,下边说正题

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

网易云免费OSS服务用做Markdown图床或博客图片外链

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

  1. 保存到磁盘
  2. MarkdownPad上传图片(当然,后来也不能用了)

MarkdownPad使用一国外网站做图床,每个MarkdownPad账户仅能上传有限个数的图片(据说破解版可以无限制上传,可能我用的版本破解的不够彻底吧),因此用了一阵子后再也不能贴图了(其实这个国外网站贴图很慢的)。

于是开始寻觅免费图床,很多图床的免费服务都有限制,比如图片个数、保存时限,更重要的是一些小图床不知道过多久就关门大吉了。

有朋友使用GitHub做图床,我没有试过,我使用了开源中国(OSChina)的码云做图床,但图片存储的是Base64编码,太占地方了。

后来我找到了网易云的OSS服务,免费注册认证过后,按照SDK编写了接口程序(网易云的C#示例代码竟然有错误语法,好粗心。。。)。

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

网易云免费OSS服务用做Markdown图床或博客图片外链

贴个小图,求打赏。

下载链接(百度云盘):https://pan.baidu.com/s/1jKcQl5W 密码:hxad

使用时,配置注册的网易云OSS的endpoint和accessKeyId、accessKeySecret,以及创建的bucket名称。

另外两个参数对应于程序窗体的复选框。

网易云免费OSS服务用做Markdown图床或博客图片外链

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

没代码的工具,不应该贴在博客园里,下边附上关键代码

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

阿里云OSS辅助类,实现阿里云OSS接口,摘抄自阿里云帮助文档。

 using Netease.Cloud.NOS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Eyuan._163Yun.Lib
{
public class YunHelper
{
#region 配置
public static string endpoint = "nos-eastchina1.126.net";
public static string accessKeyId = "";
public static string accessKeySecret = "";
public static NosClient nosClient = null;
//
public static string bucket = "dump";
/// <summary>
/// 初始化 NosClient
/// </summary>
public static void InitClient()
{
//
InitConfig();
//
ClientConfiguration conf = new ClientConfiguration();
// 设置NosClient连接超时时间,单位:毫秒
conf.ConnectionTimeout = ;
// 设置NosClient使用的最大连接数
//conf.MaxConnections(200);
// 设置socket超时时间,单位:毫秒
//conf.SocketTimeout(10000);
// 设置失败请求重试次数
//conf.MaxErrorRetry(2);
//nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
if (string.IsNullOrEmpty(endpoint) || string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(accessKeySecret))
{
return;
}
nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret, conf);
}
private static void InitConfig()
{
endpoint = ConfigHelper.Endpoint;
accessKeyId = System.Configuration.ConfigurationManager.AppSettings["accessKeyId"];
accessKeySecret = System.Configuration.ConfigurationManager.AppSettings["accessKeySecret"];
bucket = ConfigHelper.Bucket;
}
#endregion #region 上传文件
/// <summary>
/// 上传文件
/// </summary>
/// <param name="bucket">桶名</param>
/// <param name="key">对象名</param>
/// <param name="fileToUpload">上传的文件</param>
public static void PutObject(string bucket, string key, string fileToUpload)
{
try
{
nosClient.PutObject(bucket, key, fileToUpload);
Console.WriteLine("Put object:{0} succeeded", key);
}
catch (NosException ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
#endregion #region 下载文件
/// <summary>
/// 下载文件
/// </summary>
/// <param name="bucket">桶名</param>
/// <param name="key">对象名</param>
/// <param name="dirToDownload">下载文件存放的目录</param>
public static void GetObject(string bucket, string key, string dirToDownload)
{
try
{
nosClient.GetObject(bucket, key, dirToDownload + "/sample.data");
Console.WriteLine("Get object succeeded");
}
catch (NosException ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
#endregion #region 列举桶内文件
/// <summary>
/// 列举桶内文件
/// </summary>
/// <param name="bucket">桶名</param>
public static void ListObjects(string bucket)
{
try
{
var keys = new List<string>();
var listObjectsRequest = new ListObjectsRequest(bucket);
ObjectListing result = nosClient.ListObjects(listObjectsRequest);
foreach (var summary in result.ObjectSummarise)
{
Console.WriteLine(summary.Key);
keys.Add(summary.Key);
} Console.WriteLine("List objects of bucket:{0} succeeded ", bucket);
}
catch (NosException ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
#endregion #region 删除单个文件
/// <summary>
/// 删除单个文件
/// </summary>
/// <param name="bucket">桶名</param>
/// <param name="key">对象名</param>
public static void DeleteObject(string bucket, string key)
{
try
{
nosClient.DeleteObject(bucket, key);
Console.WriteLine("Delete object succeeded");
}
catch (NosException ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
#endregion }
}

我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=2ui9qt2awpwkg

上一篇:9.mysql性能优化-慢查询分析、优化索引和配置


下一篇:Spring(2)——Spring IoC 详解