Mm 29605 read permission s3 bucket (#16977)

Automatic Merge
Этот коммит содержится в:
Max Erenberg
2021-03-16 10:48:32 -04:00
коммит произвёл GitHub
родитель f0ccaa89bf
Коммит 4699845c0a
6 изменённых файлов: 116 добавлений и 12 удалений

Просмотреть файл

@@ -41,6 +41,8 @@ const (
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"}
@@ -157,16 +159,18 @@ func (b *S3FileBackend) TestConnection() error {
}
}
if exists {
mlog.Debug("Connection to S3 or minio is good. Bucket exists.")
} else {
mlog.Warn("Bucket specified does not exist. Attempting to create...")
err := b.client.MakeBucket(context.Background(), b.bucket, s3.MakeBucketOptions{Region: b.region})
if err != nil {
return errors.Wrap(err, "unable to create the s3 bucket")
}
if !exists {
return ErrNoS3Bucket
}
mlog.Debug("Connection to S3 or minio is good. Bucket exists.")
return nil
}
func (b *S3FileBackend) MakeBucket() error {
err := b.client.MakeBucket(context.Background(), b.bucket, s3.MakeBucketOptions{Region: b.region})
if err != nil {
return errors.Wrap(err, "unable to create the s3 bucket")
}
return nil
}

Просмотреть файл

@@ -4,11 +4,23 @@
package filestore
import (
"crypto/rand"
"encoding/base64"
"fmt"
"os"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// Copied from model/config.go to avoid an import cycle
const (
MinioAccessKey = "minioaccesskey"
MinioSecretKey = "miniosecretkey"
ImageDriverS3 = "amazons3"
)
func TestCheckMandatoryS3Fields(t *testing.T) {
cfg := FileBackendSettings{}
@@ -26,3 +38,42 @@ func TestCheckMandatoryS3Fields(t *testing.T) {
require.Equal(t, "s3.amazonaws.com", cfg.AmazonS3Endpoint, "should've set the endpoint to the default")
}
func TestMakeBucket(t *testing.T) {
s3Host := os.Getenv("CI_MINIO_HOST")
if s3Host == "" {
s3Host = "localhost"
}
s3Port := os.Getenv("CI_MINIO_PORT")
if s3Port == "" {
s3Port = "9000"
}
s3Endpoint := fmt.Sprintf("%s:%s", s3Host, s3Port)
// Generate a random bucket name
b := make([]byte, 30)
rand.Read(b)
bucketName := base64.StdEncoding.EncodeToString(b)
bucketName = strings.ToLower(bucketName)
bucketName = strings.Replace(bucketName, "+", "", -1)
bucketName = strings.Replace(bucketName, "/", "", -1)
cfg := FileBackendSettings{
DriverName: ImageDriverS3,
AmazonS3AccessKeyId: MinioAccessKey,
AmazonS3SecretAccessKey: MinioSecretKey,
AmazonS3Bucket: bucketName,
AmazonS3Endpoint: s3Endpoint,
AmazonS3Region: "",
AmazonS3PathPrefix: "",
AmazonS3SSL: false,
}
fileBackend, err := NewS3FileBackend(cfg)
require.NoError(t, err)
err = fileBackend.MakeBucket()
require.NoError(t, err)
}