From 56c71e3f7e7ede48fb5964ebc2a3195d22b8c1cf Mon Sep 17 00:00:00 2001 From: bpeng Date: Wed, 27 Nov 2024 12:55:33 +1300 Subject: [PATCH] add functions CheckQueueExists CheckBucketExists --- aws/s3/s3.go | 10 ++++++++++ aws/sqs/sqs.go | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/aws/s3/s3.go b/aws/s3/s3.go index 1cd6baf..c418c31 100644 --- a/aws/s3/s3.go +++ b/aws/s3/s3.go @@ -271,6 +271,16 @@ func (s *S3) PutWithMetadata(bucket, key string, object []byte, metadata Meta) e return err } +// checks if the given S3 bucket exists. +func (s *S3) CheckBucketExists(bucketName string) error { + // Check if the bucket exists. + _, err := s.client.HeadBucket(context.TODO(), &s3.HeadBucketInput{ + Bucket: aws.String(bucketName), + }) + + return err +} + // Exists checks if an object for key already exists in the bucket. func (s *S3) Exists(bucket, key string) (bool, error) { diff --git a/aws/sqs/sqs.go b/aws/sqs/sqs.go index bd7f21c..d02fac0 100644 --- a/aws/sqs/sqs.go +++ b/aws/sqs/sqs.go @@ -320,6 +320,16 @@ func (s *SQS) CreateQueue(queueName string, isFifoQueue bool) (string, error) { return aws.ToString(queue.QueueUrl), err } +// checks if the given SQS queue exists. +func (s *SQS) CheckQueueExists(queueName string) error { + // Check if the queue exists. + _, err := s.client.GetQueueUrl(context.TODO(), &sqs.GetQueueUrlInput{ + QueueName: aws.String(queueName), + }) + + return err +} + // DeleteQueue deletes an Amazon SQS queue. func (s *SQS) DeleteQueue(queueUrl string) error { _, err := s.client.DeleteQueue(context.TODO(), &sqs.DeleteQueueInput{