添加七牛cdn上传功能

This commit is contained in:
deepzz0
2016-12-18 18:00:11 +08:00
parent 7305192ca9
commit 5064894ab5
14 changed files with 223 additions and 184 deletions

View File

@@ -7,11 +7,16 @@ import (
"path/filepath"
"github.com/eiblog/eiblog/setting"
"qiniupkg.com/api.v7/conf"
"qiniupkg.com/api.v7/kodo"
"qiniupkg.com/api.v7/kodocli"
)
var qiniu_cfg = &kodo.Config{
AccessKey: setting.Conf.Kodo.AccessKey,
SecretKey: setting.Conf.Kodo.SecretKey,
Scheme: "https",
}
type bucket struct {
name string
domain string
@@ -33,15 +38,13 @@ func onProgress(fsize, uploaded int64) {
}
}
func Upload(name string, size int64, data io.Reader) (string, error) {
func FileUpload(name string, size int64, data io.Reader) (string, error) {
if setting.Conf.Kodo.AccessKey == "" || setting.Conf.Kodo.SecretKey == "" {
return "", errors.New("qiniu config error")
}
conf.ACCESS_KEY = setting.Conf.Kodo.AccessKey
conf.SECRET_KEY = setting.Conf.Kodo.SecretKey
// 创建一个client
c := kodo.New(0, nil)
c := kodo.New(0, qiniu_cfg)
// 设置上传的策略
policy := &kodo.PutPolicy{
@@ -56,20 +59,8 @@ func Upload(name string, size int64, data io.Reader) (string, error) {
zone := 0
uploader := kodocli.NewUploader(zone, nil)
ext := filepath.Ext(name)
var key string
switch ext {
case ".bmp", ".png", ".jpg", ".gif", ".ico":
key = "blog/img/" + name
case ".mov", ".mp4":
key = "blog/video/" + name
case ".go", ".js", ".css", ".cpp", ".php", ".rb", ".java", ".py", ".sql", ".lua", ".html", ".sh", ".xml", ".cs":
key = "blog/code/" + name
case ".txt", ".md", ".ini", ".yaml", ".yml", ".doc", ".ppt", ".pdf":
key = "blog/document/" + name
case ".zip", ".rar", ".tar", ".gz":
key = "blog/archive/" + name
default:
key := getKey(name)
if key == "" {
return "", errors.New("不支持的文件类型")
}
@@ -83,3 +74,40 @@ func Upload(name string, size int64, data io.Reader) (string, error) {
url := kodo.MakeBaseUrl(setting.Conf.Kodo.Domain, ret.Key)
return url, nil
}
func FileDelete(name string) error {
// new一个Bucket管理对象
c := kodo.New(0, qiniu_cfg)
p := c.Bucket(setting.Conf.Kodo.Name)
key := getKey(name)
if key == "" {
return errors.New("不支持的文件类型")
}
// 调用Delete方法删除文件
err := p.Delete(nil, key)
// 打印返回值以及出错信息
if err != nil {
return err
}
return nil
}
func getKey(name string) string {
ext := filepath.Ext(name)
var key string
switch ext {
case ".bmp", ".png", ".jpg", ".gif", ".ico":
key = "blog/img/" + name
case ".mov", ".mp4":
key = "blog/video/" + name
case ".go", ".js", ".css", ".cpp", ".php", ".rb", ".java", ".py", ".sql", ".lua", ".html", ".sh", ".xml", ".cs":
key = "blog/code/" + name
case ".txt", ".md", ".ini", ".yaml", ".yml", ".doc", ".ppt", ".pdf":
key = "blog/document/" + name
case ".zip", ".rar", ".tar", ".gz":
key = "blog/archive/" + name
}
return key
}