Repository: minio/minio (language: go)

Issue:
CopyObject() with x-amz-metadata-directive REPLACE new version is not readable
## NOTE

When creating a copy of the same object from a previous version using the `CopyObject` method, setting the `x-amz-metadata-directive` header to `REPLACE` and specifying the adjacent `x-amz-meta-` headers corrupts the copy.

## Expected Behavior

`HeadObject` request against the newly created object version must successfully complete, returning all the associated metadata.

## Current Behavior

Any of the following attempts to perform a `HeadObject` operation against the newly created version of the file fails with HTTP 500 error:

```
minio-1         | RemoteHost: 192.168.0.1
minio-1         | Host: localhost:9000
minio-1         | UserAgent: Boto3/1.34.79 md/Botocore#1.34.79 ua/2.0 os/linux#5.15.146.1-microsoft-standard-WSL2 md/arch#x86_64 lang/python#3.10.12 md/pyimpl#CPython cfg/retry-mode#legacy Botocore/1.34.79
minio-1         | Error: file is corrupted (cmd.StorageErr)
minio-1         |        6: internal/logger/logger.go:259:logger.LogIf()
minio-1         |        5: cmd/logging.go:90:cmd.internalLogIf()
minio-1         |        4: cmd/api-errors.go:2533:cmd.toAPIError()
minio-1         |        3: cmd/object-handlers.go:1073:cmd.objectAPIHandlers.headObjectHandler()
minio-1         |        2: cmd/object-handlers.go:1237:cmd.objectAPIHandlers.HeadObjectHandler()
minio-1         |        1: net/http/server.go:2136:http.HandlerFunc.ServeHTTP()
```

## Steps to Reproduce (for bugs)

1. Use the compose configuration below and wait until the server has started up.
2. Select a Python interpreter, install a `boto3` library and run the following snippet:

```python
import boto3
import logging

logging.basicConfig(level=logging.DEBUG)

key = "key"
bucket = "data"

client = boto3.client(
    "s3",
    endpoint_url="http://localhost:9000",
    aws_access_key_id="minio-root",
    aws_secret_access_key="minio-root",
    aws_session_token=None,
    config=boto3.session.Config(signature_version="s3v4"),
    verify=False,
)

logging.info(f"creating an object; {key=}")
response = client.put_object(
    Bucket=bucket,
    Key=key,
    Body=b"key-data",
    Metadata={
        "metadata-key": "metadata-value",
    },
)
original_version_id = response["VersionId"]

logging.info(f"creating an object copy; {key=}, {original_version_id=}")
response = client.copy_object(
    Bucket=bucket,
    Key=key,
    CopySource={
        "Bucket": bucket,
        "Key": key,
        "VersionId": original_version_id,
    },
    Metadata={
        "metadata-key": "other-metadata-value",
    },
    MetadataDirective="REPLACE",
)
copy_version_id = response["VersionId"]

logging.info(f"heading an object; {key=}, {copy_version_id=}")
client.head_object(
    Bucket=bucket,
    Key=key,
    VersionId=copy_version_id,
)
```

## Context

I want to create a copy of an existing object version, overriding its metadata.

## Regression

No

## Your Environment

* Version used: `DEVELOPMENT.2024-04-06T05-26-02Z (commit-id=9d63bb1b418f6c1bbcc8434fff5d8aba810ee5d7)`
* Server setup and configuration:
```yaml
version: "3.8"
services:
  minio:
    image: "bitnami/minio:2024.4.6"
    environment:
      MINIO_ROOT_USER: minio-root
      MINIO_ROOT_PASSWORD: minio-root
      MINIO_DEFAULT_BUCKETS: data
    ports:
      - "9000:9000"
      - "9001:9001"

  minio-config:
    image: "bitnami/minio-client:2024.3.30"
    depends_on:
      - minio
    environment:
      MINIO_SERVER_HOST: minio
      MINIO_SERVER_ROOT_USER: minio-root
      MINIO_SERVER_ROOT_PASSWORD: minio-root
    command:
      - /bin/bash
      - -ce
      - |
        /opt/bitnami/scripts/minio-client/setup.sh
        sleep 10
        mc version enable minio/data
```

* Operating System and version (`uname -a`): `Linux NV-F4SHWL3 5.15.146.1-microsoft-standard-WSL2 #1 SMP Thu Jan 11 04:09:03 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux`



Discussion / hints:

Fix patch:
diff --git a/cmd/erasure-object.go b/cmd/erasure-object.go
index ada14422e805b..4b40750e3fc8b 100644
--- a/cmd/erasure-object.go
+++ b/cmd/erasure-object.go
@@ -147,11 +147,12 @@ func (er erasureObjects) CopyObject(ctx context.Context, srcBucket, srcObject, d
 		modTime = dstOpts.MTime
 		fi.ModTime = dstOpts.MTime
 	}
+	// check inline before overwriting metadata.
+	inlineData := fi.InlineData()
 
 	fi.Metadata = srcInfo.UserDefined
 	srcInfo.UserDefined["etag"] = srcInfo.ETag
 
-	inlineData := fi.InlineData()
 	freeVersionID := fi.TierFreeVersionID()
 	freeVersionMarker := fi.TierFreeVersion()
 


Test patch: