如何在 Minio SDK 中设置 Content-MD5 header 以上传到 IBM Cloud Object 存储?
How can I set Content-MD5 header in Minio SDK for uploading to IBM Cloud Object Storage?
当我使用 Minio Golang SDK 将文件上传到 S3 时,我正在尝试设置 Content-MD5 header。我可以在不设置 Content-MD5 的情况下成功将文件上传到 AWS,但是上传到 IBM Cloud Object 存储失败并出现以下错误:
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 存储一直抱怨缺少 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
}
@pacalj 如果您查看 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
的 AWS 文档
Minio-go 在 http
连接的情况下设置 X-Amz-Content-Sha256
,在 https
连接的情况下设置 Content-Md5
。 Minio-go 的 FPutObject
和 PutObject
api 将 multi-part put
和 single part put
抽象到这些 API 中。在 multi-part
PUT 的情况下,每个部分将根据连接类型设置 X-Amz-Content-Sha256
或 Content-Md5
。由于调用是抽象的,因此用户无法设置 Content-Md5
。
我相信 IBM Cloud Object Storage
有一个错误,即使没有设置 Content-Md5
,它也不应该出错。
你也可以用minio Core
instead of Client
to set the MD5 and SHA256 headers. As the documentation says 关于Core
:
NewCore - Returns new initialized a Core client, this CoreClient
should be only used under special conditions such as need to access
lower primitives and being able to use them to write your own
wrappers.
它有一个 PutObject
method,它有 md5Base64
和 sha256Hex
个参数。
当我使用 Minio Golang SDK 将文件上传到 S3 时,我正在尝试设置 Content-MD5 header。我可以在不设置 Content-MD5 的情况下成功将文件上传到 AWS,但是上传到 IBM Cloud Object 存储失败并出现以下错误:
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 存储一直抱怨缺少 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
}
@pacalj 如果您查看 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 在 http
连接的情况下设置 X-Amz-Content-Sha256
,在 https
连接的情况下设置 Content-Md5
。 Minio-go 的 FPutObject
和 PutObject
api 将 multi-part put
和 single part put
抽象到这些 API 中。在 multi-part
PUT 的情况下,每个部分将根据连接类型设置 X-Amz-Content-Sha256
或 Content-Md5
。由于调用是抽象的,因此用户无法设置 Content-Md5
。
我相信 IBM Cloud Object Storage
有一个错误,即使没有设置 Content-Md5
,它也不应该出错。
你也可以用minio Core
instead of Client
to set the MD5 and SHA256 headers. As the documentation says 关于Core
:
NewCore - Returns new initialized a Core client, this CoreClient should be only used under special conditions such as need to access lower primitives and being able to use them to write your own wrappers.
它有一个 PutObject
method,它有 md5Base64
和 sha256Hex
个参数。