I'm trying to create a presigned url using the aws-sdk-go, but it it's failing with the following output:
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
I'm not seeing where I'm making a mistake, perhaps someone more experience in go could point out to me:
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)
}
You are passing a nil
session while initialising s3 service svc := s3.New(nil)
, that why you get the nil pointer error.
You can pass the session using following code,
new 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)
You might need to change the session configurations based on how you plan to use the credentials. You can read more about credential configurations here https://docs.aws.amazon.com/sdk-for-go/api/