Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/object: add copy interface #3581

Merged
merged 2 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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