Created
August 8, 2020 15:51
-
-
Save ykcab/205d9ecc6e881fa9e74abeacb9632538 to your computer and use it in GitHub Desktop.
CAUTION: not tested in production
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/service/s3/s3manager" | |
) | |
func main() { | |
file, err := os.Open("upload_file") | |
if err != nil { | |
log.Fatal("Failed to open file", err) | |
} | |
// Note required, but you could zip the file prior to uploading it | |
// using io.Pipe read/writer to stream gzip'ed 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(nil) | |
result, err := uploader.Upload(&s3manager.UploadInput{ | |
Body: reader, | |
Bucket: aws.String("myBucket"), | |
Key: aws.String("myKey"), | |
}) | |
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