Skip to content

Commit

Permalink
pkg/object: add copy interface (#3581)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijian-pro committed May 10, 2023
1 parent 55c1aad commit 435fcf7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pkg/object/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ type ObjectStorage interface {
Get(key string, off, limit int64) (io.ReadCloser, error)
// Put data read from a reader to an object specified by key.
Put(key string, in io.Reader) error
// Copy an object from src to dst.
Copy(dst, src string) error
// Delete a object.
Delete(key string) error

Expand Down
4 changes: 4 additions & 0 deletions pkg/object/object_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ func (s DefaultObjectStorage) Head(key string) (Object, error) {
return nil, notSupported
}

func (s DefaultObjectStorage) Copy(dst, src string) error {
return notSupported
}

func (s DefaultObjectStorage) CreateMultipartUpload(key string) (*MultipartUpload, error) {
return nil, notSupported
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/object/object_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ func testStorage(t *testing.T, s ObjectStorage) {
if err := s.Create(); err != nil {
t.Fatalf("err should be nil when creating a bucket with the same name")
}
s = WithPrefix(s, "unit-test/")
prefix := "unit-test/"
s = WithPrefix(s, prefix)
defer func() {
if err := s.Delete("test"); err != nil {
t.Fatalf("delete failed: %s", err)
Expand Down Expand Up @@ -371,6 +372,20 @@ func testStorage(t *testing.T, s ObjectStorage) {
t.Fatalf("storage class should be %s but got %s", sc, o.StorageClass())
}

dstKey := "test-copy"
defer s.Delete(dstKey)
err = s.Copy(fmt.Sprintf("%s%s", prefix, dstKey), fmt.Sprintf("%stest", prefix))
if err != nil && err != notSupported {
t.Fatalf("copy failed: %s", err.Error())
}
if err == nil {
if o, err := s.Head(dstKey); err != nil {
t.Fatalf("check exists failed: %s", err.Error())
} else if sc != "" && o.StorageClass() != sc {
t.Fatalf("storage class should be %s but got %s", sc, o.StorageClass())
}
}

if err := s.Delete("test"); err != nil {
t.Fatalf("delete failed: %s", err)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/object/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ func (p *withPrefix) Put(key string, in io.Reader) error {
return p.os.Put(p.prefix+key, in)
}

func (p *withPrefix) Copy(dst, src string) error {
return p.os.Copy(dst, src)
}

func (p *withPrefix) Delete(key string) error {
return p.os.Delete(p.prefix + key)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/object/sharding.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (s *sharded) Put(key string, body io.Reader) error {
return s.pick(key).Put(key, body)
}

func (s *sharded) Copy(dst, src string) error {
return notSupported
}

func (s *sharded) Delete(key string) error {
return s.pick(key).Delete(key)
}
Expand Down

0 comments on commit 435fcf7

Please sign in to comment.