minio / minio-js

MinIO Client SDK for Javascript
https://docs.min.io/docs/javascript-client-quickstart-guide.html
Apache License 2.0
932 stars 274 forks source link

Why putObject and fPutObject function in Javascript SDK does not have content-type parameters? #1216

Closed Louis-Tian closed 11 months ago

Louis-Tian commented 11 months ago

Is your feature request related to a problem? Please describe. Currently there is no way to set the content-type of objects with Javascript SDK. The putObject and fPutObject function does not have a content-type parameters. In contrast, the Python SDK has a dedicated function parameter on the putObject and fPutObject to set the content-type. So there is an inconsistency between the two SDK at the very least.

The Javascript SDK documentation on fPutObject includes an example where the content-type is set in metadata. However, it does not properly set the content-type of the object, the metadata content-type set is being save as "X-Amz-Meta-Contenttype" instead.

await this.client.putObject(hub, objectName, buffer, {
      ContentMD5: md5,
      ContentType: metadata.contentType,
      CacheControl: 'public, max-age=31536000',
    });

In additional, this issue also applies to other major HTTP header like Cache-Control, they are all being saved the with X-Amz-Meta prefix, and when the object is being served via HTTP regardless using a presigned url or not, non of those HTTP standard headers are being set by the server properly. This behavior is also not consistent/competible with S3.

Describe the solution you'd like There should be a way to set ContentType and other HTTP standard headers.

prakashsvmx commented 11 months ago

@Louis-Tian , please take a look the the examples and the sample shared. https://github.com/minio/minio-js/blob/master/examples/put-object.js

const putObjectTest = async () =>{

    const filePath = "/home/prakash/tmpwork/1.txt"
    const readStream = Fs.createReadStream(filePath)

    try {
        const result = await s3Client.putObject("test-bucket", "test-obj.txt", readStream, {
            "Content-Type": "text/plain",
            CacheControl: 'public, max-age=31536000',
            myMeta: "test-1235",
            'X-Amz-Meta-x-custom-header': 'new-custom-header'

        })

        if(result){
            console.log("Result", result)
        }

    }catch (err){
        console.log("Error", err.message)
    }

}

putObjectTest()
➜ mc stat local22/test-bucket/ 
Name      : test-obj.txt
Date      : 2023-10-23 12:26:30 IST 
Size      : 117 B  
ETag      : 4565d6ef17cf451513cd1b035abf1d4f 
Type      : file 
Metadata  :
  X-Amz-Meta-X-Custom-Header: new-custom-header 
  Content-Type              : text/plain 
  X-Amz-Meta-Cachecontrol   : public, max-age=31536000 
  X-Amz-Meta-Mymeta         : test-1235 

Please support with mc admin trace <ALIAS> if you notice any incompatibilities