-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieveFile.go
44 lines (37 loc) · 876 Bytes
/
retrieveFile.go
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
35
36
37
38
39
40
41
42
43
44
package s3
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"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"log"
"os"
)
func (c *S3Credentials) RetrieveFile(fname string, file *os.File) error {
log.Printf("Retrieving %s", fname)
sess, err := session.NewSession(
&aws.Config{
Region: aws.String(c.Region),
Credentials: credentials.NewStaticCredentials(
c.AccessKey,
c.SecretKey,
"",
),
})
if err != nil {
return err
}
downloader := s3manager.NewDownloader(sess)
numBytes, err := downloader.Download(
file,
&s3.GetObjectInput{
Bucket: aws.String(c.Bucket),
Key: aws.String(c.Path + fname),
})
if err != nil {
return err
}
log.Println("Downloaded", file.Name(), numBytes, "bytes")
return nil
}