Skip to content

Commit

Permalink
add func: ChangeMeta & ChangeMimeAndMeta & stat return meta
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSen-qn committed Aug 29, 2023
1 parent 3910f54 commit 1b45f71
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 10 deletions.
57 changes: 56 additions & 1 deletion storage/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
"encoding/base64"
"errors"
"fmt"
"github.com/qiniu/go-sdk/v7/internal/clientv2"
"net/url"
"strconv"
"strings"
"time"

"github.com/qiniu/go-sdk/v7/internal/clientv2"

"github.com/qiniu/go-sdk/v7/auth"
clientv1 "github.com/qiniu/go-sdk/v7/client"
)
Expand Down Expand Up @@ -76,6 +77,11 @@ type FileInfo struct {
*/
EndUser string `json:"endUser"`

/**
* 自定义元数据,含 meta_key 参数 及对应 metaValue 值;仅当自定义元数据后返回该字段。
*/
MetaData map[string]string `json:"x-qn-meta"`

/**
* 文件过期删除日期,int64 类型,Unix 时间戳格式,具体文件过期日期计算参考 生命周期管理。
* 文件在设置过期时间后才会返回该字段(通过生命周期规则设置文件过期时间,仅对该功能发布后满足规则条件新上传文件返回该字段;
Expand Down Expand Up @@ -490,6 +496,23 @@ func (m *BucketManager) ChangeMime(bucket, key, newMime string) (err error) {
return
}

// ChangeMeta 用来修改或者增加 metas,metas 只包含需要修改的,未涉及的保存不变
func (m *BucketManager) ChangeMeta(bucket, key string, metas map[string]string) (err error) {
return m.ChangeMimeAndMeta(bucket, key, "", metas)
}

// ChangeMimeAndMeta 用来更新文件的 MimeType 以及修改或者增加 metas,metas 只包含需要修改的
func (m *BucketManager) ChangeMimeAndMeta(bucket, key, newMime string, metas map[string]string) (err error) {
reqHost, reqErr := m.RsReqHost(bucket)
if reqErr != nil {
err = reqErr
return
}
reqURL := fmt.Sprintf("%s%s", reqHost, URIChangeMimeAndMeta(bucket, key, newMime, metas))
err = m.Client.CredentialedCall(context.Background(), m.Mac, auth.TokenQiniu, nil, "POST", reqURL, nil)
return
}

// ChangeType 用来更新文件的存储类型,0 表示普通存储,1 表示低频存储,2 表示归档存储,3 表示深度归档存储
func (m *BucketManager) ChangeType(bucket, key string, fileType int) (err error) {
reqHost, reqErr := m.RsReqHost(bucket)
Expand Down Expand Up @@ -877,6 +900,38 @@ func URIChangeMime(bucket, key, newMime string) string {
base64.URLEncoding.EncodeToString([]byte(newMime)))
}

// URIChangeMeta
//
// @Description: 构建 chgm 接口的请求命令,修改 meta
// @param bucket 空间
// @param key 文件保存的 key
// @param metas 需要修改的 metas,只包含需要更改的 metas,可增加
// @return string URI
func URIChangeMeta(bucket, key string, changeMetas map[string]string) string {
return URIChangeMimeAndMeta(bucket, key, "", changeMetas)
}

// URIChangeMimeAndMeta
//
// @Description: 构建 chgm 接口的请求命令
// @param bucket 空间
// @param key 文件保存的 key
// @param newMime 新的 mime
// @param metas 需要修改的 metas,只包含需要更改的 metas,可增加
// @return string URI
func URIChangeMimeAndMeta(bucket, key, newMime string, metas map[string]string) string {
uri := fmt.Sprintf("/chgm/%s", EncodedEntry(bucket, key))
if len(newMime) > 0 {
uri = fmt.Sprintf("%s/mime/%s", uri, base64.URLEncoding.EncodeToString([]byte(newMime)))
}
if len(metas) > 0 {
for k, v := range metas {
uri = fmt.Sprintf("%s/%s/%s", uri, k, base64.URLEncoding.EncodeToString([]byte(v)))
}
}
return uri
}

// URIChangeType 构建 chtype 接口的请求命令
func URIChangeType(bucket, key string, fileType int) string {
return fmt.Sprintf("/chtype/%s/type/%d", EncodedEntry(bucket, key), fileType)
Expand Down
100 changes: 100 additions & 0 deletions storage/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,98 @@ func TestStatWithOption(t *testing.T) {
}
}

func TestStatWithMeta(t *testing.T) {

key := "meta_test_" + time.Now().String()

data := make([]byte, 1024*1024*5)
err := putDataByResumableV2(key, data)
if err != nil {
t.Logf("TestStatWithMeta test upload data error, %s", err)
return
}

info, err := bucketManager.Stat(testBucket, key)
if err != nil {
t.Logf("TestStatWithMeta() Stat error, %s", err)
t.Fail()
}
if len(info.MetaData) == 0 {
t.Log("TestStatWithMeta() MetaData is empty")
t.Fail()
}

// 同时修改 mime 和 meta
err = bucketManager.ChangeMimeAndMeta(testBucket, key, "application/abc", map[string]string{
"x-qn-meta-b": "x-qn-meta-bb-value",
"x-qn-meta-c": "x-qn-meta-c-value",
})
if err != nil {
t.Logf("TestStatWithMeta() ChangeMimeAndMeta error, %s", err)
t.Fail()
}

info, err = bucketManager.Stat(testBucket, key)
if err != nil {
t.Logf("TestStatWithMeta() Stat 2 error, %s", err)
t.Fail()
}

if info.MimeType != "application/abc" {
t.Log("TestStatWithMeta() MimeType c is error")
t.Fail()
}

if len(info.MetaData) == 0 {
t.Log("TestStatWithMeta() MetaData 2 is empty")
t.Fail()
}

if info.MetaData["b"] != "x-qn-meta-bb-value" {
t.Log("TestStatWithMeta() MetaData b is empty")
t.Fail()
}

if info.MetaData["c"] != "x-qn-meta-c-value" {
t.Log("TestStatWithMeta() MetaData c is empty")
t.Fail()
}

// 只修改 meta
err = bucketManager.ChangeMeta(testBucket, key, map[string]string{
"x-qn-meta-c": "x-qn-meta-cc-value",
})
if err != nil {
t.Logf("TestStatWithMeta() ChangeMimeAndMeta error, %s", err)
t.Fail()
}
info, err = bucketManager.Stat(testBucket, key)
if err != nil {
t.Logf("TestStatWithMeta() Stat 2 error, %s", err)
t.Fail()
}

if info.MimeType != "application/abc" {
t.Log("TestStatWithMeta() MimeType c is error")
t.Fail()
}

if len(info.MetaData) == 0 {
t.Log("TestStatWithMeta() MetaData 2 is empty")
t.Fail()
}

if info.MetaData["b"] != "x-qn-meta-bb-value" {
t.Log("TestStatWithMeta() MetaData b is empty")
t.Fail()
}

if info.MetaData["c"] != "x-qn-meta-cc-value" {
t.Log("TestStatWithMeta() MetaData c is empty")
t.Fail()
}
}

func putDataByResumableV2(key string, data []byte) (err error) {
var putRet PutRet
putPolicy := PutPolicy{
Expand All @@ -277,6 +369,14 @@ func putDataByResumableV2(key string, data []byte) (err error) {
partSize := int64(1024 * 1024)
err = resumeUploaderV2.Put(context.Background(), &putRet, upToken, key, bytes.NewReader(data), int64(len(data)), &RputV2Extra{
PartSize: partSize,
Metadata: map[string]string{
"x-qn-meta-a": "x-qn-meta-a-value",
"x-qn-meta-b": "x-qn-meta-b-value",
},
CustomVars: map[string]string{
"x:var-a": "x:var-a-value",
"x:var-b": "x:var-b-value",
},
})
return
}
Expand Down
7 changes: 5 additions & 2 deletions storage/form_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"github.com/qiniu/go-sdk/v7/internal/hostprovider"
"hash"
"hash/crc32"
"io"
Expand All @@ -18,12 +17,16 @@ import (
"strings"
"time"

"github.com/qiniu/go-sdk/v7/internal/hostprovider"

"github.com/qiniu/go-sdk/v7/client"
)

// PutExtra 为表单上传的额外可选项
type PutExtra struct {
// 可选,用户自定义参数,必须以 "x:" 开头。若不以x:开头,则忽略。
// 可选。
// 用户自定义参数:key 以"x:"开头,而且 value 不能为空 eg: key为x:qqq
// 自定义 meta:key 以"x-qn-meta-"开头,而且 value 不能为空 eg: key为x-qn-meta-aaa
Params map[string]string

UpHost string
Expand Down
11 changes: 8 additions & 3 deletions storage/resume_uploader_apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package storage
import (
"context"
"encoding/base64"
"github.com/qiniu/go-sdk/v7/internal/hostprovider"
"io"
"net/http"
"strconv"
"strings"
"time"

"github.com/qiniu/go-sdk/v7/internal/hostprovider"

"github.com/qiniu/go-sdk/v7/client"
"github.com/qiniu/go-sdk/v7/conf"
)
Expand Down Expand Up @@ -46,8 +47,12 @@ func (p *resumeUploaderAPIs) bput(ctx context.Context, upToken string, ret *Blkp

// RputExtra 表示分片上传额外可以指定的参数
type RputExtra struct {
Recorder Recorder // 可选。上传进度记录
Params map[string]string // 可选。用户自定义参数,以"x:"开头,而且值不能为空,否则忽略
Recorder Recorder // 可选。上传进度记录

// 可选。
// 用户自定义参数:key 以"x:"开头,而且 value 不能为空 eg: key为x:qqq
// 自定义 meta:key 以"x-qn-meta-"开头,而且 value 不能为空 eg: key为x-qn-meta-aaa
Params map[string]string
UpHost string
MimeType string // 可选。
ChunkSize int // 可选。每次上传的Chunk大小
Expand Down
9 changes: 5 additions & 4 deletions storage/upload_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"encoding/json"
"errors"
"github.com/qiniu/go-sdk/v7/client"
"io"
"strings"
"sync"
"time"

"github.com/qiniu/go-sdk/v7/client"
)

type UploadResumeVersion = int
Expand All @@ -32,9 +33,9 @@ func (config *UploadConfig) init() {
}

type UploadExtra struct {
// 【可选】参数,
// 用户自定义参数,必须以 "x:" 开头。若不以 "x:" 开头,则忽略。
// meta-data 参数,必须以 "x-qn-meta-" 开头。若不以 "x-qn-meta-" 开头,则忽略。
// 可选。
// 用户自定义参数:key 以"x:"开头,而且 value 不能为空 eg: key为x:qqq
// 自定义 meta:key 以"x-qn-meta-"开头,而且 value 不能为空 eg: key为x-qn-meta-aaa
Params map[string]string

// 【可选】尝试次数
Expand Down

0 comments on commit 1b45f71

Please sign in to comment.