findify / s3mock

Embedded S3 server for easy mocking
MIT License
386 stars 107 forks source link

putObject() overrides lastModified in ObjectMetaData #163

Open jlippmann opened 4 years ago

jlippmann commented 4 years ago

I have an usecase where I want to cleanup objects created a certain time ago. For that I create a new object (putObject()) with an ObjectMetaDatas lastModified far in the past. Unfortunately this date always gets overriden in both, FileProvider as well as InMemoryProvider.

My proposal: Set the current DateTime for an empty lastModified only, keep the set value otherwise.

xhanshawn commented 3 years ago

I have a similar use case for checking the last modified time of an object. I solved it by modified the object meta in the metastore:

val api = S3Mock(port = port, dir = dir)

def overwriteLastModified(bucket: String, key: String, date: Date): Unit = {
  val metastore = api.p.metadataStore
  val meta = metastore.get(bucket, objectKey) // ObjectMetadata
  meta.setLastModified(date)
  metastore.put(bucket, objectKey, meta)
}

At least this works for me. In my case, the object is created as part of the preparation for the test. So I usually do:

mockS3.putObject(bucket, key, data)
overwriteLastModified(bucket, key)

// run the test

But I agree, ideally, the S3Mock should behave as what you raised. Hopefully, what I did can help your case.