[C#] 可程式化的免費圖床方案 - cdn.ipfsscan.io
2024-10-24
一樣今天是圖床篇,今天剛好有看到一個免費的 IPFS ( InterPlanetary File System )圖床 https://cdn.ipfsscan.io/
他竟然也有 client 端的上傳工具,大概玩了一下,很自由而且免註冊
後來測試一下,他的上傳非常簡單,重點是免註冊,但是速度稍微偏慢,鋼上傳的時候第一次 loading 比較久
之後就會比較快,或許是在做 CDN 的發散同步,畢竟他主打的是 IPFS ,接下來就是關於程式碼的部分
1. 這裡關於 http 的處理我都是使用 RestSharp
2. 上傳圖片檔案 程式碼
C# code :
This file contains hidden or 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
var filePath = AppDomain.CurrentDomain.BaseDirectory + "oooo.jpg"; | |
UploadImageToIPFS(filePath); | |
static void UploadImageToIPFS(string filePath) | |
{ | |
var client = new RestClient("https://app.img2ipfs.org/api/v0/add"); | |
var request = new RestRequest("", Method.Post); | |
//不要手動設置 Content-Type,讓 RestSharp 自動處理 multipart | |
request.AddHeader("accept", "application/json, text/javascript, */*; q=0.01"); | |
request.AddHeader("origin", "https://cdn.ipfsscan.io"); | |
request.AddHeader("referer", "https://cdn.ipfsscan.io/"); | |
request.AddHeader("sec-fetch-mode", "cors"); | |
request.AddHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0"); | |
// 使用 AddFile 讓 RestSharp 自動處理 boundary | |
request.AddFile("file", filePath, "image/jpeg"); | |
// 發送請求 | |
var response = client.Execute(request); | |
Console.WriteLine(response.Content); // 印出 API 回應 | |
} | |
//Result | |
/* | |
{"Name":"oooo.jpg","Hash":"Qmb17UZj1FH4DchcJcfqAtMn4Qn2z5TLkm1dcCrT8GATGh","Size":"5009", | |
"Url":"https://cdn.img2ipfs.com/ipfs/Qmb17UZj1FH4DchcJcfqAtMn4Qn2z5TLkm1dcCrT8GATGh?filename=oooo.jpg"} | |
*/ | |
result:
{"Name":"oooo.jpg","Hash":"Qmb17UZj1FH4DchcJcfqAtMn4Qn2z5TLkm1dcCrT8GATGh","Size":"5009",
"Url":"https://cdn.img2ipfs.com/ipfs/Qmb17UZj1FH4DchcJcfqAtMn4Qn2z5TLkm1dcCrT8GATGh?filename=oooo.jpg"}
這時候你只要拿到 Url 就可以直接使用了,這看起來很輕量簡單好用免註冊,推薦給大家,如果商用就不建議了,建議自己要備份一份