Created
May 29, 2019 07:42
-
-
Save masterxavierfox/36e9cd61526935c4f8373fc8fbd89b3f to your computer and use it in GitHub Desktop.
Revisions
-
Master Fox revised this gist
May 29, 2019 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ //https://socketloop.com/tutorials/golang-upload-and-download-file-to-from-aws-s3 package main import ( "bufio" -
Master Fox created this gist
May 29, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,103 @@ package main import ( "bufio" "fmt" "io" "launchpad.net/goamz/aws" "launchpad.net/goamz/s3" "net/http" "os" ) func main() { AWSAuth := aws.Auth{ AccessKey: "", // change this to yours SecretKey: "", } region := aws.USEast // change this to your AWS region // click on the bucketname in AWS control panel and click Properties // the region for your bucket should be under "Static Website Hosting" tab connection := s3.New(AWSAuth, region) bucket := connection.Bucket("<your bucketname>") // change this your bucket name path := "example/big.jpg" // this is the target file and location in S3 // need to read big.jpg into a []byte buffer // see https://www.socketloop.com/tutorials/golang-read-binary-file-into-memory fileToBeUploaded := "big.jpg" file, err := os.Open(fileToBeUploaded) if err != nil { fmt.Println(err) os.Exit(1) } defer file.Close() fileInfo, _ := file.Stat() var size int64 = fileInfo.Size() bytes := make([]byte, size) // read into buffer buffer := bufio.NewReader(file) _, err = buffer.Read(bytes) // then we need to determine the file type // see https://www.socketloop.com/tutorials/golang-how-to-verify-uploaded-file-is-image-or-allowed-file-types filetype := http.DetectContentType(bytes) err = bucket.Put(path, bytes, filetype, s3.ACL("public-read")) if err != nil { fmt.Println(err) os.Exit(1) // NOTE : If you get this error message // Get : 301 response missing Location header // this is because you are using the wrong region for the bucket // and if you want to figure out the bucket location automatically // see http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html // I've try it out with http.Get() and just getting the authenticating // requests part right is already too much // work for this tutorial. // See http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html // UPDATE 15th Jan 2015: See http://camlistore.org/pkg/misc/amazon/s3/#Client.BucketLocation } fmt.Printf("Uploaded to %s with %v bytes to S3.\n\n", path, size) // Download(GET) downloadBytes, err := bucket.Get(path) if err != nil { fmt.Println(err) os.Exit(1) } downloadFile, err := os.Create("download.jpg") if err != nil { fmt.Println(err) os.Exit(1) } defer downloadFile.Close() downloadBuffer := bufio.NewWriter(downloadFile) downloadBuffer.Write(downloadBytes) io.Copy(downloadBuffer, downloadFile) fmt.Printf("Downloaded from S3 and saved to download.jpg. \n\n") }