SDK 提供了文件上传后进行回调(Callback)的功能。您只需要在发送给 US3 的请求中携带相应的 Callback 参数,即能实现回调。
上传回调接口
目前SDK支持回调的上传接口有:
参数名 | 说明 |
---|---|
PutFileWithPolicy | 普通上传回调,完整代码详见 Github。 |
FinishMultipartUploadWithPolicy | 完成分片上传回调,完整代码详见 Github。 |
MPutWithPolicy | 同步分片上传回调,完整代码详见 Github。 |
AsyncMPutWithPolicy | 异步分片上传回调,完整代码详见 Github。 |
AsyncUploadWithPolicy | 异步分片并发上传回调,完整代码详见 Github。 |
说明
- 以上所有分片上传回调实际上都是通过调用 FinishMultipartUploadWithPolicy 完成。
代码示例
示例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
ufsdk "github.com/ufilesdk-dev/ufile-gosdk"
"log"
)
const (
ConfigFile = "./config.json"
FilePath = "config.json"
FileKey = "mongo"
MimeType = ""
// 若指定callbackBodyType,US3 会使用 POST Method 将 callbackBody 以 callbackBodyType 的格式发送到 callbackUrl
jsonPolicy = `{"callbackUrl":"https://ip:port/path","callbackBody":"{\"Name\":\"Alice\", \"Age\":20}","callbackBodyType": "application/json"}`
// 若未指定 callbackBodyType,US3 会使用 POST Method 将 callbackBody 以 URL Query 的格式发送到 callbackUrl
queryPolicy = `{"callbackUrl":"http://ip:port/path","callbackBody":"Name=$(x:name)&Age=$(x:age)","callbackVar":{"x:name": "Alice","x:age": "20"}}`
)
func main() {
// 加载配置,构造请求
config, err := ufsdk.LoadConfig(ConfigFile)
if err != nil {
panic(err.Error())
}
req, err := ufsdk.NewFileRequest(config, nil)
if err != nil {
panic(err.Error())
}
// 简单上传回调
err = req.PutFileWithPolicy(FilePath, FileKey, MimeType, jsonPolicy)
if err != nil {
log.Fatalf("PutWithPolicy 上传回调失败,错误信息为:%s\n", req.DumpResponse(true))
}
log.Printf("PutWithPolicy 上传回调成功")
_ = req.DeleteFile(FileKey)
// 同步分片上传回调
err = req.MPutWithPolicy(FilePath, FileKey, MimeType, jsonPolicy)
if err != nil {
log.Fatalf("MPutWithPolicy 上传文件失败,错误信息为:%s\n", req.DumpResponse(true))
}
log.Printf("MPutWithPolicy 上传回调成功")
_ = req.DeleteFile(FileKey)
// 异步分片上传回调
err = req.AsyncMPutWithPolicy(FilePath, FileKey, MimeType, queryPolicy)
if err != nil {
log.Fatalf("AsyncMPutWithPolicy 上传文件失败,错误信息为:%s\n", req.DumpResponse(true))
}
log.Printf("AsyncMPutWithPolicy 上传回调成功")
_ = req.DeleteFile(FileKey)
// 异步分片并发上传回调
jobs := 20 // 并发数为 20
err = req.AsyncUploadWithPolicy(FilePath, FileKey, MimeType, jobs, queryPolicy)
if err != nil {
log.Fatalf("AsyncUploadWithPolicy 上传回调失败,错误信息为:%s\n", req.DumpResponse(true))
}
log.Printf("AsyncUploadWithPolicy 上传回调成功")
_ = req.DeleteFile(FileKey)
}
错误码
HTTP 状态码 | RetCode | ErrMsg | 描述 |
---|---|---|---|
400 | -148653 | bucket not exists | 存储空间不存在 |
400 | -15036 | check md5 failed | MD5校验失败 |
401 | -148643 | no authorization found | 上传凭证错误 |
403 | -148643 | invalid signature | API公私钥错误 |
400 | -148660 | invalid put policy | 回调参数具体参见[待补充] |
599 | 20003 | send request error: Post https://…: dial tcp xx.xxx.xx.xx:xx: getsockopt: connection refused | 用户回调服务不可用 |
599 | 20008 | expect json, but received text/plain; charset=utf-8 | 回调字符串需指定 callbackBodyType 为 application/json |
599 | 20008 | expect json, but the response body is not a strict json string | 用户回调服务返回值必须是json形式 |
PREVIOUS指定存储类型上传
NEXT比较本地文件和远程文件etag