class Staticd::Datastores::S3

Datastore storing files on Amazon S3.

It use the file SHA1 digest as a filename so two identical files are not stored twice. Each files can be accessed afterwards using a public HTTP(S) address.

Example:

datastore = Staticd::Datastores::S3.new(
  host: bucket_name,
  username: access_key_id,
  password: secret_access_key
)
datastore.put(file_path) unless datastore.exist?(file_path)
# => http://bucket_name.hostname/sha1_digest

Public Class Methods

new(params) click to toggle source
# File lib/staticd/datastores/s3.rb, line 23
def initialize(params)
  @bucket_name = params[:host]
  @access_key = ENV["AWS_ACCESS_KEY_ID"]
  @secret_key = ENV["AWS_SECRET_ACCESS_KEY"]
end

Public Instance Methods

exist?(file_path) click to toggle source
# File lib/staticd/datastores/s3.rb, line 36
def exist?(file_path)
  s3_object = object(file_path)
  s3_object.exists? ? s3_object.public_url(secure: false) : false
end
put(file_path) click to toggle source
# File lib/staticd/datastores/s3.rb, line 29
def put(file_path)
  s3_object = object(file_path)
  s3_object.write(file: file_path)
  s3_object.acl = :public_read
  s3_object.public_url(secure: false).to_s
end

Private Instance Methods

bucket() click to toggle source
# File lib/staticd/datastores/s3.rb, line 50
def bucket
  @bucket ||= s3.buckets[@bucket_name]
end
object(file_path) click to toggle source
# File lib/staticd/datastores/s3.rb, line 54
def object(file_path)
  bucket.objects[sha1(file_path)]
end
s3() click to toggle source
# File lib/staticd/datastores/s3.rb, line 43
def s3
  @s3 ||= AWS::S3.new(
    access_key_id: @access_key,
    secret_access_key: @secret_key
  )
end
sha1(file_path) click to toggle source
# File lib/staticd/datastores/s3.rb, line 58
def sha1(file_path)
  Digest::SHA1.hexdigest(File.read(file_path))
end