AWS S3 or Amazon S3 is a storage system from Amazon to store and retrieve files from anywhere on the web. It is a highly scalable, reliable, fast, inexpensive data storage system from Amazon. It provides easy to use developer kit to store and retrieve files.
As in our previous Go tutorial, you have learned how to write data to CSV file in Golang. In this tutorial you will learn how to upload files to AWS S3 using Golang. We will use the Go AWS SDK to upload files to AWS S3.
Also, read:
We will cover this tutorial step by step to upload files to AWS S3 server using Golang with example code.
Step1: Create AWS S3 File Upload Go File
We will create main.go
file and import necessary packages to upload the files.
package main import ( "bytes" "log" "net/http" "os" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" )
Step2: Store AWS S3 Bucket Details
We will create constant to store AWS S3 REGION and AWS S3 Bucket details. So you need to your AWS credentials setup (with AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY) and you’ll need to fill in the AWS_S3_REGION and AWS_S3_BUCKET constants.
const ( AWS_S3_REGION = "" AWS_S3_BUCKET = "" )
Step3: Set up Configuration and AWS S3 Session Instance
We will setup configuration using AWS S3 REGION and create single AWS session to upload multiple files to AWS S3. Then we will call method uploadFile()
and pass AWS session instance and file details to upload file to AWS S3 server.
func main() { session, err := session.NewSession(&aws.Config{Region: aws.String(AWS_S3_REGION)}) if err != nil { log.Fatal(err) } // Upload Files err = uploadFile(session, "test.png") if err != nil { log.Fatal(err) } }
Step4: Create Upload File to S3 Method
We will create method uploadFile()
to upload files to AWS S3 server. We will open the file and store into buffer and then put the file to AWS S3 uisng PutObject()
method from S3. We will also specify options in the PutObjectInput when uploading the file. We will also enable AES256 encryption on files using ServerSideEncryption options.
func uploadFile(session *session.Session, uploadFileDir string) error { upFile, err := os.Open(uploadFileDir) if err != nil { return err } defer upFile.Close() upFileInfo, _ := upFile.Stat() var fileSize int64 = upFileInfo.Size() fileBuffer := make([]byte, fileSize) upFile.Read(fileBuffer) _, err = s3.New(session).PutObject(&s3.PutObjectInput{ Bucket: aws.String(AWS_S3_BUCKET), Key: aws.String(uploadFileDir), ACL: aws.String("private"), Body: bytes.NewReader(fileBuffer), ContentLength: aws.Int64(fileSize), ContentType: aws.String(http.DetectContentType(fileBuffer)), ContentDisposition: aws.String("attachment"), ServerSideEncryption: aws.String("AES256"), }) return err }
Step5: Complete Code to Upload File to AWS S3 with Go
Here is the complete code to upload the files to AWS S3 server using Go.
package main import ( "bytes" "log" "net/http" "os" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" ) const ( AWS_S3_REGION = "" AWS_S3_BUCKET = "" ) func main() { session, err := session.NewSession(&aws.Config{Region: aws.String(AWS_S3_REGION)}) if err != nil { log.Fatal(err) } // Upload Files err = uploadFile(session, "test.png") if err != nil { log.Fatal(err) } } func uploadFile(session *session.Session, uploadFileDir string) error { upFile, err := os.Open(uploadFileDir) if err != nil { return err } defer upFile.Close() upFileInfo, _ := upFile.Stat() var fileSize int64 = upFileInfo.Size() fileBuffer := make([]byte, fileSize) upFile.Read(fileBuffer) _, err = s3.New(session).PutObject(&s3.PutObjectInput{ Bucket: aws.String(AWS_S3_BUCKET), Key: aws.String(uploadFileDir), ACL: aws.String("private"), Body: bytes.NewReader(fileBuffer), ContentLength: aws.Int64(fileSize), ContentType: aws.String(http.DetectContentType(fileBuffer)), ContentDisposition: aws.String("attachment"), ServerSideEncryption: aws.String("AES256"), }) return err }
You may also like:
- Regular Expressions in Golang
- Creating REST API with Golang
- How to Delete Files in Golang
- Working with Structs in Golang
- Working with Date and Time in Golang
- How to Make HTTP Requests in Golang
- Working with Channels in Golang
- Concurrency with Goroutines in Golang
- Working with Maps in Golang
- Marshalling and Unmarshalling in Golang
- Write Data to CSV File using Golang
- How to Read CSV File using Golang
- Parsing JSON Data using Golang
- Read File Line by Line using Golang