英文:
aws-sdk-go-v2 set custom header on PutObjectInput
问题
在将对象放入S3兼容存储系统时,您可以通过设置自定义标头来实现。要指定自定义标头,您可以在s3.PutObjectInput
结构体中的Metadata
字段中提供键值对。例如,您可以使用以下代码设置自定义标头:
metadata := map[string]string{
"CustomHeaderKey": "CustomHeaderValue",
}
_, err := uploader.Upload(ctx, &s3.PutObjectInput{
Bucket: aws.String(util.GlobalConfig.S3.Bucket),
Key: aws.String(filename),
Body: w,
ContentMD5: func() *string {
h := md5.New()
h.Write(w.Bytes())
return aws.String(base64.StdEncoding.EncodeToString(h.Sum(nil)))
}(),
Metadata: metadata,
ObjectLockMode: types.ObjectLockModeCompliance,
ObjectLockRetainUntilDate: &objectLockTime,
})
在上述代码中,您可以将metadata
变量替换为您想要设置的自定义标头键值对。请确保键和值都是字符串类型。
英文:
I am in a situation where i need to set a custom header on putting an object to an s3 compatible storage system.
_, err := uploader.Upload(ctx, &s3.PutObjectInput{
Bucket: aws.String(util.GlobalConfig.S3.Bucket),
Key: aws.String(filename),
Body: w,
ContentMD5: func() *string {
h := md5.New()
h.Write(w.Bytes())
return aws.String(base64.StdEncoding.EncodeToString(h.Sum(nil)))
}(),
Metadata: metadata,
ObjectLockMode: types.ObjectLockModeCompliance,
ObjectLockRetainUntilDate: &objectLockTime,
})
How can one specify a custom header?
答案1
得分: 3
你可以通过将BuildMiddleware
注册到中间件堆栈中来实现这一点。请参阅自定义 AWS SDK for Go V2 客户端请求获取更多信息。
这里是一个简单的示例:
package main
import (
"context"
"fmt"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
)
func SetHttpHeader(key, value string) func(*middleware.Stack) error {
return func(stack *middleware.Stack) error {
return stack.Build.Add(middleware.BuildMiddlewareFunc("IDoEVaultGrant", func(
ctx context.Context, in middleware.BuildInput, next middleware.BuildHandler,
) (
middleware.BuildOutput, middleware.Metadata, error,
) {
switch v := in.Request.(type) {
case *smithyhttp.Request:
v.Header.Add(key, value)
}
return next.HandleBuild(ctx, in)
}), middleware.Before)
}
}
func main() {
client := s3.New(s3.Options{
Credentials: credentials.NewStaticCredentialsProvider("your-key-id", "your-key-secret", ""),
}, s3.WithAPIOptions(SetHttpHeader("key", "value")))
uploader := manager.NewUploader(client, func(uploader *manager.Uploader) {
uploader.PartSize = 64 * 1024 * 1024 // 64MB
})
file, err := os.Open("your-file")
if err != nil {
fmt.Println(err)
return
}
output, err := uploader.Upload(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String("your-bucket"),
Key: aws.String("your-key"),
Body: file,
})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(output.Location)
return
}
英文:
You can do this by registering BuildMiddleware
to the middleware stack. Please see Customizing the AWS SDK for Go V2 Client Requests for more information.
Here is a simple example:
package main
import (
"context"
"fmt"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
)
func SetHttpHeader(key, value string) func(*middleware.Stack) error {
return func(stack *middleware.Stack) error {
return stack.Build.Add(middleware.BuildMiddlewareFunc("IDoEVaultGrant", func(
ctx context.Context, in middleware.BuildInput, next middleware.BuildHandler,
) (
middleware.BuildOutput, middleware.Metadata, error,
) {
switch v := in.Request.(type) {
case *smithyhttp.Request:
v.Header.Add(key, value)
}
return next.HandleBuild(ctx, in)
}), middleware.Before)
}
}
func main() {
client := s3.New(s3.Options{
Credentials: credentials.NewStaticCredentialsProvider("your-key-id", "your-key-secret", ""),
}, s3.WithAPIOptions(SetHttpHeader("key", "value")))
uploader := manager.NewUploader(client, func(uploader *manager.Uploader) {
uploader.PartSize = 64 * 1024 * 1024 // 64MB
})
file, err := os.Open("your-file")
if err != nil {
fmt.Println(err)
return
}
output, err := uploader.Upload(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String("your-bucket"),
Key: aws.String("your-key"),
Body: file,
})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(output.Location)
return
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论