SimpleS3 is a Go library for manipulating objects in S3 buckets using REST API calls or Presigned URLs signed using AWS Signature Version 4.
go get github.com/rhnvrm/simples3
testTxt, _ := os.Open("testdata/test.txt")
defer testTxt.Close()
// Create an instance of the package
// You can either create by manually supplying credentials
// (preferably using Environment vars)
s3 := simples3.New(Region, AWSAccessKey, AWSSecretKey)
// or you can use this on an EC2 instance to
// obtain credentials from IAM attached to the instance.
s3, _ := simples3.NewUsingIAM(Region)
// You can also set a custom endpoint to a compatible s3 instance.
s3.SetEndpoint(CustomEndpoint)
// Note: Consider adding a testTxt.Seek(0, 0)
// in case you have read
// the body, as the pointer is shared by the library.
// File Upload is as simple as providing the following
// details.
resp, err := s3.FileUpload(simples3.UploadInput{
Bucket: AWSBucket,
ObjectKey: "test.txt",
ContentType: "text/plain",
FileName: "test.txt",
Body: testTxt,
})
// Similarly, Files can be deleted.
err := s3.FileDelete(simples3.DeleteInput{
Bucket: os.Getenv("AWS_S3_BUCKET"),
ObjectKey: "test.txt",
})
// You can also download the file.
file, _ := s3.FileDownload(simples3.DownloadInput{
Bucket: AWSBucket,
ObjectKey: "test.txt",
})
data, _ := ioutil.ReadAll(file)
file.Close()
// You can also use this library to generate
// Presigned URLs that can for eg. be used to
// GET/PUT files on S3 through the browser.
var time, _ = time.Parse(time.RFC1123, "Fri, 24 May 2013 00:00:00 GMT")
url := s.GeneratePresignedURL(PresignedInput{
Bucket: AWSBucket,
ObjectKey: "test.txt",
Method: "GET",
Timestamp: time,
ExpirySeconds: 86400,
})
You are more than welcome to contribute to this project. Fork and make a Pull Request, or create an Issue if you see any problem or want to propose a feature.
Rohan Verma hello@rohanverma.net
BSD-2-Clause-FreeBSD