Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to set object Content-Type in composeObject #1304

Open
realAndDream opened this issue Jun 10, 2024 · 5 comments
Open

how to set object Content-Type in composeObject #1304

realAndDream opened this issue Jun 10, 2024 · 5 comments

Comments

@realAndDream
Copy link

realAndDream commented Jun 10, 2024

Hello everyone.
I want to upload an mp4 file to minio. I first uploaded the file's fragmented data, and then used composeObject to merge these fragmented files. The objects can be merged into a new object, but its Content-Type is binary/octet-stream. I can't modify the ContentType using UserMetadata. How do I specify this Content-Type? Thanks, the code is following:

async composeFileParts(dto: ComposeFilePartsDto) {
    const stream = this.minioClient.listObjects(dto.bucket, `${dto.md5}_`)
    const data = (await getReadObjectsNamesStreamData(stream)) as [
        {
            name: string
        },
    ]

    const allIndex: number[] = data.map((value) => {
        return parseInt(value.name.substring(value.name.lastIndexOf("_") + 1))
    })
    allIndex.sort((a, b) => (a < b ? -1 : 1))

    const sourceList: minio.CopySourceOptions[] = []
    allIndex.forEach((value) => {
        const source = new minio.CopySourceOptions({
            Bucket: dto.bucket,
            Object: `${dto.md5}_${value}`,
        })
        sourceList.push(source)
    })

    const destOption = new minio.CopyDestinationOptions({
        Bucket: dto.bucket,
        Object: `${dto.md5}.${dto.suffix}`,
        UserMetadata: {
            "Content-Type": "video/mp4",
            test: "test",
        },
        MetadataDirective: "REPLACE",
    })
    await this.minioClient.composeObject(destOption, sourceList)
}
@MamyAKKK
Copy link

I also encountered the same problem. (^_^)/

@fengjinlovewei
Copy link

Have the same problem and no developer to help?

@fengjinlovewei
Copy link

By the way, even if I set the "content-type" to "video/mp4" when I use fetch to upload the shard, the problem still exists

@fengjinlovewei
Copy link

I found a solution

import * as Minio from 'minio';
import mime from 'mime';

class MyCopyDestinationOptions extends Minio.CopyDestinationOptions {
  headers: Record<string, string> = {};
  constructor(
    option: Minio.ICopyDestinationOptions,
    headers: Record<string, string> = {},
  ) {
    super(option); // 调用父类的constructor()
    this.headers = headers;
  }
  getHeaders() {
    const headerOptions = super.getHeaders();

    for (const [key, value] of Object.entries(this.headers)) {
      headerOptions[key] = value;
    }

    return headerOptions;
  }
}

...
const destOption = new MyCopyDestinationOptions(
    {
      Bucket: bucketName,
      Object: fileName,
      UserMetadata: {
        'Content-Type': mime.getType(fileName)!,
      },
    },
    { 'Content-Type': mime.getType(fileName)! },
  );

const res = await minioClient.composeObject(destOption, sourceList);

If it doesn't work out, you can contact me

@MamyAKKK
Copy link

MamyAKKK commented Jan 2, 2025

Change set metadata from userMetadata attr to header attr, it's ok now~

ComposeObjectArgs.Builder builder = ComposeObjectArgs.builder();
builder.bucket(bucket);
builder.object(targetPath);
builder.sources(sourceList);

// Metadata setting
Map<String, String> metadata = Maps.newHashMap();
metadata.put("Content-Type", this.getContentType(targetPath));
builder.headers(metadata);// Right
// builder.userMetadata(metadata);// Wrong

this.client.composeObject(builder.build());// Compose Operation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants