Class: Staticd::Datastores::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/staticd/datastores/s3.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ S3

Returns a new instance of S3.



23
24
25
26
27
# 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

Instance Method Details

#exist?(file_path) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
# 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) ⇒ Object



29
30
31
32
33
34
# 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