In a Go 1.12 app, while dealing with AWS S3, for an insertion or deletion action each time creating new session object will be bad idea, can someone help sharing how can we create only single session throughout the app? Tried below sample code to generate bigS3 object at app bootup:
var bigS3 *s3.S3
func main() {
sess, awsSessionErr := session.NewSession(&aws.Config{
Region: aws.String(awsRegion),
Credentials: credentials.NewStaticCredentials(awsAccessKeyID, awsSecretAccessKey, awsSessionToken),
})
isError(awsSessionErr, "Error creating aws session: ")
bigS3 := s3.New(sess)
_, awsListBucketErr := bigS3.ListBuckets(nil) //Used to just built the session
isError(awsListBucketErr, "Unable to list AWS bucket(s): ")
}
Then tried using it like below in other function, but it fails as bigS3 in below method is nil.
func listObjectsInsideBucket(bucketName string) {
resp, awsListObjectsErr := bigS3.ListObjects(&s3.ListObjectsInput{Bucket: aws.String(bucketName)})
isError(awsListObjectsErr, "Unable to list items in bucket.")
}