Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

serve s3: fix CopyObject of a missing object not returning NoSuchKey

Nick Craig-Woodce5351c52e94
diff --git a/cmd/serve/s3/backend.go b/cmd/serve/s3/backend.go
index e1c23e3bc..d780c6527 100644
--- a/cmd/serve/s3/backend.go
+++ b/cmd/serve/s3/backend.go
@@ -561,6 +561,10 @@ func (b *s3Backend) CopyObject(ctx context.Context, srcBucket, srcKey, dstBucket
if err != nil {
return result, err
}
+ cStat, err := _vfs.Stat(fp)
+ if err != nil || !cStat.IsFile() {
+ return result, gofakes3.KeyNotFound(srcKey)
+ }
if srcBucket == dstBucket && srcKey == dstKey {
b.meta.Store(fp, meta)
@@ -580,11 +584,6 @@ func (b *s3Backend) CopyObject(ctx context.Context, srcBucket, srcKey, dstBucket
return result, _vfs.Chtimes(fp, ti, ti)
}
- cStat, err := _vfs.Stat(fp)
- if err != nil {
- return
- }
-
c, err := b.GetObject(ctx, srcBucket, srcKey, nil)
if err != nil {
return
diff --git a/cmd/serve/s3/backend_test.go b/cmd/serve/s3/backend_test.go
index 679506a26..6f3d3dd23 100644
--- a/cmd/serve/s3/backend_test.go
+++ b/cmd/serve/s3/backend_test.go
@@ -221,3 +221,17 @@ func TestEtagHashAuto(t *testing.T) {
assert.Equal(t, want[:], obj.Hash)
}
}
+
+// TestCopyObjectMissingSource checks that copying an object which
+// doesn't exist fails with NoSuchKey, including a copy onto itself to
+// replace its metadata.
+func TestCopyObjectMissingSource(t *testing.T) {
+ b, _ := newTestBackend(t)
+ ctx := context.Background()
+ for _, dstKey := range []string{"missing.txt", "other.txt"} {
+ _, err := b.CopyObject(ctx, "bucket", "missing.txt", "bucket", dstKey, map[string]string{"X-Amz-Meta-Colour": "red"})
+ assert.True(t, gofakes3.HasErrorCode(err, gofakes3.ErrNoSuchKey), "copy to %q: %v", dstKey, err)
+ }
+ _, err := b.HeadObject(ctx, "bucket", "other.txt")
+ assert.True(t, gofakes3.HasErrorCode(err, gofakes3.ErrNoSuchKey))
+}