Created
May 24, 2017 09:45
-
-
Save mpfund/0040810577df70caa37651541721089c to your computer and use it in GitHub Desktop.
golang upload file to aws
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"compress/gzip" | |
"io" | |
"log" | |
"os" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/s3/s3manager" | |
) | |
func main() { | |
file, err := os.Open("upload_file") | |
if err != nil { | |
log.Fatal("Failed to open file", err) | |
} | |
// Not required, but you could zip the file before uploading it | |
// using io.Pipe read/writer to stream gzip'd file contents. | |
reader, writer := io.Pipe() | |
go func() { | |
gw := gzip.NewWriter(writer) | |
io.Copy(gw, file) | |
file.Close() | |
gw.Close() | |
writer.Close() | |
}() | |
uploader := s3manager.NewUploader(session.New(&aws.Config{Region: aws.String("eu-central-1")})) | |
result, err := uploader.Upload(&s3manager.UploadInput{ | |
Body: reader, | |
Bucket: aws.String("blogs-1403714785"), | |
Key: aws.String("myKey.zip"), | |
}) | |
if err != nil { | |
log.Fatalln("Failed to upload", err) | |
} | |
log.Println("Successfully uploaded to", result.Location) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment