Skip to content

Instantly share code, notes, and snippets.

@clemens
Created September 5, 2014 09:33
Show Gist options
  • Select an option

  • Save clemens/f0a087d3c7229a531f42 to your computer and use it in GitHub Desktop.

Select an option

Save clemens/f0a087d3c7229a531f42 to your computer and use it in GitHub Desktop.
class FogFile
def initialize(path)
@path = path
end
def url
directory = connection.directories.new(:key => uploader.fog_directory)
file = directory.files.new(:key => @path)
file.url(::Fog::Time.now + uploader.fog_authenticated_url_expiration)
end
def connection
@connection ||= CarrierWave::Storage::Fog.new(uploader).connection
end
def uploader
@uploader ||= OpenStruct.new(
:fog_authenticated_url_expiration => 600,
:fog_credentials => {
:provider => 'AWS',
:aws_access_key_id => ENV['S3_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['S3_SECRET_ACCESS_KEY'],
:region => ENV['S3_REGION'] || 'eu-west-1'
},
:fog_directory => ENV['S3_BUCKET_NAME']
)
end
end
Product.all.each do |product|
product.update_attributes(:remote_factsheet_url => FogFile.new("products/#{product.identifier}.pdf").url)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment