[C#] 可程式化的免費圖床方案 - SM.MS
2024-08-26
免費圖床系列介紹之續篇,今天帶來的是 sm.ms。這是一家歷史悠久的圖床服務,許多朋友都推薦使用它。
sm.ms 因為服務穩定、規則清晰明確,尤其在免費方案方面表現不錯,所以我決定趁此機會記錄下這些使用心得。
1. 你得先註冊,然後就可以到這裡 https://sm.ms/home/apitoken
之後就會拿到 Secret Token ,拿到之後就是程式碼的部份
2. 接下來就是,上傳圖片的部份,基本上沒有很困難,在它們的 API 文件中,其實也可以透過自己帳號密碼拿到 Secret Token
C# Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 構建 HTTP 請求 | |
using (var client = new HttpClient()) | |
{ | |
client.DefaultRequestHeaders.Add("Authorization", "{api_secret}"); | |
using (var content = new MultipartFormDataContent()) | |
{ | |
var fileStream = File.OpenRead(filePath); | |
content.Add(new StreamContent(fileStream), "smfile", Path.GetFileName(filePath)); | |
try | |
{ | |
var response = client.PostAsync("https://sm.ms/api/v2/upload", content).Result; | |
var responseContent = response.Content.ReadAsStringAsync().Result; | |
return System.Text.Json.JsonDocument.Parse(responseContent).RootElement.GetProperty("images").ToString(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"An error occurred: {ex.Message}"); | |
return null; | |
} | |
} | |
} | |
/* JSON Response | |
{"success":false,"code":"image_repeated","message":"Image upload repeated limit, this | |
image exists at: https://s2.loli.net/2024/08/26/NbLBOsMumHajYwA.jpg", | |
"images":"https://s2.loli.net/2024/08/26/NbLBOsMumHajYwA.jpg","RequestId":"78F65CE4-80C2-4B85-94D2-D670F2DABFC9"} | |
*/ | |
看一下他的免費原則
有 5G 的 Total 可以用不算少,但是他有限制每分鐘,不能超過20 ,一個小時不能超過一百,一天不能超過兩百
我覺得這數字對於單一小網站夠用,但是如果稍微圖片多一點就會超過每天的限制,不過畢竟免費,這就算是一個還算不錯的穩定服務
而且有後台可以自己管理。
reference: