From 842cd58e8e0eb1011dd178d2cc23425e5d3768a0 Mon Sep 17 00:00:00 2001 From: Krisztian Banhidy Date: Tue, 10 Feb 2026 15:54:29 +0300 Subject: [PATCH] Check if buckets exists or not --- pkg/config/s3.go | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/pkg/config/s3.go b/pkg/config/s3.go index cce15a3a..a0c889ce 100644 --- a/pkg/config/s3.go +++ b/pkg/config/s3.go @@ -116,26 +116,34 @@ func (s *S3Info) awsUpload(ctx context.Context, bucket, key, asset string, rwc i } clt := s3.NewFromConfig(cfg) - opts := s3.CreateBucketInput{ - Bucket: &bucket, - CreateBucketConfiguration: &types.CreateBucketConfiguration{ - LocationConstraint: types.BucketLocationConstraint(*s.Region), - }, - } - if _, err = clt.CreateBucket(ctx, &opts); err != nil { - var ( - exists *types.BucketAlreadyExists - owned *types.BucketAlreadyOwnedByYou - ) - switch { - case errors.As(err, &exists): - log.Info().Msgf("bucket %s already exists", bucket) - case errors.As(err, &owned): - log.Info().Msgf("bucket %s already owned by you", bucket) - default: - log.Err(err).Msgf("failed to create bucket %s", bucket) - return err + _, err = clt.HeadBucket(ctx, &s3.HeadBucketInput{Bucket: &bucket}) + if err != nil { + log.Debug().Msgf("bucket %s not found or not accessible, attempting to create", bucket) + opts := s3.CreateBucketInput{ + Bucket: &bucket, + CreateBucketConfiguration: &types.CreateBucketConfiguration{ + LocationConstraint: types.BucketLocationConstraint(*s.Region), + }, } + if _, err = clt.CreateBucket(ctx, &opts); err != nil { + var ( + exists *types.BucketAlreadyExists + owned *types.BucketAlreadyOwnedByYou + ) + switch { + case errors.As(err, &exists): + log.Info().Msgf("bucket %s already exists", bucket) + case errors.As(err, &owned): + log.Info().Msgf("bucket %s already owned by you", bucket) + default: + log.Err(err).Msgf("failed to create bucket %s", bucket) + return err + } + } else { + log.Info().Msgf("successfully created bucket %s", bucket) + } + } else { + log.Info().Msgf("bucket %s already exists", bucket) } uploader := manager.NewUploader(clt)