· 9 years ago · Jan 02, 2017, 11:21 PM
1var (
2 awsAuth aws.Auth
3 region aws.Region
4 connection s3.S3
5 bucket *s3.Bucket
6)
7func init() {
8
9 // Set up the AWS S3 Connection config.
10 awsAuth = aws.Auth{
11 AccessKey: os.Getenv("ACCESS_KEY"), // change this to yours
12 SecretKey: os.Getenv("SECRET_KEY"),
13 }
14 fmt.Println("AWS: ", awsAuth)
15 region := aws.EUWest
16 connection := s3.New(awsAuth, region)
17
18 bucket = connection.Bucket(os.Getenv("S3_BUCKET")) // change this your bucket name
19}
20func uploadToS3(filename string, byteArray *[]byte) error {
21
22 var err error
23 //should check if file already uploaded, encrypted by this password
24 fmt.Printf("[uploadToS3] starting upload of %sn", filename)
25
26 err = bucket.Put(filename, *byteArray, "text/plain; charset=utf-8", s3.PublicRead)
27 if err != nil {
28 fmt.Printf("[uploadToS3] error uploading: %sn", err)
29 return err
30 } else {
31 fmt.Printf("[uploadToS3] done uploading %sn", filename)
32 return nil
33 }
34
35 return nil // redundancy
36}