Class: S3Uploader::S3BucketManager

Inherits:
Object
  • Object
show all
Defined in:
app/s3_uploader/lib/s3_bucket_manager.rb

Class Method Summary collapse

Class Method Details

.ensure_bucket(bucket_name) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/s3_uploader/lib/s3_bucket_manager.rb', line 3

def self.ensure_bucket(bucket_name)
  prop_key = "s3_bucket_#{bucket_name}"
  unless Volt.current_app.properties[prop_key]
    Volt.logger.info("Creating/Setting up S3 Bucket: #{bucket_name}")

    creds = Aws::Credentials.new(Volt.config.s3.key, Volt.config.s3.secret)
    s3 = Aws::S3::Resource.new(region: 'us-east-1', credentials: creds)

    s3.create_bucket(
      {
        acl: "private", # accepts private, public-read, public-read-write, authenticated-read
        bucket: bucket_name, # required
        # create_bucket_configuration: {
        #   location_constraint: "us-east-1", # accepts EU, eu-west-1, us-west-1, us-west-2, ap-southeast-1, ap-southeast-2, ap-northeast-1, sa-east-1, cn-north-1, eu-central-1
        # },
        # grant_full_control: "GrantFullControl",
        # grant_read: "GrantRead",
        # grant_read_acp: "GrantReadACP",
        # grant_write: "GrantWrite",
        # grant_write_acp: "GrantWriteACP",
      }
    )

    bucket = s3.bucket(bucket_name)

    bucket.cors.put(
      {
        bucket: bucket_name,
        cors_configuration: {
          cors_rules: [
            {
              allowed_origins: ["*"],
              allowed_methods: ["GET", "POST", "PUT"], # required
              allowed_headers: ["*"]
            },
          ],
        }
      }
    )

    Volt.current_app.properties[prop_key] = '1'
  end
end