-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- show/delete commands for buckets - executes calls on behalf of the user account
- Loading branch information
Showing
5 changed files
with
227 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Package s3 simulates an administrative interface for people who maintain ostor | ||
// this package wraps around ostor and minio/minio-go to executes calls on behalf | ||
// of accounts in the system. This is achieved by returning an account's credential | ||
// pair and using it for calls. It requires that an account has one. You can call | ||
// the key management features in the ostor CLI to achieve that or use the user methods | ||
// in the ostor package to achieve the same. | ||
package s3 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
log "log/slog" | ||
"net/url" | ||
"os" | ||
|
||
"github.com/Luzilla/acronis-s3-usage/pkg/ostor" | ||
"github.com/minio/minio-go/v7" | ||
"github.com/minio/minio-go/v7/pkg/credentials" | ||
) | ||
|
||
// S3 wraps minio and ostor to provide a management API. | ||
type S3 struct { | ||
mc *minio.Client | ||
} | ||
|
||
// NewS3 creates the S3 struct from endpoint, email and ostor. | ||
func NewS3(endpointURL, email string, ostorClient *ostor.Ostor) (*S3, error) { | ||
endpoint, err := url.Parse(endpointURL) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to parse %s: %s", endpoint, err) | ||
} | ||
|
||
user, _, err := ostorClient.GetUser(email) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(user.AccessKeys) == 0 { | ||
return nil, fmt.Errorf("account has no keys, please generate a key-pair first") | ||
} | ||
|
||
keyPair := user.AccessKeys[0] | ||
|
||
mc, err := minio.New(endpoint.Host, &minio.Options{ | ||
Creds: credentials.NewStaticV4( | ||
keyPair.AccessKeyID, | ||
keyPair.SecretAccessKey, | ||
""), | ||
Secure: true, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to initialize s3 client: %s", err) | ||
} | ||
|
||
return &S3{mc: mc}, nil | ||
} | ||
|
||
// CheckBucket determines if a bucket exists. | ||
func (s *S3) CheckBucket(ctx context.Context, bucketName string) error { | ||
status, err := s.mc.BucketExists(ctx, bucketName) | ||
if err != nil { | ||
return fmt.Errorf("unable to check if the bucket exists: %s", err) | ||
} | ||
|
||
if !status { | ||
fmt.Println("bucket does not exist") | ||
return nil | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// DeleteBucket does a recursive delete on all objects within a bucket and empties it. | ||
func (s *S3) DeleteBucket(ctx context.Context, bucketName string) error { | ||
delChan := make(chan minio.ObjectInfo) | ||
|
||
go func() { | ||
defer close(delChan) | ||
for object := range s.ListContents(ctx, bucketName) { | ||
if object.Err != nil { | ||
log.Error(object.Err.Error()) | ||
os.Exit(1) | ||
} | ||
delChan <- object | ||
} | ||
}() | ||
|
||
for rErr := range s.mc.RemoveObjects(ctx, bucketName, delChan, minio.RemoveObjectsOptions{}) { | ||
log.Error("Error detected during deletion: " + rErr.Err.Error()) | ||
} | ||
|
||
return s.mc.RemoveBucket(ctx, bucketName) | ||
} | ||
|
||
// ListContents lists the contents of a bucket and returns a channel to "range" on. | ||
func (s *S3) ListContents(ctx context.Context, bucketName string) <-chan minio.ObjectInfo { | ||
return s.mc.ListObjects(ctx, bucketName, minio.ListObjectsOptions{Recursive: true}) | ||
} |