Skip to content

Commit 0bf0a11

Browse files
Support query parameter aws_profile for S3 (#261)
Add support for aws_profile query parameter to allow fetching from S3 using that specified profile. Before this, that did not seem possible, even setting AWS_PROFILE environment variable did nothing. Now it is possible to provide the profile directly in the url, making it more flexible. Example of usage: s3::bucket.s3.amazonaws.com/file?aws_profile=some-profile
1 parent 7ac233b commit 0bf0a11

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ are also supported. If the query parameters are present, these take priority.
318318
* `aws_access_key_id` - AWS access key.
319319
* `aws_access_key_secret` - AWS access key secret.
320320
* `aws_access_token` - AWS access token if this is being used.
321+
* `aws_profile` - Use this profile from local ~/.aws/ config. Takes priority over the other three.
321322

322323
#### Using IAM Instance Profiles with S3
323324

get_s3.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ func (g *S3Getter) ClientMode(u *url.URL) (ClientMode, error) {
3030
}
3131

3232
// Create client config
33-
config := g.getAWSConfig(region, u, creds)
34-
sess := session.New(config)
35-
client := s3.New(sess)
33+
client, err := g.newS3Client(region, u, creds)
34+
if err != nil {
35+
return 0, err
36+
}
3637

3738
// List the object(s) at the given prefix
3839
req := &s3.ListObjectsInput{
@@ -88,9 +89,10 @@ func (g *S3Getter) Get(dst string, u *url.URL) error {
8889
return err
8990
}
9091

91-
config := g.getAWSConfig(region, u, creds)
92-
sess := session.New(config)
93-
client := s3.New(sess)
92+
client, err := g.newS3Client(region, u, creds)
93+
if err != nil {
94+
return err
95+
}
9496

9597
// List files in path, keep listing until no more objects are found
9698
lastMarker := ""
@@ -144,9 +146,11 @@ func (g *S3Getter) GetFile(dst string, u *url.URL) error {
144146
return err
145147
}
146148

147-
config := g.getAWSConfig(region, u, creds)
148-
sess := session.New(config)
149-
client := s3.New(sess)
149+
client, err := g.newS3Client(region, u, creds)
150+
if err != nil {
151+
return err
152+
}
153+
150154
return g.getObject(ctx, client, dst, bucket, path, version)
151155
}
152156

@@ -261,3 +265,25 @@ func (g *S3Getter) parseUrl(u *url.URL) (region, bucket, path, version string, c
261265

262266
return
263267
}
268+
269+
func (g *S3Getter) newS3Client(
270+
region string, url *url.URL, creds *credentials.Credentials,
271+
) (*s3.S3, error) {
272+
var sess *session.Session
273+
274+
if profile := url.Query().Get("aws_profile"); profile != "" {
275+
var err error
276+
sess, err = session.NewSessionWithOptions(session.Options{
277+
Profile: profile,
278+
SharedConfigState: session.SharedConfigEnable,
279+
})
280+
if err != nil {
281+
return nil, err
282+
}
283+
} else {
284+
config := g.getAWSConfig(region, url, creds)
285+
sess = session.New(config)
286+
}
287+
288+
return s3.New(sess), nil
289+
}

0 commit comments

Comments
 (0)