Class: SitemapGenerator::S3Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/sitemap_generator/adapters/s3_adapter.rb

Overview

Class for uploading sitemaps to an S3 bucket using the Fog gem.

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ S3Adapter

Requires Fog::Storage to be defined.

Alternatively you can use an environment variable to configure each option (except fog_storage_options). The environment variables have the same name but capitalized, e.g. FOG_PATH_STYLE.

Parameters:

  • opts (Hash) (defaults to: {})

    Fog configuration options

  • :aws_access_key_id (Hash)

    a customizable set of options

  • :aws_secret_access_key (Hash)

    a customizable set of options

  • :fog_provider (Hash)

    a customizable set of options

  • :fog_directory (Hash)

    a customizable set of options

  • :fog_region (Hash)

    a customizable set of options

  • :fog_path_style (Hash)

    a customizable set of options

  • :fog_storage_options (Hash)

    a customizable set of options

  • :fog_public (Hash)

    a customizable set of options



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sitemap_generator/adapters/s3_adapter.rb', line 25

def initialize(opts = {}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  @aws_access_key_id = opts[:aws_access_key_id] || ENV.fetch('AWS_ACCESS_KEY_ID', nil)
  @aws_secret_access_key = opts[:aws_secret_access_key] || ENV.fetch('AWS_SECRET_ACCESS_KEY', nil)
  @aws_session_token = opts[:aws_session_token] || ENV.fetch('AWS_SESSION_TOKEN', nil)
  @fog_provider = opts[:fog_provider] || ENV.fetch('FOG_PROVIDER', nil)
  @fog_directory = opts[:fog_directory] || ENV.fetch('FOG_DIRECTORY', nil)
  @fog_region = opts[:fog_region] || ENV.fetch('FOG_REGION', nil)
  @fog_path_style = opts[:fog_path_style] || ENV.fetch('FOG_PATH_STYLE', nil)
  @fog_storage_options = opts[:fog_storage_options] || {}
  fog_public = opts[:fog_public].nil? ? ENV.fetch('FOG_PUBLIC', nil) : opts[:fog_public]
  @fog_public = !SitemapGenerator::Utilities.falsy?(fog_public)
end

Instance Method Details

#write(location, raw_data) ⇒ Object

Call with a SitemapLocation and string data



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
# File 'lib/sitemap_generator/adapters/s3_adapter.rb', line 39

def write(location, raw_data) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  SitemapGenerator::FileAdapter.new.write(location, raw_data)

  credentials = { provider: @fog_provider }

  if @aws_access_key_id && @aws_secret_access_key
    credentials[:aws_access_key_id] = @aws_access_key_id
    credentials[:aws_secret_access_key] = @aws_secret_access_key
    credentials[:aws_session_token] = @aws_session_token if @aws_session_token
  else
    credentials[:use_iam_profile] = true
  end

  credentials[:region] = @fog_region if @fog_region
  credentials[:path_style] = @fog_path_style if @fog_path_style

  storage   = Fog::Storage.new(@fog_storage_options.merge(credentials))
  directory = storage.directories.new(key: @fog_directory)
  directory.files.create(
    key: location.path_in_public,
    body: File.open(location.path),
    public: @fog_public,
    content_type: location.content_type
  )
end