Class: WebpackerUploader::Providers::Aws

Inherits:
Object
  • Object
show all
Defined in:
lib/webpacker_uploader/providers/aws.rb

Overview

AWS provider uploads files to AWS S3. It uses the +aws-sdk-s3+ gem.

Defined Under Namespace

Classes: CredentialsError

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Aws

Note:

Any unknown options will be passed directly to the +Aws::S3::Client+ class during initialization.

Returns a new instance of Aws.

Examples:

Initialize using a named profile:


provider_options = {
  credentials: { profile_name: "staging" },
  region: "eu-central-1",
  bucket: "application-assets-20200929124523451600000001"
}
provider = WebpackerUploader::Providers::Aws.new(provider_options)

Initialize using IAM keys


provider_options = {
  credentials: { access_key_id: "KEY_ID", secret_access_key: "ACCESS_KEY" },
  region: "eu-central-1",
  bucket: "application-assets-20200929124523451600000001"
}
provider = WebpackerUploader::Providers::Aws.new(provider_options)

Initialize using an EC2 instance profile


provider_options = {
  credentials: { instance_profile: true },
  region: "eu-central-1",
  bucket: "application-assets-20200929124523451600000001"
}
provider = WebpackerUploader::Providers::Aws.new(provider_options)

Parameters:

  • options (Hash)
    • :region (String) The S3 region name.
    • :bucket (String) The S3 bucket name.
    • :credentials (Hash) credential options for the AWS provider:
      • :profile_name (String) use a named profile configured in ~/.aws/credentials
      • :instance_profile (Boolean) use an instance profile from an EC2
      • :access_key_id (String) the AWS credentials access id.
      • :secret_access_key (String) the AWS credentials secret access key.

Raises:



53
54
55
56
57
58
59
# File 'lib/webpacker_uploader/providers/aws.rb', line 53

def initialize(options)
  @region      = options.delete(:region)
  @bucket_name = options.delete(:bucket)
  @credentials = credentials(options.delete(:credentials))
  @aws_options = options
  @resource    = ::Aws::S3::Resource.new(client: client)
end

Instance Method Details

#upload!(object_key, file, content_type = "", cache_control = "")

This method returns an undefined value.

Uploads a file to AWS S3.

Parameters:

  • object_key (String)

    Is the remote path name for the S3 object.

  • file (Pathname)

    Path of the local file.

  • content_type (String) (defaults to: "")

    The content type that will be set to the S3 object.



67
68
69
70
# File 'lib/webpacker_uploader/providers/aws.rb', line 67

def upload!(object_key, file, content_type = "", cache_control = "")
  object = @resource.bucket(@bucket_name).object(object_key)
  object.upload_file(file, content_type: content_type, cache_control: cache_control)
end