Skip to content

Commit

Permalink
add bucket inventory xml api
Browse files Browse the repository at this point in the history
  • Loading branch information
taowei.wtw authored and kkuai committed May 25, 2022
1 parent 6468ccd commit 8b2b398
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 4 deletions.
101 changes: 100 additions & 1 deletion oss/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ func (client Client) DeleteBucketQosInfo(bucketName string, options ...Option) e
//
// Set the Bucket inventory.
//
// bucketName tht bucket name.
// bucketName the bucket name.
//
// inventoryConfig the inventory configuration.
//
Expand Down Expand Up @@ -1390,6 +1390,47 @@ func (client Client) SetBucketInventory(bucketName string, inventoryConfig Inven
return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
}

// SetBucketInventoryXml API operation for Object Storage Service
//
// Set the Bucket inventory
//
// bucketName the bucket name.
//
// xmlBody the inventory configuration.
//
// error it's nil if no error, otherwise it's an error.
//
func (client Client) SetBucketInventoryXml(bucketName string, xmlBody string, options ...Option) error {
var inventoryConfig InventoryConfiguration
err := xml.Unmarshal([]byte(xmlBody), &inventoryConfig)
if err != nil {
return err
}

if inventoryConfig.Id == "" {
return fmt.Errorf("inventory id is empty in xml")
}

params := map[string]interface{}{}
params["inventoryId"] = inventoryConfig.Id
params["inventory"] = nil

buffer := new(bytes.Buffer)
buffer.Write([]byte(xmlBody))

contentType := http.DetectContentType(buffer.Bytes())
headers := make(map[string]string)
headers[HTTPHeaderContentType] = contentType

resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
if err != nil {
return err
}

defer resp.Body.Close()
return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
}

// GetBucketInventory API operation for Object Storage Service
//
// Get the Bucket inventory.
Expand Down Expand Up @@ -1418,6 +1459,33 @@ func (client Client) GetBucketInventory(bucketName string, strInventoryId string
return out, err
}

// GetBucketInventoryXml API operation for Object Storage Service
//
// Get the Bucket inventory.
//
// bucketName tht bucket name.
//
// strInventoryId the inventory id.
//
// InventoryConfiguration the inventory configuration.
//
// error it's nil if no error, otherwise it's an error.
//
func (client Client) GetBucketInventoryXml(bucketName string, strInventoryId string, options ...Option) (string, error) {
params := map[string]interface{}{}
params["inventory"] = nil
params["inventoryId"] = strInventoryId

resp, err := client.do("GET", bucketName, params, nil, nil, options...)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
out := string(body)
return out, err
}

// ListBucketInventory API operation for Object Storage Service
//
// List the Bucket inventory.
Expand Down Expand Up @@ -1450,6 +1518,37 @@ func (client Client) ListBucketInventory(bucketName, continuationToken string, o
return out, err
}

// ListBucketInventoryXml API operation for Object Storage Service
//
// List the Bucket inventory.
//
// bucketName tht bucket name.
//
// continuationToken the users token.
//
// ListInventoryConfigurationsResult list all inventory configuration by .
//
// error it's nil if no error, otherwise it's an error.
//
func (client Client) ListBucketInventoryXml(bucketName, continuationToken string, options ...Option) (string, error) {
params := map[string]interface{}{}
params["inventory"] = nil
if continuationToken == "" {
params["continuation-token"] = nil
} else {
params["continuation-token"] = continuationToken
}

resp, err := client.do("GET", bucketName, params, nil, nil, options...)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
out := string(body)
return out, err
}

// DeleteBucketInventory API operation for Object Storage Service.
//
// Delete Bucket inventory information.
Expand Down
90 changes: 90 additions & 0 deletions oss/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3660,6 +3660,96 @@ func (s *OssClientSuite) TestBucketInventoryNegative(c *C) {
c.Assert(err, IsNil)
}

func (s *OssClientSuite) TestBucketInventoryXmlSuccess(c *C) {
client, err := New(endpoint, accessID, accessKey)
c.Assert(err, IsNil)

bucketName := bucketNamePrefix + RandLowStr(5)
err = client.CreateBucket(bucketName)
c.Assert(err, IsNil)

bl := true
invConfig := InventoryConfiguration{
Id: "report1",
IsEnabled: &bl,
Prefix: "filterPrefix/",
OSSBucketDestination: OSSBucketDestination{
Format: "CSV",
AccountId: accountID,
RoleArn: stsARN,
Bucket: "acs:oss:::" + bucketName,
Prefix: "prefix1",
},
Frequency: "Daily",
IncludedObjectVersions: "All",
OptionalFields: OptionalFields{
Field: []string{
"Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
},
},
}

tagBucket := "acs:oss:::" + bucketName

var xmlBody []byte
xmlBody, _ = xml.Marshal(invConfig)

// set enventory
err = client.SetBucketInventoryXml(bucketName, string(xmlBody))
c.Assert(err, IsNil)

// get enventory
xmlGet, err := client.GetBucketInventoryXml(bucketName, "report1")
c.Assert(err, IsNil)
c.Assert(strings.Contains(xmlGet, tagBucket), Equals, true)

// list enventory
xmlList, err := client.ListBucketInventoryXml(bucketName, "", Marker("report1"))
c.Assert(err, IsNil)
c.Assert(strings.Contains(xmlList, tagBucket), Equals, true)

err = client.DeleteBucket(bucketName)
c.Assert(err, IsNil)
}

func (s *OssClientSuite) TestBucketInventoryXmlError(c *C) {
client, err := New(endpoint, accessID, accessKey)
c.Assert(err, IsNil)

bucketName := bucketNamePrefix + RandLowStr(5)
err = client.CreateBucket(bucketName)
c.Assert(err, IsNil)

// not xml format
xmlBody := `
<InventoryConfiguration>
<Id>report1</Id>
<IsEnabled>true</IsEnabled>
`
err = client.SetBucketInventoryXml(bucketName, xmlBody)
c.Assert(err, NotNil)

// no id
xmlBody = `
<InventoryConfiguration>
<IsEnabled>true</IsEnabled>
<Filter>
<Prefix>filterPrefix/</Prefix>
<LastModifyBeginTimeStamp>1637883649</LastModifyBeginTimeStamp>
<LastModifyEndTimeStamp>1638347592</LastModifyEndTimeStamp>
<LowerSizeBound>1024</LowerSizeBound>
<UpperSizeBound>1048576</UpperSizeBound>
<StorageClass>Standard,IA</StorageClass>
</Filter>
</InventoryConfiguration>
`
err = client.SetBucketInventoryXml(bucketName, xmlBody)
c.Assert(err, NotNil)

err = client.DeleteBucket(bucketName)
c.Assert(err, IsNil)
}

func (s *OssClientSuite) TestBucketAsyncTask(c *C) {
client, err := New(endpoint, accessID, accessKey)
c.Assert(err, IsNil)
Expand Down
2 changes: 1 addition & 1 deletion oss/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ const (

DefaultContentSha256 = "UNSIGNED-PAYLOAD" // for v4 signature

Version = "v2.2.3" // Go SDK version
Version = "v2.2.4" // Go SDK version
)

// FrameType
Expand Down
2 changes: 1 addition & 1 deletion oss/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ListCloudBoxResult struct {
MaxKeys int `xml:"MaxKeys"` // The max entry count to return. This information is returned when IsTruncated is true.
IsTruncated bool `xml:"IsTruncated"` // Flag true means there's remaining cloudboxes to return.
NextMarker string `xml:"NextMarker"` // The marker filter for the next list call
Owner string `xml:"Owner"` // The owner information
Owner string `xml:"Owner>DisplayName"` // The owner information
CloudBoxes []CloudBoxProperties `xml:"CloudBoxes>CloudBox"` // The cloudbox list
}

Expand Down
2 changes: 1 addition & 1 deletion oss/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (bucket Bucket) uploadFile(objectKey, filePath string, partSize int64, opti
}
}

event = newProgressEvent(TransferStartedEvent, completedBytes, totalBytes, 0)
event = newProgressEvent(TransferCompletedEvent, completedBytes, totalBytes, 0)
publishProgress(listener, event)

// Complete the multpart upload
Expand Down

0 comments on commit 8b2b398

Please sign in to comment.