Skip to content

Commit

Permalink
[baidu] add ListFolders, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-sawatzky committed Aug 10, 2020
1 parent e352864 commit b4cd8e5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 38 deletions.
84 changes: 46 additions & 38 deletions baidu.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,44 +67,52 @@ func NewBaiDuBOSBackend(bucket string, prefix string, endpoint string) *BaiduBOS

// ListObjects lists all objects in Baidu Cloud BOS bucket, at prefix
func (b BaiduBOSBackend) ListObjects(prefix string) ([]Object, error) {
var objects []Object

prefix = pathutil.Join(b.Prefix, prefix)
listObjectsArgs := &api.ListObjectsArgs{
Prefix: prefix,
Marker: "",
MaxKeys: 1000,
}
for {
lor, err := b.Client.ListObjects(b.Bucket, listObjectsArgs)
if err != nil {
return objects, err
}

for _, obj := range lor.Contents {
path := removePrefixFromObjectPath(prefix, obj.Key)
if objectPathIsInvalid(path) {
continue
}
lastModified, err := time.Parse(time.RFC3339, obj.LastModified)
if err != nil {
continue
}
object := Object{
Path: path,
Content: []byte{},
LastModified: lastModified,
}
objects = append(objects, object)
}
if !lor.IsTruncated {
break
}
listObjectsArgs.Prefix = lor.Prefix
listObjectsArgs.Marker = lor.NextMarker
}

return objects, nil
return getObjects(b, prefix)
}

// ListFolders lists all folders in Baidu Cloud BOS bucket, at prefix
func (b BaiduBOSBackend) ListFolders(prefix string) ([]Object, error) {
return getFolders(b, prefix)
}

func (b BaiduBOSBackend) ListObjects(prefix string) ([]Object, error) {
ch := make(chan(Item))
go func() {
prefix = pathutil.Join(b.Prefix, prefix)
listObjectsArgs := &api.ListObjectsArgs{
Prefix: prefix,
Marker: "",
MaxKeys: 1000,
}
for {
lor, err := b.Client.ListObjects(b.Bucket, listObjectsArgs)
if err != nil {
return nil, err
}

for _, obj := range lor.Contents {
path := removePrefixFromObjectPath(prefix, obj.Key)
lastModified, err := time.Parse(time.RFC3339, obj.LastModified)
if err != nil {
continue
}
ch <- Item{
&Object{
Path: path,
Content: []byte{},
LastModified: lastModified,
}, nil
}
}
if !lor.IsTruncated {
break
}
listObjectsArgs.Prefix = lor.Prefix
listObjectsArgs.Marker = lor.NextMarker
}
close(ch)
}
return ch
}

// GetObject retrieves an object from Baidu Cloud BOS bucket, at prefix
Expand Down
8 changes: 8 additions & 0 deletions baidu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ func (suite *BaiduTestSuite) TestListObjects() {
suite.Equal(len(objs), bosTestCount, "able to list objects")
}

func (suite *BaiduTestSuite) TestListFolders() {
_, err := suite.BrokenBaiduBOSBackend.ListFolders("")
suite.NotNil(err, "cannot list folders with bad bucket")

_, err := suite.NoPrefixBaiduBOSBackend.ListFolders("")
suite.Nil(err, "can list folders with good bucket, no prefix")
}

func (suite *BaiduTestSuite) TestGetObject() {
_, err := suite.BrokenBaiduBOSBackend.GetObject("this-file-cannot-possibly-exist.tgz")
suite.NotNil(err, "cannot get objects with bad bucket")
Expand Down

0 comments on commit b4cd8e5

Please sign in to comment.