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: report the object's modtime as Last-Modified after a PUT - fixes #10128

kevin932781e7372f6fbe
diff --git a/cmd/serve/s3/backend.go b/cmd/serve/s3/backend.go
index ffa18deaa..731195900 100644
--- a/cmd/serve/s3/backend.go
+++ b/cmd/serve/s3/backend.go
@@ -168,7 +168,12 @@ func (t *tenant) getMeta(ctx context.Context, fp string, node vfs.Node) (meta ma
// If meta has a modification time in "X-Amz-Meta-Mtime" or "mtime" the
// object's modification time is set to it, returning any error, and
// both are set to it in meta.
+//
+// Any "Last-Modified" in meta is dropped: gofakes3 sets it to the time
+// of the request, and caching it would make HeadObject and GetObject
+// report that instead of the object's modification time.
func (t *tenant) storeMeta(_vfs *vfs.VFS, fp string, meta map[string]string) (err error) {
+ delete(meta, "Last-Modified")
for _, key := range []string{"X-Amz-Meta-Mtime", "mtime"} {
val, ok := meta[key]
if !ok {
diff --git a/cmd/serve/s3/put_test.go b/cmd/serve/s3/put_test.go
index 64d829807..6f6fc08c3 100644
--- a/cmd/serve/s3/put_test.go
+++ b/cmd/serve/s3/put_test.go
@@ -176,6 +176,34 @@ func TestPutObjectMtime(t *testing.T) {
}
}
+// TestPutObjectLastModified checks that HeadObject and GetObject report
+// the object's modification time as Last-Modified straight after a PUT.
+// gofakes3 adds the time of the upload to the metadata it passes in, and
+// that must not be cached in place of the object's own time.
+func TestPutObjectLastModified(t *testing.T) {
+ want := fstest.Time("2021-06-06T06:06:06Z")
+ b, _, bucket := newPutTestBackend(t, "", nil)
+ ctx := context.Background()
+ const object = "last-modified.txt"
+
+ contents := []byte(random.String(50))
+ meta := map[string]string{
+ "X-Amz-Meta-Mtime": swift.TimeToFloatString(want),
+ "Last-Modified": formatHeaderTime(time.Now()),
+ }
+ _, err := b.PutObject(ctx, bucket, object, meta, bytes.NewReader(contents), int64(len(contents)))
+ require.NoError(t, err)
+
+ head, err := b.HeadObject(ctx, bucket, object)
+ require.NoError(t, err)
+ assert.Equal(t, formatHeaderTime(want), head.Metadata["Last-Modified"], "HeadObject")
+
+ get, err := b.GetObject(ctx, bucket, object, nil)
+ require.NoError(t, err)
+ _ = get.Contents.Close()
+ assert.Equal(t, formatHeaderTime(want), get.Metadata["Last-Modified"], "GetObject")
+}
+
// waitForObject waits for bucket/object to appear on the backing Fs (e.g.
// after the VFS write-back delay).
func waitForObject(t *testing.T, f fs.Fs, bucket, object string) {