跳至主要內容

七牛云

LiCheng小于 1 分钟

七牛云

七牛云存储对接

  • go
  • 2022/11/26

后端go具体代码

import (
	"github.com/qiniu/go-sdk/v7/auth/qbox"
	"github.com/qiniu/go-sdk/v7/storage"
)


//七牛云密钥 - 密钥查看: https://portal.qiniu.com/user/key
const accessKey = "your accessKey"  
const secretKey = "your secretKey"
//你的资源资源桶名称 - 桶查看: https://portal.qiniu.com/kodo/bucket
const bucket ="my-video-test"
//获取token
func getToken()  {
	putPolicy := storage.PutPolicy{
		Scope: bucket,
	}
	mac := qbox.NewMac(accessKey, secretKey)
	upToken := putPolicy.UploadToken(mac)
	log.Println(upToken)
}

// DeleteFile 删除文件
func DeleteFile(key string) error {
	mac := qbox.NewMac(accessKey, secretKey)
	cfg := storage.Config{
		UseHTTPS: false,
		Zone:     &storage.ZoneHuanan,
	}
	bucketManager := storage.NewBucketManager(mac, &cfg)
	err := bucketManager.Delete(bucket, key)
	return err
}

// DeleteFileByUrl 根据url删除
func DeleteFileByUrl(url string) error {
	key := ParseKey(url)
	return DeleteFile(key)
}


// ParseKey 解析 url中的key
func ParseKey(url string) string {
	// 从url中解析出key
	index := strings.LastIndex(url, "/")
	key := url[index+1:]

	// 去除参数
	index = strings.Index(key, "?")
	if index != -1 {
		key = key[:index]
	}
	return key
}

前端Vue

  • vue2
 <template>
    <el-upload
        :limit="1"
        ref="upload"
        action="https://upload-z2.qiniup.com"
        :data="{token:fileAction.token}"
        :on-success="onUploadOk"
        :auto-upload="false">
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
    </el-upload>
 </template>
 <script>
export default {
  data() {
    return {
      fileAction:{
         token : "eV3kvzxi6iRIBq7U-7olByD0bQIZfonL6SqBXB2Q:9mf6GZdDd0aXH1v8EWhvNlFfhLk=:eyJzY29wZSI6Im15LXZpZGVvLXRlc3QiLCJkZWFkbGluZSI6MTY2OTQzNDU3MX0="
      }
    }
  },
  methods: {
    onUploadOk(r,f,fs){
      console.log("上传结果: "+r.key)
    },
    submitUpload() {
      this.$refs.upload.submit();
    },
  },
}
</script>