Class: Jets::AwsServices::S3Bucket

Inherits:
Object
  • Object
show all
Includes:
Jets::AwsServices
Defined in:
lib/jets/aws_services/s3_bucket.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Jets::AwsServices

#apigateway, #aws_options, #cfn, #codebuild, #dynamodb, #lambda_client, #logs, #s3, #s3_resource, #sns, #sqs, #ssm, #sts, #wafv2

Methods included from StackStatus

#output_value, #stack_exists?

Methods included from GlobalMemoist

included, #reset_cache!

Constructor Details

#initialize(name) ⇒ S3Bucket

Returns a new instance of S3Bucket.



9
10
11
# File 'lib/jets/aws_services/s3_bucket.rb', line 9

def initialize(name)
  @name = name
end

Class Method Details

.ensure_exists(bucket_name) ⇒ Object



5
6
7
# File 'lib/jets/aws_services/s3_bucket.rb', line 5

def self.ensure_exists(bucket_name)
  new(bucket_name).ensure_exists
end

Instance Method Details

#ensure_existsObject



13
14
15
16
17
18
19
# File 'lib/jets/aws_services/s3_bucket.rb', line 13

def ensure_exists
  s3.create_bucket(bucket: @name) unless exists?
rescue Aws::S3::Errors::BucketAlreadyExists => e
  puts "ERROR #{e.class}: #{e.message}".color(:red)
  puts "Bucket name: #{@name}"
  exit 1
end

#exists?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
# File 'lib/jets/aws_services/s3_bucket.rb', line 21

def exists?
  s3.head_bucket(bucket: @name)
  true
rescue Aws::S3::Errors::BucketAlreadyOwnedByYou, Aws::S3::Errors::Http301Error => e
  # These exceptions indicate bucket already exists
  # Aws::S3::Errors::Http301Error could be inaccurate but compromising for simplicity
  true
rescue
  false
end