goS3 is a Go package for interacting with Amazon S3. It provides a simple interface for uploading files and generating presigned URLs for downloading files.
To install goS3, run the following command:
go get github.com/einnovationlabs/goS3To create a new client, you need to provide your AWS credentials and the name of the S3 bucket you want to interact with. Here's an example:
package main
import (
"context"
"fmt"
"github.com/einnovationlabs/goS3"
)
func main() {
cfg := goS3.Config{
AccessKey: "your-access-key",
SecretKey: "your-secret-key",
Region: "your-region",
BucketName: "your-bucket-name",
}
ctx := context.Background()
client, err := goS3.New(ctx, cfg)
if err != nil {
fmt.Println(err)
return
}
}To upload a file to your S3 bucket, use the Upload method:
fileContent := []byte("Hello, World!")
key := "path/to/your/file.txt"
if err := client.Upload(ctx, key, fileContent); err != nil {
fmt.Println(err)
return
}
fmt.Println("File uploaded successfully.")To generate a presigned URL for downloading a file, use the GeneratePresignedURL method:
key := "path/to/your/file.txt"
expiry := 5 * time.Minute // URL will be valid for 5 minutes
url, err := client.GeneratePresignedURL(ctx, key, expiry)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Presigned URL:", url)goS3 is released under the MIT License.