package s3 import ( "context" "io" "git.kapelle.org/niklas/s3browser/internal/types" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" ) type minioS3 struct { client *minio.Client } func NewMinio(config types.AppConfig) (S3Service, error) { client, err := minio.New(config.S3Endoint, &minio.Options{ Creds: credentials.NewStaticV4(config.S3AccessKey, config.S3SecretKey, ""), Secure: config.S3SSL, }) if err != nil { return nil, err } return &minioS3{ client: client, }, nil } func (m *minioS3) ListBuckets(ctx context.Context) ([]string, error) { buckets, err := m.client.ListBuckets(ctx) if err != nil { return nil, err } var rtn []string for _, v := range buckets { rtn = append(rtn, v.Name) } return rtn, nil } func (m *minioS3) ListObjects(ctx context.Context, id types.ID) ([]Object, error) { var result []Object for objInfo := range m.client.ListObjects(ctx, id.Bucket, minio.ListObjectsOptions{ Prefix: id.Key, Recursive: false, }) { objId := types.ID{ Bucket: id.Bucket, Key: objInfo.Key, } result = append(result, Object{ ID: objId, Size: objInfo.Size, }) } return result, nil } func (m *minioS3) ListObjectsRecursive(ctx context.Context, id types.ID) ([]Object, error) { var result []Object for objInfo := range m.client.ListObjects(ctx, id.Bucket, minio.ListObjectsOptions{ Prefix: id.Key, Recursive: true, }) { objId := types.ID{ Bucket: id.Bucket, Key: objInfo.Key, } result = append(result, Object{ ID: objId, Size: objInfo.Size, }) } return result, nil } func (m *minioS3) GetObject(ctx context.Context, id types.ID) (ObjectReader, error) { object, err := m.client.GetObject(ctx, id.Bucket, id.Key, minio.GetObjectOptions{}) if err != nil { return nil, err } return object, nil } func (m *minioS3) PutObject(ctx context.Context, id types.ID, reader io.Reader, objectSize int64) error { _, err := m.client.PutObject(ctx, id.Bucket, id.Key, reader, objectSize, minio.PutObjectOptions{}) return err } func (m *minioS3) CopyObject(ctx context.Context, src types.ID, dest types.ID) error { _, err := m.client.CopyObject(ctx, minio.CopyDestOptions{ Bucket: dest.Bucket, Object: dest.Key, }, minio.CopySrcOptions{ Bucket: src.Bucket, Object: src.Key, }) return err } func (m *minioS3) StatObject(ctx context.Context, id types.ID) (*Object, error) { info, err := m.client.StatObject(ctx, id.Bucket, id.Key, minio.GetObjectOptions{}) if err != nil { return nil, err } return &Object{ ID: id, Size: info.Size, LastModified: info.LastModified, ContentType: info.ContentType, ETag: info.ETag, }, nil } func (m *minioS3) RemoveObject(ctx context.Context, id types.ID) error { return m.client.RemoveObject(ctx, id.Bucket, id.Key, minio.RemoveObjectOptions{}) }