aws-sdk-go-v2在PutObjectInput上设置自定义标头

huangapple go评论87阅读模式
英文:

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
}

huangapple
  • 本文由 发表于 2022年3月23日 14:07:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/71582516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定