Check first if minio bucket exists before trying to create it (#26420) (#26465)

Backport #26420 by @lunny

For some reason, the permission of the client_id and secret may cannot
create bucket, so now we will check whether bucket does exist first and
then try to create a bucket if it doesn't exist.

Try to fix #25984

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
(cherry picked from commit 2d1202b32c1a975fe6cf7b139d1ab4ed292bcca1)
This commit is contained in:
Giteabot 2023-08-12 17:00:51 +08:00 committed by Earl Warren
parent a12ea7a7cd
commit 82e5247a43
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00

View file

@ -90,12 +90,16 @@ func NewMinioStorage(ctx context.Context, cfg *setting.Storage) (ObjectStorage,
return nil, convertMinioErr(err) return nil, convertMinioErr(err)
} }
if err := minioClient.MakeBucket(ctx, config.Bucket, minio.MakeBucketOptions{ // Check to see if we already own this bucket
Region: config.Location, exists, errBucketExists := minioClient.BucketExists(ctx, config.Bucket)
}); err != nil { if errBucketExists != nil {
// Check to see if we already own this bucket (which happens if you run this twice) return nil, convertMinioErr(err)
exists, errBucketExists := minioClient.BucketExists(ctx, config.Bucket) }
if !exists || errBucketExists != nil {
if !exists {
if err := minioClient.MakeBucket(ctx, config.Bucket, minio.MakeBucketOptions{
Region: config.Location,
}); err != nil {
return nil, convertMinioErr(err) return nil, convertMinioErr(err)
} }
} }