Class: Cosmos::S3Utilities
Class Method Summary collapse
- .ensure_public_bucket(bucket_name) ⇒ Object
- .get_cache_control(filename) ⇒ Object
- .get_total_size_and_oldest_list(bucket, prefix, max_list_length = 10000) ⇒ Object
- .list_files_before_time(bucket, prefix, time) ⇒ Object
- .move_log_file_to_s3(filename, s3_key, metadata: {}) ⇒ Object
Class Method Details
.ensure_public_bucket(bucket_name) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/cosmos/utilities/s3.rb', line 144 def self.ensure_public_bucket(bucket_name) rubys3_client = Aws::S3::Client.new begin rubys3_client.head_bucket(bucket: bucket_name) rescue Aws::S3::Errors::NotFound rubys3_client.create_bucket(bucket: bucket_name) policy = <<~EOL { "Version": "2012-10-17", "Statement": [ { "Action": [ "s3:GetBucketLocation", "s3:ListBucket" ], "Effect": "Allow", "Principal": { "AWS": [ "*" ] }, "Resource": [ "arn:aws:s3:::#{bucket_name}" ], "Sid": "" }, { "Action": [ "s3:GetObject" ], "Effect": "Allow", "Principal": { "AWS": [ "*" ] }, "Resource": [ "arn:aws:s3:::#{bucket_name}/*" ], "Sid": "" } ] } EOL rubys3_client.put_bucket_policy({ bucket: bucket_name, policy: policy }) end end |
.get_cache_control(filename) ⇒ Object
194 195 196 197 198 199 200 |
# File 'lib/cosmos/utilities/s3.rb', line 194 def self.get_cache_control(filename) # Allow caching for files that have a filename versioning strategy has_version_number = /(-|_|\.)\d+(-|_|\.)\d+(-|_|\.)\d+\./.match(@filename) has_content_hash = /\.[a-f0-9]{20}\./.match(@filename) return nil if has_version_number or has_content_hash return 'no-cache' end |
.get_total_size_and_oldest_list(bucket, prefix, max_list_length = 10000) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/cosmos/utilities/s3.rb', line 86 def self.get_total_size_and_oldest_list(bucket, prefix, max_list_length = 10000) rubys3_client = Aws::S3::Client.new oldest_list = [] total_size = 0 # Return nothing if bucket doesn't exist (it won't at the very beginning) begin rubys3_client.head_bucket(bucket: bucket) rescue Aws::S3::Errors::NotFound return total_size, oldest_list end # Get List of Files from S3 token = nil while true resp = rubys3_client.list_objects_v2({ bucket: bucket, max_keys: 1000, prefix: prefix, continuation_token: token }) resp.contents.each do |item| total_size += item.size end oldest_list.concat(resp.contents) oldest_list.sort! { |a, b| File.basename(a.key) <=> File.basename(b.key) } oldest_list = oldest_list[0..(max_list_length - 1)] break unless resp.is_truncated token = resp.next_continuation_token end return total_size, oldest_list end |
.list_files_before_time(bucket, prefix, time) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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/cosmos/utilities/s3.rb', line 25 def self.list_files_before_time(bucket, prefix, time) rubys3_client = Aws::S3::Client.new oldest_list = [] total_size = 0 # Return nothing if bucket doesn't exist (it won't at the very beginning) begin rubys3_client.head_bucket(bucket: bucket) rescue Aws::S3::Errors::NotFound return total_size, oldest_list end # Get List of Packet Names - Assumes prefix gets us to a folder of packet names token = nil folder_list = [] while true resp = rubys3_client.list_objects_v2({ bucket: bucket, max_keys: 1000, prefix: prefix, delimiter: '/', continuation_token: token }) resp.common_prefixes.each do |item| folder_list << item.prefix end break unless resp.is_truncated token = resp.next_continuation_token end # Go through each folder and keep files that end before time folder_list.each do |folder| token = nil next_folder = false while true resp = rubys3_client.list_objects_v2({ bucket: bucket, max_keys: 1000, prefix: folder, continuation_token: token }) resp.contents.each do |item| t = item.key.split('__')[1] file_end_time = Time.utc(t[0..3], t[4..5], t[6..7], t[8..9], t[10..11], t[12..13]) if file_end_time < time oldest_list << item total_size += item.size else next_folder = true break end end break if !resp.is_truncated or next_folder token = resp.next_continuation_token end end return total_size, oldest_list end |
.move_log_file_to_s3(filename, s3_key, metadata: {}) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/cosmos/utilities/s3.rb', line 120 def self.move_log_file_to_s3(filename, s3_key, metadata: {}) Thread.new do rubys3_client = Aws::S3::Client.new # Ensure logs bucket exists begin rubys3_client.head_bucket(bucket: 'logs') rescue Aws::S3::Errors::NotFound rubys3_client.create_bucket(bucket: 'logs') end # Write to S3 Bucket File.open(filename, 'rb') do |read_file| rubys3_client.put_object(bucket: 'logs', key: s3_key, body: read_file, metadata: ) end Logger.debug "logs/#{s3_key} written to S3" ReducerModel.add_file(s3_key) # Record the new file for data reduction File.delete(filename) rescue => err Logger.error("Error saving log file to bucket: #{filename}\n#{err.formatted}") end end |