Skip to content

Commit

Permalink
add post bucket inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoliang committed Mar 6, 2023
1 parent 336fbea commit 492db1b
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
20 changes: 20 additions & 0 deletions bucket_inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ type BucketPutInventoryOptions struct {
Destination *BucketInventoryDestination `xml:"Destination>COSBucketDestination"`
}

type BucketPostInventoryOptions struct {
XMLName xml.Name `xml:"InventoryConfiguration"`
ID string `xml:"Id"`
IncludedObjectVersions string `xml:"IncludedObjectVersions"`
Filter *BucketInventoryFilter `xml:"Filter,omitempty"`
OptionalFields *BucketInventoryOptionalFields `xml:"OptionalFields,omitempty"`
Destination *BucketInventoryDestination `xml:"Destination>COSBucketDestination"`
}

// ListBucketInventoryConfigResult result of ListBucketInventoryConfiguration
type ListBucketInventoryConfigResult struct {
XMLName xml.Name `xml:"ListInventoryConfigurationResult"`
Expand Down Expand Up @@ -130,3 +139,14 @@ func (s *BucketService) ListInventoryConfigurations(ctx context.Context, token s
return &res, resp, err

}
func (s *BucketService) PostInventory(ctx context.Context, id string, opt *BucketPostInventoryOptions) (*Response, error) {
u := fmt.Sprintf("/?inventory&id=%s", id)
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: u,
method: http.MethodPost,
body: opt,
}
resp, err := s.client.send(ctx, &sendOpt)
return resp, err
}
50 changes: 50 additions & 0 deletions bucket_inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,53 @@ func TestBucketService_DeleteInventory(t *testing.T) {
t.Fatalf("Bucket.DeleteInventory returned error: %v", err)
}
}

func TestBucketService_PostInventory(t *testing.T) {
setup()
defer teardown()
opt := &BucketPostInventoryOptions{
XMLName: xml.Name{Local: "InventoryConfiguration"},
ID: "list1",
IncludedObjectVersions: "All",
Filter: &BucketInventoryFilter{"myPrefix", nil},
Destination: &BucketInventoryDestination{
Bucket: "qcs::cos:ap-guangzhou::examplebucket-1250000000",
AccountId: "100000000001",
Prefix: "list1",
Format: "CSV",
Encryption: &BucketInventoryEncryption{},
},
OptionalFields: &BucketInventoryOptionalFields{
BucketInventoryFields: []string{
"Size",
"LastModifiedDate",
"ETag",
"StorageClass",
"IsMultipartUploaded",
"ReplicationStatus",
},
},
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
vs := values{
"inventory": "",
"id": "list1",
}
testFormValues(t, r, vs)

body := &BucketPostInventoryOptions{}
xml.NewDecoder(r.Body).Decode(body)
want := opt
if !reflect.DeepEqual(want, body) {
t.Fatalf("Bucket.PostInventory request\n body: %+v\n, want %+v\n", body, want)
}
})

_, err := client.Bucket.PostInventory(context.Background(), "list1", opt)
if err != nil {
t.Fatalf("Bucket.PostInventory failed, error: %v", err)
}
}


70 changes: 70 additions & 0 deletions example/bucket/inventory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"context"
"fmt"
"github.com/tencentyun/cos-go-sdk-v5"
"github.com/tencentyun/cos-go-sdk-v5/debug"
"net/http"
"net/url"
"os"
)

func log_status(err error) {
if err == nil {
return
}
if cos.IsNotFoundError(err) {
// WARN
fmt.Println("Resource is not existed")
} else if e, ok := cos.IsCOSError(err); ok {
fmt.Printf("Code: %v\n", e.Code)
fmt.Printf("Message: %v\n", e.Message)
fmt.Printf("Resource: %v\n", e.Resource)
fmt.Printf("RequestId: %v\n", e.RequestID)
// ERROR
} else {
fmt.Println(err)
// ERROR
}
}

func main() {
// 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
// 替换为用户的 region,存储桶region可以在COS控制台“存储桶概览”查看 https://console.cloud.tencent.com/ ,关于地域的详情见 https://cloud.tencent.com/document/product/436/6224 。
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
client := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
// 通过环境变量获取密钥
// 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
SecretID: os.Getenv("COS_SECRETID"),
// 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: true,
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
},
},
})
opt := &cos.BucketPostInventoryOptions{
ID: "test_id",
IncludedObjectVersions: "All",
Filter: &cos.BucketInventoryFilter{
Prefix: "test",
},
OptionalFields: &cos.BucketInventoryOptionalFields{
BucketInventoryFields: []string{
"Size", "LastModifiedDate",
},
},
Destination: &cos.BucketInventoryDestination{
Bucket: "qcs::cos:ap-guangzhou::test-1259654469",
Format: "CSV",
},
}
_, err := client.Bucket.PostInventory(context.Background(), "test_id", opt)
log_status(err)
}

0 comments on commit 492db1b

Please sign in to comment.