Module: Paperclip::Storage::OracleObjectStorage

Defined in:
lib/paperclip/storage/oracle_object_storage.rb,
lib/paperclip/storage/oracle_object_storage/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



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
# File 'lib/paperclip/storage/oracle_object_storage.rb', line 6

def self.extended(base)
  begin
    require "oci"
  rescue LoadError => e
    raise("#{e.message} (You may need to install the oci gem)")
  end

  base.instance_eval do
    #TODO: Load config by file.
    # if oci_credentials[:config_file]
    #   @config = OCI::ConfigFileLoader.load_config(
    #         oci_credentials[:config_file],
    #         oci_credentials[:profile] || "DEFAULT"
    #       )
    # end
    @config = OCI::Config.new

    @config.user = oci_credentials[:user]
    @config.fingerprint = oci_credentials[:fingerprint]
    @config.tenancy = oci_credentials[:tenancy]
    @config.region = oci_credentials[:region]
    @config.key_file = oci_credentials[:key_file]


    @bucket_name = oci_credentials[:bucket_name]
    @options[:path] = path_option.gsub(/:url/, @options[:url]).sub(/\A:rails_root\/public\/system/, "")
    @options[:url] = @options[:url].inspect if @options[:url].is_a?(Symbol)
  end
end

Instance Method Details

#bucket_nameObject



40
41
42
# File 'lib/paperclip/storage/oracle_object_storage.rb', line 40

def bucket_name
  @bucket_name
end

#copy_to_local_file(style, local_dest_path) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/paperclip/storage/oracle_object_storage.rb', line 140

def copy_to_local_file(style, local_dest_path)
  log("copying #{path(style)} to #{local_dest_path}")

  response =
    object_storage_client.get_object(
      namespace,
      bucket_name,
      object_name(style)
    )

  File.binwrite(local_dest_path, response.data)
rescue OCI::Errors::ServiceError => e
  warn("#{e} - cannot copy #{path(style)}")
  false
end

#create_bucketObject



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/paperclip/storage/oracle_object_storage.rb', line 156

def create_bucket
  details =
    OCI::ObjectStorage::Models::CreateBucketDetails.new(
      name: bucket_name,
      compartment_id: compartment_id
    )

  object_storage_client.create_bucket(
    namespace,
    details
  )
end

#exists?(style = default_style) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/paperclip/storage/oracle_object_storage.rb', line 63

def exists?(style = default_style)
  return false unless original_filename

  object_storage_client.head_object(
    namespace,
    bucket_name,
    object_name(style)
  )

  true
rescue OCI::Errors::ServiceError => e
  return false if e.status_code == 404

  raise
end

#expiring_url(time = 3600, style = default_style) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/paperclip/storage/oracle_object_storage.rb', line 79

def expiring_url(time = 3600, style = default_style)
  expires_at = Time.now.utc + time

  details =
    OCI::ObjectStorage::Models::CreatePreauthenticatedRequestDetails.new(
      name: "paperclip-#{SecureRandom.hex(8)}",
      object_name: object_name(style),
      access_type: "ObjectRead",
      time_expires: expires_at
    )

  response =
    object_storage_client.create_preauthenticated_request(
      namespace,
      bucket_name,
      details
    )

  endpoint = object_storage_client.endpoint

  "#{endpoint.chomp("/")}#{response.data.access_uri}"
end

#flush_deletesObject

:nodoc:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/paperclip/storage/oracle_object_storage.rb', line 123

def flush_deletes #:nodoc:
  @queued_for_delete.each do |path|
    begin
      log("deleting #{path}")
      object_storage_client.delete_object(
        object_storage_client.get_namespace.data,
        bucket_name,
        object_name
      )
    rescue
      log("error deleting #{path}")
      raise e
    end
  end
  @queued_for_delete = []
end

#flush_writesObject

:nodoc:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/paperclip/storage/oracle_object_storage.rb', line 102

def flush_writes #:nodoc:
  @queued_for_write.each do |style, file|
    begin
      log("saving to Oracle object storage #{path(style)}")
      object_storage_client.put_object(
        object_storage_client.get_namespace.data,
        bucket_name,
        object_name(style),
        File.open(file.path),
        {
          content_type: file.content_type
        }
      )
    rescue => e
      log("error saving to Oracle object storage #{path(style)}")
      raise e
    end
  end
  @queued_for_write = {}
end

#namespaceObject



44
45
46
47
48
49
50
51
52
# File 'lib/paperclip/storage/oracle_object_storage.rb', line 44

def namespace
  @namespace ||= begin
    configured = @options[:namespace] || oci_credentials[:namespace]

    return configured if configured.present?

    object_storage_client.get_namespace.data
  end
end

#object_name(style = default_style) ⇒ Object



59
60
61
# File 'lib/paperclip/storage/oracle_object_storage.rb', line 59

def object_name(style = default_style)
  path(style).sub(%r{\A/}, "").unicode_normalize(:nfc).then { |p| I18n.transliterate(p) }
end

#object_storage_clientObject



54
55
56
57
# File 'lib/paperclip/storage/oracle_object_storage.rb', line 54

def object_storage_client
  # Doc of OCI client: https://docs.oracle.com/en-us/iaas/tools/ruby/2.22.0/OCI/ObjectStorage/ObjectStorageClient.html#put_object_lifecycle_policy-instance_method
  @object_storage_client ||= OCI::ObjectStorage::ObjectStorageClient.new(config: @config)
end

#oci_credentialsObject



36
37
38
# File 'lib/paperclip/storage/oracle_object_storage.rb', line 36

def oci_credentials
  @oci_credentials ||= parse_credentials(@options[:oci_credentials])
end