forked from koblas/s3-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
34 lines (28 loc) · 1.02 KB
/
session.go
File metadata and controls
34 lines (28 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
const DEFAULT_REGION = "us-east-1"
func SessionNew(config *Config) *s3.S3 {
creds := credentials.NewStaticCredentials(config.AccessKey, config.SecretKey, "")
// By default make sure a region is specified
return s3.New(session.New(&aws.Config{ Credentials: creds, Region: aws.String(DEFAULT_REGION) }))
}
func SessionForBucket(svc *s3.S3, bucket string) (*s3.S3, error) {
/*
params := &s3.HeadBucketInput{ Bucket: aws.String(bucket) }
_, err := svc.HeadBucket(params)
if err != nil {
return nil, err
}
*/
if loc, err := svc.GetBucketLocation(&s3.GetBucketLocationInput{Bucket: &bucket}); err != nil {
return nil, err
} else if (loc.LocationConstraint != nil) {
return s3.New(session.New(&svc.Client.Config, &aws.Config{Region: loc.LocationConstraint})), nil
}
return svc, nil
}