Class: OpenC3::AwsBucket

Inherits:
Bucket show all
Defined in:
lib/openc3/utilities/aws_bucket.rb

Direct Known Subclasses

LocalBucket

Constant Summary collapse

CREATE_CHECK_COUNT =

10 seconds

100

Instance Method Summary collapse

Methods inherited from Bucket

getClient

Constructor Details

#initializeAwsBucket

Returns a new instance of AwsBucket.



28
29
30
# File 'lib/openc3/utilities/aws_bucket.rb', line 28

def initialize
  @client = Aws::S3::Client.new
end

Instance Method Details

#check_object(bucket:, key:) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/openc3/utilities/aws_bucket.rb', line 159

def check_object(bucket:, key:)
  @client.wait_until(:object_exists,
    {
      bucket: bucket,
      key: key
    },
    {
      max_attempts: 30,
      delay: 0.1, # seconds
    }
  )
  true
rescue Aws::Waiters::Errors::TooManyAttemptsError
  false
end

#create(bucket) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/openc3/utilities/aws_bucket.rb', line 32

def create(bucket)
  unless exist?(bucket)
    @client.create_bucket({ bucket: bucket })
    count = 0
    until exist?(bucket) or count > CREATE_CHECK_COUNT
      sleep(0.1)
      count += 1
    end
  end
  bucket
end

#delete(bucket) ⇒ Object



93
94
95
96
97
# File 'lib/openc3/utilities/aws_bucket.rb', line 93

def delete(bucket)
  if exist?(bucket)
    @client.delete_bucket({ bucket: bucket })
  end
end

#delete_object(bucket:, key:) ⇒ Object



175
176
177
# File 'lib/openc3/utilities/aws_bucket.rb', line 175

def delete_object(bucket:, key:)
  @client.delete_object(bucket: bucket, key: key)
end

#delete_objects(bucket:, keys:) ⇒ Object



179
180
181
# File 'lib/openc3/utilities/aws_bucket.rb', line 179

def delete_objects(bucket:, keys:)
  @client.delete_objects(bucket: bucket, delete: { objects: keys.map {|key| { key: key } } })
end

#ensure_public(bucket) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/openc3/utilities/aws_bucket.rb', line 44

def ensure_public(bucket)
  policy = <<~EOL
  {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": [
          "s3:GetBucketLocation",
          "s3:ListBucket"
        ],
        "Effect": "Allow",
        "Principal": {
          "AWS": [
            "*"
          ]
        },
        "Resource": [
          "arn:aws:s3:::#{bucket}"
        ],
        "Sid": ""
      },
      {
        "Action": [
          "s3:GetObject"
        ],
        "Effect": "Allow",
        "Principal": {
          "AWS": [
            "*"
          ]
        },
        "Resource": [
          "arn:aws:s3:::#{bucket}/*"
        ],
        "Sid": ""
      }
    ]
  }
  EOL
  @client.put_bucket_policy({ bucket: bucket, policy: policy })
end

#exist?(bucket) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
# File 'lib/openc3/utilities/aws_bucket.rb', line 86

def exist?(bucket)
  @client.head_bucket({ bucket: bucket })
  true
rescue Aws::S3::Errors::NotFound
  false
end

#get_object(bucket:, key:, path: nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/openc3/utilities/aws_bucket.rb', line 99

def get_object(bucket:, key:, path: nil)
  if path
    @client.get_object(bucket: bucket, key: key, response_target: path)
  else
    @client.get_object(bucket: bucket, key: key)
  end
# If the key is not found return nil
rescue Aws::S3::Errors::NoSuchKey
  nil
end

#list_directories(bucket:, path:) ⇒ Object

Lists the directories under a specified path



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/openc3/utilities/aws_bucket.rb', line 125

def list_directories(bucket:, path:)
  # Trailing slash is important in AWS S3 when listing files
  # See https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Types/ListObjectsV2Output.html#common_prefixes-instance_method
  if path[-1] != '/'
    path += '/'
  end
  token = nil
  result = []
  while true
    resp = @client.list_objects_v2({
      bucket: bucket,
      max_keys: 1000,
      prefix: path,
      delimiter: '/',
      continuation_token: token
    })
    resp.common_prefixes.each do |item|
      # If path was DEFAULT/targets_modified/ then the
      # results look like DEFAULT/targets_modified/INST/
      result << item.prefix.split('/')[-1]
    end
    break unless resp.is_truncated
    token = resp.next_continuation_token
  end
  result
end

#list_objects(bucket:, prefix: nil, max_request: 1000, max_total: 100_000) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/openc3/utilities/aws_bucket.rb', line 110

def list_objects(bucket:, prefix: nil, max_request: 1000, max_total: 100_000)
  token = nil
  result = []
  while true
    resp = @client.list_objects_v2(bucket: bucket, prefix: prefix, max_keys: max_request)
    result.concat(resp.contents)
    break if result.length >= max_total
    break unless resp.is_truncated
    token = resp.next_continuation_token
  end
  # Array of objects with key and size methods
  result
end

#presigned_request(bucket:, key:, method:, internal: true) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/openc3/utilities/aws_bucket.rb', line 183

def presigned_request(bucket:, key:, method:, internal: true)
  s3_presigner = Aws::S3::Presigner.new

  if internal
    prefix = '/'
  else
    prefix = '/files/'
  end

  url, headers = s3_presigner.presigned_request(method, bucket: bucket, key: key)
  return {
    :url => prefix + url.split('/')[3..-1].join('/'),
    :headers => headers,
    :method => method.to_s.split('_')[0],
  }
end

#put_object(bucket:, key:, body:, content_type: nil, cache_control: nil, metadata: nil) ⇒ Object

put_object fires off the request to store but does not confirm



153
154
155
156
# File 'lib/openc3/utilities/aws_bucket.rb', line 153

def put_object(bucket:, key:, body:, content_type: nil, cache_control: nil, metadata: nil)
  @client.put_object(bucket: bucket, key: key, body: body,
    content_type: content_type, cache_control: cache_control, metadata: )
end