коммит произвёл
GitHub
родитель
565f65a0c3
Коммит
3a3ec001bf
@@ -94,9 +94,9 @@ func (s *FileBackendTestSuite) SetupTest() {
|
||||
s.backend = backend
|
||||
|
||||
// This is needed to create the bucket if it doesn't exist.
|
||||
if err := s.backend.TestConnection(); err == ErrNoS3Bucket {
|
||||
s3Backend, ok := s.backend.(*S3FileBackend)
|
||||
s.True(ok)
|
||||
err = s.backend.TestConnection()
|
||||
if _, ok := err.(*S3FileBackendNoBucketError); ok {
|
||||
s3Backend := s.backend.(*S3FileBackend)
|
||||
s.NoError(s3Backend.MakeBucket())
|
||||
} else {
|
||||
s.NoError(err)
|
||||
|
||||
@@ -36,13 +36,18 @@ type S3FileBackend struct {
|
||||
client *s3.Client
|
||||
}
|
||||
|
||||
type S3FileBackendAuthError struct {
|
||||
DetailedError string
|
||||
}
|
||||
|
||||
// S3FileBackendNoBucketError is returned when testing a connection and no S3 bucket is found
|
||||
type S3FileBackendNoBucketError struct{}
|
||||
|
||||
const (
|
||||
// This is not exported by minio. See: https://github.com/minio/minio-go/issues/1339
|
||||
bucketNotFound = "NoSuchBucket"
|
||||
)
|
||||
|
||||
// ErrNoS3Bucket is returned when testing a connection and no S3 bucket is found
|
||||
var ErrNoS3Bucket = errors.New("no such bucket")
|
||||
var (
|
||||
imageExtensions = map[string]bool{".jpg": true, ".jpeg": true, ".gif": true, ".bmp": true, ".png": true, ".tiff": true, "tif": true}
|
||||
imageMimeTypes = map[string]string{".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".bmp": "image/bmp", ".png": "image/png", ".tiff": "image/tiff", ".tif": "image/tif"}
|
||||
@@ -61,6 +66,14 @@ func getImageMimeType(ext string) string {
|
||||
return imageMimeTypes[ext]
|
||||
}
|
||||
|
||||
func (s *S3FileBackendAuthError) Error() string {
|
||||
return s.DetailedError
|
||||
}
|
||||
|
||||
func (s *S3FileBackendNoBucketError) Error() string {
|
||||
return "no such bucket"
|
||||
}
|
||||
|
||||
// NewS3FileBackend returns an instance of an S3FileBackend.
|
||||
func NewS3FileBackend(settings FileBackendSettings) (*S3FileBackend, error) {
|
||||
backend := &S3FileBackend{
|
||||
@@ -148,19 +161,19 @@ func (b *S3FileBackend) TestConnection() error {
|
||||
if obj.Err != nil {
|
||||
typedErr := s3.ToErrorResponse(obj.Err)
|
||||
if typedErr.Code != bucketNotFound {
|
||||
return errors.Wrap(err, "unable to list objects in the s3 bucket")
|
||||
return &S3FileBackendAuthError{DetailedError: "unable to list objects in the S3 bucket"}
|
||||
}
|
||||
exists = false
|
||||
}
|
||||
} else {
|
||||
exists, err = b.client.BucketExists(context.Background(), b.bucket)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "unable to check if the s3 bucket exists")
|
||||
return &S3FileBackendAuthError{DetailedError: "unable to check if the S3 bucket exists"}
|
||||
}
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return ErrNoS3Bucket
|
||||
return &S3FileBackendNoBucketError{}
|
||||
}
|
||||
mlog.Debug("Connection to S3 or minio is good. Bucket exists.")
|
||||
return nil
|
||||
|
||||
Ссылка в новой задаче
Block a user