创建Presigned url时出现紧急错误

时间:2018-02-20 02:12:40

标签: amazon-web-services go amazon-s3

我尝试使用aws-sdk-go创建一个预先指定的网址,但它输出的内容却失败了:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x138d40a]

goroutine 1 [running]:
github.com/aws/aws-sdk-go/service/s3.New(0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
    /Users/me/go/src/github.com/aws/aws-sdk-go/service/s3/service.go:47 +0x3a
main.main()
    /Users/me/go/src/Test/main.go:22 +0xa0
exit status 2

我没有看到我犯错误的地方,或许更多有经验的人可以向我指出:

package main

import (
    "fmt"
    "log"
    "time"

    // "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/service/s3"
    "github.com/kelseyhightower/envconfig"
)


func main() {
  var configuration s3.PutObjectInput
  err := envconfig.Process("configuration", &configuration)

  if err != nil {
    log.Fatal(err.Error())
  }

  svc := s3.New(nil)
  req, _ := svc.PutObjectRequest(&configuration)
  url, err := req.Presign(15 * time.Minute)
  fmt.Println("The url is ", url)
}

1 个答案:

答案 0 :(得分:0)

您正在初始化s3服务nil时传递svc := s3.New(nil)会话,这就是为什么会出现nil指针错误。

您可以使用以下代码

传递会话

import "github.com/aws/aws-sdk-go/aws/session"

awsSession = session.Must(session.NewSessionWithOptions(session.Options{
    SharedConfigState: session.SharedConfigEnable,
}))
// Create S3 service client
serviceS3 = s3.New(awsSession)

您可能需要根据计划使用凭据的方式更改会话配置。您可以在此处阅读有关凭据配置的更多信息https://docs.aws.amazon.com/sdk-for-go/api/