File: fog_helpers.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (51 lines) | stat: -rw-r--r-- 1,415 bytes parent folder | download
1
2
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
46
47
48
49
50
51
# frozen_string_literal: true

module ObjectStorage
  module FogHelpers
    include ::Gitlab::Utils::StrongMemoize

    def available?
      object_store.enabled
    end

    private

    def delete_object(key)
      return unless available?

      connection.delete_object(bucket_name, object_key(key))

    # So far, only GoogleCloudStorage raises an exception when the file is not found.
    # Other providers support idempotent requests and does not raise an error
    # when the file is missing.
    rescue ::Google::Apis::ClientError => e
      Gitlab::ErrorTracking.log_exception(e)
    end

    def storage_location_identifier
      raise NotImplementedError, "#{self} does not implement #{__method__}"
    end

    def object_store
      ObjectStorage::Config::LOCATIONS.fetch(storage_location_identifier).object_store
    end

    def bucket_name
      object_store.remote_directory
    end

    def object_key(key)
      # We allow administrators to create "sub buckets" by setting a prefix.
      # This makes it possible to deploy GitLab with only one object storage
      # bucket. This mirrors the implementation in app/uploaders/object_storage.rb.
      File.join([object_store.bucket_prefix, key].compact)
    end

    def connection
      return unless available?

      ::Fog::Storage.new(object_store.connection.to_hash.deep_symbolize_keys)
    end
    strong_memoize_attr :connection
  end
end