Skip to content

Commit b61537c

Browse files
authored
add put object with tagging (#17)
1 parent 2a0551a commit b61537c

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

cloud/examples/object_storage/object_storage.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func object_storage_examples(bucketName string, option cloud.Option) {
4444
}
4545

4646
files := map[string]*object_storage.PutObjectInput{
47-
"abc/a.txt": {Body: []byte("abc"), ContentType: "application/json"},
47+
"abc/a.txt": {Body: []byte("abc"), ContentType: "application/json", Tagging: "Key1=Value1&Key2=Value2"},
4848
"abc/ab.txt": {Body: []byte("abcdefg"), ContentType: "text/html"},
4949
"bc/b.txt": {Body: []byte("xyz")},
5050
}

cloud/object_storage/alicloud_object_storage.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"context"
66
"errors"
77
"fmt"
8+
"io"
9+
"time"
10+
811
"github.com/alibabacloud-go/tea/tea"
912
"github.com/aliyun/aliyun-oss-go-sdk/oss"
1013
"github.com/byte-power/gorich/cloud"
1114
"github.com/byte-power/gorich/utils"
12-
"io"
13-
"time"
1415
)
1516

1617
var ossClientMap = make(map[string]*oss.Client)
@@ -264,7 +265,14 @@ func (service *AliCloudObjectStorageService) PutObject(ctx context.Context, key
264265
if err != nil {
265266
return err
266267
}
267-
return bucket.PutObject(key, bytes.NewReader(input.Body), oss.ContentType(input.ContentType))
268+
var opts []oss.Option
269+
if input.ContentType != "" {
270+
opts = append(opts, oss.ContentType(input.ContentType))
271+
}
272+
if input.Tagging != "" {
273+
opts = append(opts, oss.SetHeader(oss.HTTPHeaderOssTagging, input.Tagging))
274+
}
275+
return bucket.PutObject(key, bytes.NewReader(input.Body), opts...)
268276
}
269277

270278
func (service *AliCloudObjectStorageService) DeleteObject(ctx context.Context, key string) error {

cloud/object_storage/aws_object_storage.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"context"
66
"errors"
7-
"io/ioutil"
7+
"io"
88
"time"
99

1010
"github.com/aws/aws-sdk-go/aws"
@@ -110,7 +110,7 @@ func (service *AWSObjectStorageService) GetObject(ctx context.Context, key strin
110110
}
111111
return Object{}, err
112112
}
113-
bs, err := ioutil.ReadAll(resp.Body)
113+
bs, err := io.ReadAll(resp.Body)
114114
if err != nil {
115115
return Object{}, err
116116
}
@@ -139,6 +139,9 @@ func (service *AWSObjectStorageService) PutObject(ctx context.Context, key strin
139139
Key: &key,
140140
Body: bytes.NewReader(input.Body),
141141
}
142+
if input.Tagging != "" {
143+
opts.Tagging = aws.String(input.Tagging)
144+
}
142145
if input.ContentType != "" {
143146
opts.ContentType = &input.ContentType
144147
}

cloud/object_storage/object_storage.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ type ObjectStorageService interface {
2222
DeleteObject(ctx context.Context, key string) error
2323
DeleteObjects(ctx context.Context, keys ...string) error
2424
GetSignedURL(key string, duration time.Duration) (string, error)
25-
//GetSignedURLForExistedKey generates signed url if key exists. If key does not exist, return error
25+
// GetSignedURLForExistedKey generates signed url if key exists. If key does not exist, return error
2626
GetSignedURLForExistedKey(ctx context.Context, key string, duration time.Duration) (string, error)
2727
}
2828

2929
type PutObjectInput struct {
3030
Body []byte
3131
ContentType string
32+
Tagging string
3233
}
3334

3435
type Object struct {

cloud/object_storage/tencentcloud_object_storage.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"context"
66
"errors"
77
"fmt"
8-
"io/ioutil"
8+
"io"
99
"net/http"
1010
"net/url"
1111
"time"
@@ -55,7 +55,8 @@ func GetTencentCloudObjectService(bucketName string, option cloud.Option) (Objec
5555
Transport: &cos.AuthorizationTransport{
5656
SecretID: option.GetSecretID(),
5757
SecretKey: option.GetSecretKey(),
58-
}})
58+
},
59+
})
5960
return &TencentCloudObjectStorageService{client: client}, nil
6061
}
6162

@@ -136,7 +137,7 @@ func (service *TencentCloudObjectStorageService) GetObject(ctx context.Context,
136137
}
137138
return Object{}, err
138139
}
139-
bs, err := ioutil.ReadAll(resp.Body)
140+
bs, err := io.ReadAll(resp.Body)
140141
if err != nil {
141142
return Object{}, err
142143
}
@@ -198,13 +199,12 @@ func (service *TencentCloudObjectStorageService) PutObject(ctx context.Context,
198199
if input == nil {
199200
return errors.New("parameter input is nil")
200201
}
201-
var opts *cos.ObjectPutOptions
202+
opts := &cos.ObjectPutOptions{ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{}}
202203
if input.ContentType != "" {
203-
opts = &cos.ObjectPutOptions{
204-
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
205-
ContentType: input.ContentType,
206-
},
207-
}
204+
opts.ContentType = input.ContentType
205+
}
206+
if input.Tagging != "" {
207+
opts.XOptionHeader = &http.Header{"x-cos-tagging": []string{input.Tagging}}
208208
}
209209
_, err := service.client.Object.Put(ctx, key, bytes.NewReader(input.Body), opts)
210210
return err

0 commit comments

Comments
 (0)