4 回答

TA貢獻1797條經(jīng)驗 獲得超4個贊
不幸的是,自從編寫了接受的答案以來,該庫似乎已經(jīng)更新,并且解決方案不再相同。經(jīng)過反復試驗,這似乎是處理簽名的最新方法:
import (
? ? "context"
? ? "net/http"
? ? "time"
? ? "github.com/aws/aws-sdk-go-v2/config"
? ? "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
)
func main() {
? ? // Context is not being used in this example.
? ? cfg, err := config.LoadDefaultConfig(context.TODO())
? ? if err != nil {
? ? ? ? // Handle error.
? ? }
? ? credentials, err := cfg.Credentials.Retrieve(context.TODO())
? ? if err != nil {
? ? ? ? // Handle error.
? ? }
? ? // The signer requires a payload hash. This hash is for an empty payload.
? ? hash := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
? ? req, _ := http.NewRequest(http.MethodGet, "api-gw-url", nil)
? ? signer := v4.NewSigner()
? ? err = signer.SignHTTP(context.TODO(), credentials, req, hash, "execute-api", cfg.Region, time.Now())
? ? if err != nil {
? ? ? ? // Handle error.
? ? }
? ? // Use `req`
}

TA貢獻1810條經(jīng)驗 獲得超5個贊
下面的解決方案使用 aws-sdk-go-v2 https://github.com/aws/aws-sdk-go-v2
// A AWS SDK session is created because the HTTP API is secured using a
// IAM authorizer. As such, we need AWS client credentials and a
// session to properly sign the request.
cfg, err := external.LoadDefaultAWSConfig(
external.WithSharedConfigProfile(profile),
)
if err != nil {
fmt.Println("unable to create an AWS session for the provided profile")
return
}
req, _ := http.NewRequest(http.MethodGet, "", nil)
req = req.WithContext(ctx)
signer := v4.NewSigner(cfg.Credentials)
_, err = signer.Sign(req, nil, "execute-api", cfg.Region, time.Now())
if err != nil {
fmt.Printf("failed to sign request: (%v)\n", err)
return
}
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("failed to call remote service: (%v)\n", err)
return
}
defer res.Body.Close()
if res.StatusCode != 200 {
fmt.Printf("service returned a status not 200: (%d)\n", res.StatusCode)
return
}

TA貢獻1865條經(jīng)驗 獲得超7個贊
第一個參數(shù)request.New
是aws.Config
,您可以在其中發(fā)送憑據(jù)。
例如使用靜態(tài)值:
creds:=?credentials.NewStaticCredentials("AKID",?"SECRET_KEY",?"TOKEN") req?:=?request.New(aws.Config{Credentials:?creds},?...)

TA貢獻1798條經(jīng)驗 獲得超7個贊
如果您查看 s3.New() 函數(shù)的代碼aws-sdk-go/service/s3/service.go
func?New(p?client.ConfigProvider,?cfgs?...*aws.Config)?*S3?{ c?:=?p.ClientConfig(EndpointsID,?cfgs...) return?newClient(*c.Config,?c.Handlers,?c.Endpoint,?c.SigningRegion,?.SigningName)?}
相對于 request.New() 函數(shù)aws-sdk-go/aws/request/request.go
func?New(cfg?aws.Config,?clientInfo?metadata.ClientInfo,?handlers?Handlers, retryer?Retryer,?operation?*Operation,?params?interface{},?data?interface{})?*Request?{?...
正如您在 s3 場景中看到的,*aws.Config 結構是一個指針,因此可能在其他地方初始化/填充。與 aws.Config 是參數(shù)的請求函數(shù)相反。所以我猜請求模塊可能是一個非常低級的模塊,它不會自動獲取共享憑證。
func?New(p?client.ConfigProvider,?cfgs?...*aws.Config)?*APIGateway?{ c?:=?p.ClientConfig(EndpointsID,?cfgs...) return?newClient(*c.Config,?c.Handlers,?c.Endpoint,?c.SigningRegion,?c.SigningName)?}...
它看起來與 s3 客戶端幾乎相同,所以也許嘗試使用它看看你如何去?
- 4 回答
- 0 關注
- 249 瀏覽
添加回答
舉報