Class: SiteMaps::Adapters::AwsSdk::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/site_maps/adapters/aws_sdk/storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Storage

Returns a new instance of Storage.



6
7
8
# File 'lib/site_maps/adapters/aws_sdk/storage.rb', line 6

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/site_maps/adapters/aws_sdk/storage.rb', line 4

def config
  @config
end

Instance Method Details

#delete(location) ⇒ Object



35
36
37
38
39
# File 'lib/site_maps/adapters/aws_sdk/storage.rb', line 35

def delete(location)
  object(location.remote_path).delete
rescue Aws::S3::Errors::NoSuchKey
  raise SiteMaps::FileNotFoundError, "File not found: #{location.remote_path}"
end

#read(location) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/site_maps/adapters/aws_sdk/storage.rb', line 21

def read(location)
  obj = object(location.remote_path).get
   = {
    content_type: obj.content_type
  }
  if (raw = obj.["given-last-modified"]) &&
      (time = Time.parse(raw))
    [:last_modified] = time
  end
  [obj.body.read, ]
rescue Aws::S3::Errors::NoSuchKey
  raise SiteMaps::FileNotFoundError, "File not found: #{location.remote_path}"
end

#upload(location, **options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/site_maps/adapters/aws_sdk/storage.rb', line 10

def upload(location, **options)
  options[:acl] ||= config.acl if config.acl
  options[:cache_control] ||= config.cache_control if config.cache_control
  options[:content_type] ||= location.gzip? ? "application/gzip" : "application/xml"
  lastmod = options.delete(:last_modified) || Time.now
  options[:metadata] ||= {}
  options[:metadata]["given-last-modified"] = lastmod.utc.strftime("%Y-%m-%dT%H:%M:%S%:z")
  obj = object(location.remote_path)
  obj.upload_file(location.path, **options)
end