通过 Base64 数据 URL 上传文件
curl --request POST \
--url https://files-api.sinancode.com/api/upload/base64 \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"data": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
}
'import requests
url = "https://files-api.sinancode.com/api/upload/base64"
payload = { "data": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==" }
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
data: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='
})
};
fetch('https://files-api.sinancode.com/api/upload/base64', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://files-api.sinancode.com/api/upload/base64",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'data' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://files-api.sinancode.com/api/upload/base64"
payload := strings.NewReader("{\n \"data\": \"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://files-api.sinancode.com/api/upload/base64")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"data\": \"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://files-api.sinancode.com/api/upload/base64")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"data\": \"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==\"\n}"
response = http.request(request)
puts response.read_body{
"url": "https://upload-tmp.sinancode.com/uploads/user123/abc123def456.png",
"cn_url": "https://beijing-user-upload-tmp.tos-cn-beijing.volces.com/uploads/user123/abc123def456.png",
"key": "uploads/user123/abc123def456.png",
"md5": "d41d8cd98f00b204e9800998ecf8427e",
"size": 12345,
"content_type": "image/png",
"expires_at": "2024-01-17T00:00:00.000Z"
}{
"message": "Missing required field: data",
"error_code": "UPLOAD.INVALID_FILE"
}{
"message": "Missing authorization token",
"error_code": "AUTH.MISSING_TOKEN"
}{
"message": "File size exceeds maximum limit of 50MB",
"error_code": "UPLOAD.FILE_TOO_LARGE"
}{
"message": "Internal server error",
"error_code": "INTERNAL_ERROR"
}文件上传
文件上传(Base64)
上传 Base64 编码的数据 URL 格式文件。MIME 类型会从数据 URL 前缀自动检测。
POST
/
api
/
upload
/
base64
通过 Base64 数据 URL 上传文件
curl --request POST \
--url https://files-api.sinancode.com/api/upload/base64 \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"data": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
}
'import requests
url = "https://files-api.sinancode.com/api/upload/base64"
payload = { "data": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==" }
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
data: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='
})
};
fetch('https://files-api.sinancode.com/api/upload/base64', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://files-api.sinancode.com/api/upload/base64",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'data' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://files-api.sinancode.com/api/upload/base64"
payload := strings.NewReader("{\n \"data\": \"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://files-api.sinancode.com/api/upload/base64")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"data\": \"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://files-api.sinancode.com/api/upload/base64")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"data\": \"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==\"\n}"
response = http.request(request)
puts response.read_body{
"url": "https://upload-tmp.sinancode.com/uploads/user123/abc123def456.png",
"cn_url": "https://beijing-user-upload-tmp.tos-cn-beijing.volces.com/uploads/user123/abc123def456.png",
"key": "uploads/user123/abc123def456.png",
"md5": "d41d8cd98f00b204e9800998ecf8427e",
"size": 12345,
"content_type": "image/png",
"expires_at": "2024-01-17T00:00:00.000Z"
}{
"message": "Missing required field: data",
"error_code": "UPLOAD.INVALID_FILE"
}{
"message": "Missing authorization token",
"error_code": "AUTH.MISSING_TOKEN"
}{
"message": "File size exceeds maximum limit of 50MB",
"error_code": "UPLOAD.FILE_TOO_LARGE"
}{
"message": "Internal server error",
"error_code": "INTERNAL_ERROR"
}通过 Base64 数据 URL 格式上传文件到云存储。
接口特点:
- 前端友好 - 适合 Canvas、截图等场景直接上传
- 自动解析 - 从 data URL 前缀自动检测 MIME 类型
- 大文件支持 - 最大支持 50MB 文件上传
- 双线路 - 返回全球和中国两个访问 URL
授权
从 api.sinancode.com 获取的授权令牌
请求体
application/json
Base64 数据 URL,格式:data:{mime-type};base64,{base64-data}
示例:
"data:image/png;base64,iVBORw0KGgo..."
响应
文件上传成功
已上传文件的公开访问 URL(全球)
示例:
"https://upload-tmp.sinancode.com/uploads/user123/abc123def456.png"
已上传文件的公开访问 URL(中国)
示例:
"https://beijing-user-upload-tmp.tos-cn-beijing.volces.com/uploads/user123/abc123def456.png"
已上传文件的存储键
示例:
"uploads/user123/abc123def456.png"
文件内容的哈希值(SHA-256 截断)
示例:
"d41d8cd98f00b204e9800998ecf8427e"
文件大小(字节)
示例:
12345
文件的 MIME 类型
示例:
"image/png"
文件过期时间(ISO 8601 格式)
示例:
"2024-01-17T00:00:00.000Z"
⌘I