1 回答
TA贡献1887条经验 获得超5个赞
正如@dazwilkin提到的那样,fmt.Sprintf
对于这种情况来说已经足够了,但是您也可以使用我从mozilla移植的这个库
import (
"context"
"fmt"
"time"
"cloud.google.com/go/storage"
"github.com/dkbyo/go-stringhttpheader"
)
type Headers struct {
GoogleMetaReference string `header:"x-goog-meta-reference"`
}
func GenerateSignedURL(bucket string, key string, expiration time.Time,
) (string, error) {
ctx := context.Background()
gcsClient, err := storage.NewClient(ctx)
if err != nil {
return "", fmt.Errorf("storage.NewClient: %v", err)
}
defer gcsClient.Close()
headers := Headers{
GoogleMetaReference: "xxx",
}
fmt.Print(stringhttpheader.Encode(headers))
stringheaders, _ := stringhttpheader.Encode(headers)
//storage.SignedURL()
opts := &storage.SignedURLOptions{
Scheme: storage.SigningSchemeV4,
Method: "PUT",
ContentType: "text/csv",
Headers: stringheaders,
Expires: expiration,
}
url, err := gcsClient.Bucket(bucket).SignedURL(key, opts)
if err != nil {
log.WithContext(ctx).Warn("Failed to generate a GCS signed URL")
return "", err
}
return url, nil
}
- 1 回答
- 0 关注
- 89 浏览
添加回答
举报