import "github.com/upyun/go-sdk/upyun"
UPYUN Go SDK, 集成:
- UPYUN Go SDK
示例代码见 examples/。
type UpYun struct {
Bucket string // 空间名(即服务名称)
Username string // 操作员
Passwd string // 密码
ChunkSize int // 块读取大小, 默认32KB
}func NewUpYun(bucket, username, passwd string) *UpYun// Auto: Auto detected, based on user's internet
// Telecom: (ISP) China Telecom
// Cnc: (ISP) China Unicom
// Ctt: (ISP) China Tietong
const (
Auto = iota
Telecom
Cnc
Ctt
)
func (u *UpYun) SetEndpoint(ed int) errorfunc (u *UpYun) Usage() (int64, error)func (u *UpYun) Mkdir(key string) errorfunc (u *UpYun) Put(key string, value io.Reader, useMD5 bool,
headers map[string]string) (http.Header, error)key 为 UPYUN 上的存储路径,value 既可以是文件,也可以是 buffer,useMD5 是否 MD5 校验,headers 自定义上传参数,除 上传参数,还可以设置 Content-Length,支持流式上传。流式上传需要指定 Contnet-Length,如需 MD5 校验,需要设置 Content-MD5。
func (u *UpYun) ResumePut(key string, value *os.File, useMD5 bool,
headers map[string]string, reporter ResumeReporter) (http.Header, error)以断点续传方式上传文件,当文件在上传过程中遭遇网络故障时,将等待 5 秒后,在失败断点处自动重试 3 次。参数 reporter 用于报告上传进度。可通过修改全局变量 ResumeWaitTime 与 ResumeRetryCount 自定义重试等待时间与重试次数。
func (u *UpYun) Get(key string, value io.Writer) (int, error)此方法返回文件大小
// 同步删除
func (u *UpYun) Delete(key string) error
// 异步删除文件
func (u *UpYun) AsyncDelete(key string) errortype FileInfo struct {
Size int64 // 文件大小
Time time.Time // 修改时间
Name string // 文件名
Type string // 类型,folder 或者 file
}
func (u *UpYun) GetInfo(key string) (*FileInfo, error)// 少量文件
func (u *UpYun) GetList(key string) ([]*FileInfo, error)
// 大量文件
func (u *UpYun) GetLargeList(key string, asc, recursive bool) (chan *FileInfo, chan error)key 必须为目录。对于目录下有大量文件的,建议使用 GetLargeList。
func (u *UpYun) Purge(urls []string) (string, error)type UpYunForm struct {
Secret string // 表单密钥
Bucket string // 空间名(即服务名称)
}func NewUpYunForm(bucket, key string) *UpYunFormtype FormAPIResp struct {
Code int `json:"code"`
Msg string `json:"message"`
Url string `json:"url"`
Timestamp int64 `json:"time"`
ImgWidth int `json:"image-width"`
ImgHeight int `json:"image-height"`
ImgFrames int `json:"image-frames"`
ImgType string `json:"image-type"`
Sign string `json:"sign"`
}func (u *UpYunForm) SetEndpoint(ed int) errorfunc (uf *UpYunForm) Put(fpath, saveas string, expireAfter int64,
options map[string]string) (*FormAPIResp, error)fpath 上传文件名,saveas UPYUN 存储保存路径,expireAfter 过期时间长度,options 上传参数。
type UpYunMultiPart struct {
Bucket string // 空间名(即服务名称)
Secret string // 表单密钥
BlockSize int64 // 分块大小,单位字节, 建议 1024000
}type UploadResp struct {
// returns after init request
SaveToken string `json:"save_token"`
// token_secert is equal to UPYUN Form API Secret
Secret string `json:"token_secret"`
// UPYUN Bucket Name
Bucket string `json:"bucket_name"`
// Number of Blocks
Blocks int `json:"blocks"`
Status []int `json:"status"`
ExpireAt int64 `json:"expire_at"`
}type MergeResp struct {
Path string `json:"path"`
ContentType string `json:"mimetype"`
ContentLength interface{} `json:"file_size"`
LastModify int64 `json:"last_modified"`
Signature string `json:"signature"`
ImageWidth int `json:"image_width"`
ImageHeight int `json:"image_height"`
ImageFrames int `json:"image_frames"`
}func NewUpYunMultiPart(bucket, secret string, blocksize int64) *UpYunMultiPartfunc (ump *UpYunMultiPart) Put(fpath, saveas string,
expireAfter int64, options map[string]interface{}) (*MergeResp, error)type UpYunMedia struct {
Username string // 操作员
Passwd string // 密码
Bucket string // 空间名(即服务名称)
}type MediaStatusResp struct {
Tasks map[string]interface{} `json:"tasks"`
}func NewUpYunMedia(bucket, user, pass string) *UpYunMediafunc (upm *UpYunMedia) PostTasks(src, notify, accept string,
tasks []map[string]interface{}) ([]string, error)src 音视频文件 UPYUN 存储路径,notify 回调URL,accept 设置回调格式,可选 json,tasks 任务列表,返回结果为任务 id 列表。
func (upm *UpYunMedia) GetProgress(task_ids string) (*MediaStatusResp, error)task_ids 是多个 task_id 用 , 连接起来。