add option to use an existing s3 client.
This commit is contained in:
parent
ff784c6585
commit
2edba5d03f
1 changed files with 16 additions and 10 deletions
26
storage.go
26
storage.go
|
@ -42,6 +42,9 @@ type Config struct {
|
||||||
S3AccessKey string
|
S3AccessKey string
|
||||||
|
|
||||||
S3BucketName string
|
S3BucketName string
|
||||||
|
|
||||||
|
// Override default s3 client.
|
||||||
|
MinioClient *minio.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new storage interface
|
// New creates a new storage interface
|
||||||
|
@ -54,26 +57,29 @@ func New(config Config) (*Storage, error) {
|
||||||
|
|
||||||
switch config.Type {
|
switch config.Type {
|
||||||
case "minio", "s3":
|
case "minio", "s3":
|
||||||
s3Client, err := minio.New(config.S3Endpoint, &minio.Options{
|
if config.MinioClient != nil {
|
||||||
Creds: credentials.NewStaticV4(config.S3AccessID, config.S3AccessKey, ""),
|
newStorage.s3Client = config.MinioClient
|
||||||
Secure: config.S3Secure,
|
} else {
|
||||||
})
|
s3Client, err := minio.New(config.S3Endpoint, &minio.Options{
|
||||||
if err != nil {
|
Creds: credentials.NewStaticV4(config.S3AccessID, config.S3AccessKey, ""),
|
||||||
return nil, err
|
Secure: config.S3Secure,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
newStorage.s3Client = s3Client
|
||||||
}
|
}
|
||||||
|
|
||||||
bucketExists, err := s3Client.BucketExists(newStorage.ctx, config.S3BucketName)
|
bucketExists, err := newStorage.s3Client.BucketExists(newStorage.ctx, config.S3BucketName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bucketExists {
|
if !bucketExists {
|
||||||
if err := s3Client.MakeBucket(newStorage.ctx, config.S3BucketName, minio.MakeBucketOptions{}); err != nil {
|
if err := newStorage.s3Client.MakeBucket(newStorage.ctx, config.S3BucketName, minio.MakeBucketOptions{}); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
newStorage.s3Client = s3Client
|
|
||||||
case "local":
|
case "local":
|
||||||
if statInfo, err := os.Stat(config.Path); err != nil && errors.Is(err, fs.ErrNotExist) {
|
if statInfo, err := os.Stat(config.Path); err != nil && errors.Is(err, fs.ErrNotExist) {
|
||||||
if err := os.MkdirAll(config.Path, 0740); err != nil {
|
if err := os.MkdirAll(config.Path, 0740); err != nil {
|
||||||
|
|
Loading…
Reference in a new issue