Class: BucketStore::Gcs
- Inherits:
-
Object
- Object
- BucketStore::Gcs
- Defined in:
- lib/bucket_store/gcs.rb
Constant Summary collapse
- DEFAULT_TIMEOUT_SECONDS =
30
Class Method Summary collapse
Instance Method Summary collapse
- #delete!(bucket:, key:) ⇒ Object
- #download(bucket:, key:, file:) ⇒ Object
-
#initialize(timeout_seconds) ⇒ Gcs
constructor
A new instance of Gcs.
- #list(bucket:, key:, page_size:) ⇒ Object
- #upload!(bucket:, key:, file:) ⇒ Object
Constructor Details
#initialize(timeout_seconds) ⇒ Gcs
Returns a new instance of Gcs.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/bucket_store/gcs.rb', line 16 def initialize(timeout_seconds) # Ruby's GCS library does not natively support setting up a simulator, but it allows # for a specific endpoint to be passed down which has the same effect. The simulator # needs to be special cased as in that case we want to bypass authentication, # which we can only do by accessing the `.anonymous` version of the Storage class. simulator_endpoint = ENV["STORAGE_EMULATOR_HOST"] is_simulator = !simulator_endpoint.nil? args = { endpoint: simulator_endpoint, timeout: timeout_seconds, }.compact @storage = if is_simulator Google::Cloud::Storage.anonymous(**args) else Google::Cloud::Storage.new(**args) end end |
Class Method Details
.build(timeout_seconds = DEFAULT_TIMEOUT_SECONDS) ⇒ Object
12 13 14 |
# File 'lib/bucket_store/gcs.rb', line 12 def self.build(timeout_seconds = DEFAULT_TIMEOUT_SECONDS) Gcs.new(timeout_seconds) end |
Instance Method Details
#delete!(bucket:, key:) ⇒ Object
73 74 75 76 77 |
# File 'lib/bucket_store/gcs.rb', line 73 def delete!(bucket:, key:) get_bucket(bucket).file(key).delete true end |
#download(bucket:, key:, file:) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/bucket_store/gcs.rb', line 45 def download(bucket:, key:, file:) file.tap do |f| get_bucket(bucket). file(key). download(f) f.rewind end end |
#list(bucket:, key:, page_size:) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/bucket_store/gcs.rb', line 55 def list(bucket:, key:, page_size:) Enumerator.new do |yielder| token = nil loop do page = get_bucket(bucket).files(prefix: key, max: page_size, token: token) yielder.yield({ bucket: bucket, keys: page.map(&:name), }) break if page.token.nil? token = page.token end end end |
#upload!(bucket:, key:, file:) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/bucket_store/gcs.rb', line 36 def upload!(bucket:, key:, file:) get_bucket(bucket).create_file(file, key) { bucket: bucket, key: key, } end |