Skip to content

Copying a file with preserved UserMetadata resets the Content-Type #2094

@JonasDoe

Description

@JonasDoe

If I'm copying a file and transfer the UserMetadata, the ContentType is lost and defaults to binary/octet-stream.

func TestCopyWithContentType(t *testing.T) {
	client, err := minio.New("127.0.0.1:9000", &minio.Options{
		Creds:  credentials.NewStaticV4("minioadmin", "minioadmin", ""),
		Secure: false,
	})
	if err != nil {
		panic(err)
	}
	bucket := "unittest"
	ctx := context.Background()
	_ = client.MakeBucket(ctx, bucket, minio.MakeBucketOptions{})
	content := []byte("some text")
	if _, err := client.PutObject(ctx, bucket, "file1.txt", bytes.NewReader(content), int64(len(content)), minio.PutObjectOptions{ContentType: "text/plain"}); err != nil {
		panic(err)
	}
	uploadedInfo, err := client.StatObject(ctx, bucket, "file1.txt", minio.StatObjectOptions{})
	if err != nil {
		panic(err)
	}
        // Replacing the Metadata is important to cause this issue
	if _, err := client.CopyObject(ctx, minio.CopyDestOptions{Bucket: bucket, Object: "file2.txt", UserMetadata: uploadedInfo.UserMetadata, ReplaceMetadata: true}, minio.CopySrcOptions{Bucket: bucket, Object: "file1.txt"}); err != nil {
		panic(err)
	}
	copiedInfo, err := client.StatObject(ctx, bucket, "file2.txt", minio.StatObjectOptions{})
	if err != nil {
		panic(err)
	}
	if uploadedInfo.ContentType != copiedInfo.ContentType { // text/plain != binary/octet-stream
		t.FailNow()
	}
}

I understand that's b/c minio handles this particular entry differently, e.g. client.PutObject(ctx, bucket, "file1.txt", bytes.NewReader(content), int64(len(content)), minio.PutObjectOptions{UserMetadata: map[string]string{"Content-Type": "text/plain"}}) will cause an error Content-Type unsupported user defined metadata name, so I guess this "special handling" is missing when invoking Copy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions