1

当我使用 Minio Golang SDK 将文件上传到 S3 时,我正在尝试设置 Content-MD5 标头。我可以在不设置 Content-MD5 的情况下成功将文件上传到 AWS,但是上传到 IBM Cloud Object Storage 失败并出现以下错误:

ERR: Object write failed, reason: Missing required header for this request: Content-MD5

根据 Minio SDK,
https:
//docs.minio.io/docs/golang-client-api-reference#FPutObject 我使用 minio.PutObjectOptions 中的 UserMetadata 字段来设置 Content-MD5,但 IBM Cloud Object Storage 一直抱怨缺失MD5,我在下面的代码中做错了吗?

func (cloudIO *CloudIO) FWrite(name string) (n int, err error) {
    f, err := os.Open(name)
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()

    h := md5.New()
    if _, err := io.Copy(h, f); err != nil {
        log.Fatal(err)
    }

    bytesWritten, err := cloudIO.client.FPutObject(cloudIO.bucket, cloudIO.address,
        name,
        minio.PutObjectOptions{UserMetadata: map[string]string{"Content-MD5": hex.EncodeToString(h.Sum(nil))}})
    return int(bytesWritten), err
}
4

2 回答 2

1

@pacalj If you look at AWS documentation for PutObject https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html . Content-MD5 is not a required field. Meaning if it is not set by the client, the server should not error out, as you have already seen with AWS S3. As far as minio-go sdk is concerned, content-MD5 cannot be set via PutObjectOptions as explained in https://docs.minio.io/docs/golang-client-api-reference#FPutObject

Minio-go sets X-Amz-Content-Sha256 in the case of http connection and Content-Md5 in the case of https connections. Minio-go's FPutObject and PutObject api abstracts both multi-part put and single part put into these APIs. In the case of multi-part PUT, each part will set either X-Amz-Content-Sha256 or Content-Md5 depending on the type of connection. Since the call is abstracted, it is not possible for a user to set Content-Md5.

I believe IBM Cloud Object Storage has a bug as it should not error out, even if Content-Md5 is not set.

于 2018-07-06T18:17:59.907 回答
0

您也可以使用 minioCore代替Client设置 MD5 和 SHA256 标头。正如文档所说Core

NewCore - 返回新初始化的 Core 客户端,此 CoreClient 应仅在特殊情况下使用,例如需要访问较低的原语并能够使用它们来编写自己的包装器。

它有一个带有and参数的PutObject方法。md5Base64sha256Hex

于 2021-08-30T12:06:18.090 回答