s3Compat
is a Go package that simplifies interactions with Amazon Simple Storage Service (S3) or S3-compatible storage services. The package provides an easy-to-use and straightforward API for various S3 operations such as uploading, downloading, and deleting objects.
- Upload objects to S3 using byte arrays, streams, or seekable streams.
- Download objects from S3 as byte arrays or streams.
- Delete objects from S3.
- List objects in a bucket.
- Support for custom S3-compatible services by specifying the endpoint.
To install s3Compat
, run the following command:
go get -u github.com/8ff/s3Compat
First, import the package into your Go application:
import "github.com/8ff/s3Compat"
Create a new S3 Params struct:
client, err := s3Compat.New(s3Compat.Params{
REGION: "us-west-2",
BUCKET: "my-bucket",
ACCESS_KEY: "your-access-key",
SECRET_KEY: "your-secret-key",
ENDPOINT: "https://s3.amazonaws.com",
})
Upload a byte array to S3:
err := client.PutObject("my-object-key", []byte("Hello, world!"))
Download an object from S3:
data, err := client.GetObject("my-object-key")
Delete an object from S3:
err := client.DeleteObject("my-object-key")
List objects in the bucket:
objectNames, err := client.ListObjects()
Here's a simple example that uploads and then downloads a text file:
package main
import (
"github.com/8ff/s3Compat"
)
func main() {
client, _ := s3Compat.New(s3Compat.Params{
REGION: "us-west-2",
BUCKET: "my-bucket",
ACCESS_KEY: "your-access-key",
SECRET_KEY: "your-secret-key",
ENDPOINT: "https://s3.amazonaws.com",
})
// Upload
_ = client.PutObject("test-file", []byte("Hello, world!"))
// Download
data, _ := client.GetObject("test-file")
println(string(data))
}
The following examples demonstrate how to use s3Compat
for stream and pipe operations.
This example reads from the standard input and uploads the data to S3.
func pipeObject(access_key string, secret string, endpoint string, args []string) {
// (Omitted: Error checks and argument validation)
api, err := s3Compat.New(s3Compat.Params{BUCKET: bucket, ACCESS_KEY: access_key, SECRET_KEY: secret, ENDPOINT: endpoint})
if err != nil {
// Handle error
}
err = api.PutObjectStream(file, os.Stdin)
if err != nil {
// Handle error
}
}
This example downloads an object from S3 and writes it to the standard output.
func catObject(access_key string, secret string, endpoint string, args []string) {
// (Omitted: Error checks and argument validation)
api, err := s3Compat.New(s3Compat.Params{BUCKET: bucket, ACCESS_KEY: access_key, SECRET_KEY: secret, ENDPOINT: endpoint})
if err != nil {
// Handle error
}
readCloser, err := api.GetObjectStream(file)
if err != nil {
// Handle error
}
defer readCloser.Close()
_, err = io.Copy(os.Stdout, readCloser)
if err != nil {
// Handle error
}
}
This example deletes an object from S3.
func rmObject(access_key string, secret string, endpoint string, args []string) {
// (Omitted: Error checks and argument validation)
api, err := s3Compat.New(s3Compat.Params{BUCKET: bucket, ACCESS_KEY: access_key, SECRET_KEY: secret, ENDPOINT: endpoint})
if err != nil {
// Handle error
}
err = api.DeleteObject(file)
if err != nil {
// Handle error
}
}
Additionally s3Compat_test.go contains more examples.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the AGPL 3.0 License. See the LICENSE file for details.