isle/go/garage/client.go

58 lines
1.4 KiB
Go
Raw Permalink Normal View History

package garage
import (
"errors"
2022-10-19 14:53:31 +00:00
"fmt"
"net/http"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
// IsKeyNotFound returns true if the given error is the result of a key not
// being found in a bucket.
func IsKeyNotFound(err error) bool {
var mErr minio.ErrorResponse
return errors.As(err, &mErr) && mErr.Code == "NoSuchKey"
}
// S3APIClient is a client used to interact with garage's S3 API.
type S3APIClient struct {
*minio.Client
transport *http.Transport
}
// Close cleans up all resources held by the client.
func (c *S3APIClient) Close() error {
c.transport.CloseIdleConnections()
return nil
}
// S3APICredentials describe data fields necessary for authenticating with a
// garage S3 API endpoint.
type S3APICredentials struct {
ID string
Secret string
}
// NewS3APIClient returns a minio client configured to use the given garage S3 API
// endpoint.
func NewS3APIClient(addr string, creds S3APICredentials) *S3APIClient {
transport := http.DefaultTransport.(*http.Transport).Clone()
2022-10-19 14:53:31 +00:00
client, err := minio.New(addr, &minio.Options{
Creds: credentials.NewStaticV4(creds.ID, creds.Secret, ""),
Region: Region,
Transport: transport,
})
2022-10-19 14:53:31 +00:00
if err != nil {
panic(fmt.Sprintf("initializing minio client at addr %q and with creds %+v", addr, creds))
}
return &S3APIClient{
Client: client,
transport: transport,
}
}