Skip to content

Copy to same bucket/key empties content of object. #468

@samuelsayag

Description

@samuelsayag

Hello,

I am currently using S3Mock and discovered that copying an object to the same (bucket/key) "empties" the content of the object.
The same code can be run against AWS/S3 and of course copies correctly the object (when multi-versioning is activated the versions are accumulated correctly).

Tested with s3mock: 2.4.9

Here is the code with S3Mock (it is Scala code):

  val port = 9090
  val host = "localhost"
  val scheme = "http"
  val endpoint = s"$scheme://$host:$port"
  val region = Region.EU_WEST_1
  val bucket = "raw-data"

  var server: S3MockApplication = _
  try {
    server = S3MockApplication.start(s"--com.adobe.testing.s3mock.domain.initialBuckets=$bucket")
    val s3 = S3Client.builder().region(region).endpointOverride(URI.create(endpoint)).build()
    copyLogic(s3, bucket)
  } finally {
    server.stop()
  }

  def copyLogic(s3: S3Client, bucketName: String) = {
    val stringObjKeyName = "README.md"
    val fileObjKeyName = "sam/testdata"
    val keyName = s"$fileObjKeyName/$stringObjKeyName"
    val source = s"$bucketName/$keyName"
    val content =
      """README
        |======
        |
        | This is a readme.""".stripMargin.replaceAll("\n", " ")

    s3.putObject(PutObjectRequest.builder().bucket(bucketName).key(keyName).build(), RequestBody.fromString(content))
    getAndLog(s3, bucketName, keyName)
    s3.copyObject(
      CopyObjectRequest.builder().copySource(source).destinationBucket(bucketName).destinationKey(keyName).build()
    )
    getAndLog(s3, bucketName, keyName)
  }

  def getAndLog(s3: S3Client, bucket: String, key: String) = {
    val get = s3.getObject(GetObjectRequest.builder().bucket(bucket).key(key).build())
    val content = get.readAllBytes()
    println(s"content: [${new String(content)}]")
    println(s"size: [${content.length}]")
  }

This logs:

...
2022-03-06 11:39:54 [main] INFO  scala.App - Started App in 3.115 seconds (JVM running for 3.628)
content: [README ======   This is a readme.]
size: [33]
content: []
size: [0]
...

I think it is a bug no? WDYT?

thanks!

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions