由于Typora的插入的文件、截图都是保存到本地,所以想把文件地址自动设置为云端地址。
刚开始查了下发现Typora支持自动上传文件
网上查了下教程,就开始按照Typora+pic-Go+阿里云Oss开始配置了起来,文件上传倒是可以,但是我的oss是私有的,pic-go不支持。
无奈之下只好自己简单写了一会儿代码。
// See https://aka.ms/new-console-template for more information
using Aliyun.OSS;
using Microsoft.Extensions.Configuration;
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Upload Success:");
var config = new ConfigurationBuilder().AddJsonFile("config.json", true, true).Build();
foreach (string arg in args)
{
var filePath = arg?.Replace("\"", "");
var endpoint = config.GetSection("endpoint").Value;
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
var accessKeyId = config.GetSection("accessKeyId").Value;
var accessKeySecret = config.GetSection("accessKeySecret").Value;
// 填写Bucket名称,例如examplebucket。
var bucketName = config.GetSection("bucket").Value;
// 创建OSSClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
var fileName = Path.GetFileName(filePath);
var path = config.GetSection("path")?.Value;
var bucketPath = path + fileName;
client.PutObject(bucketName, bucketPath, filePath);
client.SetObjectAcl(bucketName, bucketPath, CannedAccessControlList.PublicRead);
var customUrl = config.GetSection("customUrl")?.Value;//回写地址
if (string.IsNullOrEmpty(path))
{
Console.WriteLine("https://oss-cn-shanghai.aliyuncs.com/" + bucketPath);
}
else
{
Console.WriteLine(customUrl + "/" + bucketPath);
}
}
}
}
配置文件如下config.json
{
"bucket": "",
"accessKeyId": "",
"accessKeySecret": "",
"endpoint": "oss-cn-shanghai.aliyuncs.com",
"path": "img/",
"customUrl": "https://cdn.test.cn"
}
放到typora测试
标签:GetSection,上传,Oss,Typora,Value,var,path,config,bucketPath From: https://www.cnblogs.com/lostsea/p/17226262.html