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) error
func (u *UpYun) Usage() (int64, error)
func (u *UpYun) Mkdir(key string) error
func (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) Get(key string, value io.Writer) (int, error)
此方法返回文件大小
func (u *UpYun) Delete(key string) error
type 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, recursive bool) chan *FileInfo
key
必须为目录。对于目录下有大量文件的,建议使用 GetLargeList
。
func (u *UpYun) Purge(urls []string) (string, error)
type UpYunForm struct {
Secret string // 表单密钥
Bucket string // 空间名(即服务名称)
}
func NewUpYunForm(bucket, key string) *UpYunForm
type 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) error
func (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) *UpYunMultiPart
func (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) *UpYunMedia
func (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
用 ,
连接起来。