1 回答

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊
正如@dazwilkin提到的那樣,fmt.Sprintf
對于這種情況來說已經(jīng)足夠了,但是您也可以使用我從mozilla移植的這個(gè)庫
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 關(guān)注
- 131 瀏覽
添加回答
舉報(bào)