I have made this simple software to connect to AWS and generate a presigned put url:
package main
import (
"fmt"
"log"
"time"
"github.com/minio/minio-go"
"github.com/kelseyhightower/envconfig"
)
// AWS contains the configuration to connect and create a presigned url on AWS.
type AWS struct {
Endpoint string
AccessKeyId string
SecretAccessKey string
SSL bool
BucketHost string
BucketKey string
}
func main() {
url, err = generatePresignedPutUrl()
if err != nil {
log.Fatal(err)
}
fmt.Println(url)
}
func generatePresignedPutUrl() {
var aws AWS
if err := envconfig.Process("aws", &aws); err != nil {
return err
}
svc, err := minio.New(aws.Endpoint, aws.AccessKeyId, aws.SecretAccessKey, aws.SSL)
if err != nil {
return err
}
url, err := svc.PresignedPutObject(aws.BucketHost, aws.BucketKey, time.Second * 24 * 60 * 60)
if err != nil {
return url, err
}
}
But when I try to run go run main.go
, the following errors show in my terminal:
# command-line-arguments
./main.go:23:5: undefined: url
./main.go:23:10: undefined: err
./main.go:23:39: generatePresignedPutUrl() used as value
./main.go:24:8: undefined: err
./main.go:25:19: undefined: err
./main.go:27:17: undefined: url
./main.go:33:9: too many arguments to return
have (error)
want ()
./main.go:38:9: too many arguments to return
have (error)
want ()
./main.go:43:9: too many arguments to return
have (*url.URL, error)
want ()
What exactly am I doing wrong here? I started to learn go a few days ago. I'm sorry if this is to much obvious.
Update:
I was able to figure it out that I should declare what I'm returning from generatePresignedPutUrl
, but still:
package main
import (
"fmt"
"log"
"time"
"github.com/minio/minio-go"
"github.com/kelseyhightower/envconfig"
)
// AWS contains the configuration to connect and create a pre-signed url on AWS.
type AWS struct {
Endpoint string
AccessKeyId string
SecretAccessKey string
SSL bool
BucketHost string
BucketKey string
}
func main() {
url, err := generatePresignedPutUrl()
if err != nil {
log.Fatal(err)
}
fmt.Println(url)
}
// generatePresignedPutUrl connects to S3 and generates a pre-signed put url
// using configurations from environemnt variables.
func generatePresignedPutUrl() (url string, err error) {
var aws AWS
if err := envconfig.Process("aws", &aws); err != nil {
return err
}
svc, err := minio.New(aws.Endpoint, aws.AccessKeyId, aws.SecretAccessKey, aws.SSL)
if err != nil {
return err
}
url, err := svc.PresignedPutObject(aws.BucketHost, aws.BucketKey, time.Second * 24 * 60 * 60)
if err != nil {
return url, err
}
return url
}
Errors:
# command-line-arguments
./main.go:36:9: not enough arguments to return
have (error)
want (string, error)
./main.go:41:9: not enough arguments to return
have (error)
want (string, error)
./main.go:44:14: no new variables on left side of :=
./main.go:44:14: cannot assign *url.URL to url (type string) in multiple assignment
./main.go:49:5: not enough arguments to return
have (string)
want (string, error)