Class: IiifS3::AmazonS3

Inherits:
Object
  • Object
show all
Defined in:
lib/iiif_s3/amazon_s3.rb

Overview

Class AmazonS3 wraps the functionality needed up upload files to Amazon S3.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Void

Intitializing an AmazonS3 instance will verify that the required ENV keys exist (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_BUCKET_NAME, and AWS_REGION), verify that the bucket exists, and then connect to that bucket.

It will also set the CORS rules to allow cross-domain access to that bucket, as well as configuring the bucket as a website.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/iiif_s3/amazon_s3.rb', line 24

def initialize(options = {})
  defaults = {
    verbose: false
  }
  @options = defaults.merge(options)
  if ENV['AWS_ACCESS_KEY_ID'].nil?
    raise IiifS3::Error::BadAmazonCredentials, "Could not find the correct Access Key ID in ENV['AWS_ACCESS_KEY_ID']"
  elsif ENV['AWS_SECRET_ACCESS_KEY'].nil?
    raise IiifS3::Error::BadAmazonCredentials, "Could not find the correct secret in ENV['AWS_SECRET_ACCESS_KEY']"
  elsif ENV['AWS_BUCKET_NAME'].nil?
    raise IiifS3::Error::BadAmazonCredentials, "Could not find a bucket name in ENV['AWS_BUCKET_NAME']"
  elsif ENV['AWS_REGION'].nil?
    raise IiifS3::Error::BadAmazonCredentials, "Could not find a AWS region in ENV['AWS_REGION']"
  end

  @bucket = Aws::S3::Bucket.new(ENV['AWS_BUCKET_NAME'])
  unless @bucket.exists?
    raise IiifS3::Error::BadAmazonCredentials, "The bucket name in ENV['AWS_BUCKET_NAME'] does not exist.  You supplied '#{ENV['AWS_BUCKET_NAME']}'" 
  end
  @bucket.cors.put(cors_rules)
  @bucket.website.put(website_rules)
  return nil
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



11
12
13
# File 'lib/iiif_s3/amazon_s3.rb', line 11

def bucket
  @bucket
end

Instance Method Details

#add_image(key, filename) ⇒ Hash

A helper method for uploading a JPEG file to S3.

Parameters:

  • key (String)

    The ID that the file will be uploaded to. Typically, this is the path of the URI, without the domain or port.

  • filename (String)

    The path to a JPEG file on the local file system that should be uploaded.

Returns:

  • (Hash)

    The completed options hash.



107
108
109
110
# File 'lib/iiif_s3/amazon_s3.rb', line 107

def add_image(key, filename)
  obj = {content_type:  "image/jpeg"}
  upload_file(key, filename, obj)
end

#add_json(key, filename) ⇒ Hash

A helper method for uploading a JSON file to S3.

Parameters:

  • key (String)

    The ID that the file will be uploaded to. Typically, this is the path of the URI, without the domain or port.

  • filename (String)

    The path to a JSON file on the local file system that should be uploaded.

Returns:

  • (Hash)

    The completed options hash.



91
92
93
94
# File 'lib/iiif_s3/amazon_s3.rb', line 91

def add_json(key, filename)
  obj = {content_type: "application/json"}
  upload_file(key, filename, obj)
end

#add_redirect(key, redirect_key) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/iiif_s3/amazon_s3.rb', line 112

def add_redirect(key, redirect_key) 
   obj = {
      acl: "public-read",
      key: key,
      website_redirect_location: redirect_key
    }
  bucket.put_object(obj)
end

#upload_file(key, filename, options = {}) ⇒ Hash

Upload a file to the default bucket. By default, this will upload a file with public-read permissions, and any standard metadata. passing in an options hash as the last parameter allows one to add or overwrite any of the default parameters.

append to the default parameters.

Parameters:

  • key (String)

    The ID that the file will be uploaded to. Typically, this is the path of the URI, without the domain or port.

  • filename (String)

    The path on the local file system to the file that should be uploaded

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

    Any values that should be uploaded to override or

Returns:

  • (Hash)

    The completed options hash.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/iiif_s3/amazon_s3.rb', line 63

def upload_file(key, filename, options = {})
  obj = {
      acl: "public-read",
      key: key
      # metadata: {
      #   "MetadataKey" => "MetadataValue",
      # },
  }
  obj.merge!(options)

  puts "uploading #{filename} to #{key}" if @options[:verbose]
  File.open(filename,'rb') do |source_file|
    obj[:body] = source_file
    bucket.put_object(obj)
  end
  return obj
end