Skip to content

Commit

Permalink
add put object with tagging (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
cubatic45 authored Feb 27, 2024
1 parent 2a0551a commit b61537c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cloud/examples/object_storage/object_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func object_storage_examples(bucketName string, option cloud.Option) {
}

files := map[string]*object_storage.PutObjectInput{
"abc/a.txt": {Body: []byte("abc"), ContentType: "application/json"},
"abc/a.txt": {Body: []byte("abc"), ContentType: "application/json", Tagging: "Key1=Value1&Key2=Value2"},
"abc/ab.txt": {Body: []byte("abcdefg"), ContentType: "text/html"},
"bc/b.txt": {Body: []byte("xyz")},
}
Expand Down
14 changes: 11 additions & 3 deletions cloud/object_storage/alicloud_object_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"context"
"errors"
"fmt"
"io"
"time"

"github.com/alibabacloud-go/tea/tea"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/byte-power/gorich/cloud"
"github.com/byte-power/gorich/utils"
"io"
"time"
)

var ossClientMap = make(map[string]*oss.Client)
Expand Down Expand Up @@ -264,7 +265,14 @@ func (service *AliCloudObjectStorageService) PutObject(ctx context.Context, key
if err != nil {
return err
}
return bucket.PutObject(key, bytes.NewReader(input.Body), oss.ContentType(input.ContentType))
var opts []oss.Option
if input.ContentType != "" {
opts = append(opts, oss.ContentType(input.ContentType))
}
if input.Tagging != "" {
opts = append(opts, oss.SetHeader(oss.HTTPHeaderOssTagging, input.Tagging))
}
return bucket.PutObject(key, bytes.NewReader(input.Body), opts...)
}

func (service *AliCloudObjectStorageService) DeleteObject(ctx context.Context, key string) error {
Expand Down
7 changes: 5 additions & 2 deletions cloud/object_storage/aws_object_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"errors"
"io/ioutil"
"io"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -110,7 +110,7 @@ func (service *AWSObjectStorageService) GetObject(ctx context.Context, key strin
}
return Object{}, err
}
bs, err := ioutil.ReadAll(resp.Body)
bs, err := io.ReadAll(resp.Body)
if err != nil {
return Object{}, err
}
Expand Down Expand Up @@ -139,6 +139,9 @@ func (service *AWSObjectStorageService) PutObject(ctx context.Context, key strin
Key: &key,
Body: bytes.NewReader(input.Body),
}
if input.Tagging != "" {
opts.Tagging = aws.String(input.Tagging)
}
if input.ContentType != "" {
opts.ContentType = &input.ContentType
}
Expand Down
3 changes: 2 additions & 1 deletion cloud/object_storage/object_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ type ObjectStorageService interface {
DeleteObject(ctx context.Context, key string) error
DeleteObjects(ctx context.Context, keys ...string) error
GetSignedURL(key string, duration time.Duration) (string, error)
//GetSignedURLForExistedKey generates signed url if key exists. If key does not exist, return error
// GetSignedURLForExistedKey generates signed url if key exists. If key does not exist, return error
GetSignedURLForExistedKey(ctx context.Context, key string, duration time.Duration) (string, error)
}

type PutObjectInput struct {
Body []byte
ContentType string
Tagging string
}

type Object struct {
Expand Down
18 changes: 9 additions & 9 deletions cloud/object_storage/tencentcloud_object_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -55,7 +55,8 @@ func GetTencentCloudObjectService(bucketName string, option cloud.Option) (Objec
Transport: &cos.AuthorizationTransport{
SecretID: option.GetSecretID(),
SecretKey: option.GetSecretKey(),
}})
},
})
return &TencentCloudObjectStorageService{client: client}, nil
}

Expand Down Expand Up @@ -136,7 +137,7 @@ func (service *TencentCloudObjectStorageService) GetObject(ctx context.Context,
}
return Object{}, err
}
bs, err := ioutil.ReadAll(resp.Body)
bs, err := io.ReadAll(resp.Body)
if err != nil {
return Object{}, err
}
Expand Down Expand Up @@ -198,13 +199,12 @@ func (service *TencentCloudObjectStorageService) PutObject(ctx context.Context,
if input == nil {
return errors.New("parameter input is nil")
}
var opts *cos.ObjectPutOptions
opts := &cos.ObjectPutOptions{ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{}}
if input.ContentType != "" {
opts = &cos.ObjectPutOptions{
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
ContentType: input.ContentType,
},
}
opts.ContentType = input.ContentType
}
if input.Tagging != "" {
opts.XOptionHeader = &http.Header{"x-cos-tagging": []string{input.Tagging}}
}
_, err := service.client.Object.Put(ctx, key, bytes.NewReader(input.Body), opts)
return err
Expand Down

0 comments on commit b61537c

Please sign in to comment.