Skip to content

Instantly share code, notes, and snippets.

@gekola
Last active July 27, 2016 21:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gekola/1306e41989a398dc14b7 to your computer and use it in GitHub Desktop.
Save gekola/1306e41989a398dc14b7 to your computer and use it in GitHub Desktop.
Paperclip/Carrierwave configuration for Bluemix object space
require 'bundler/setup'
require 'carrierwave'
CREDENTIALS = YAML.load_file('./credentials.yml')
CarrierWave.configure do |config|
config.storage = :fog
config.fog_credentials = {
provider: 'openstack',
openstack_auth_url: CREDENTIALS[:auth_url],
openstack_api_key: CREDENTIALS[:password],
openstack_project_id: CREDENTIALS[:project_id],
openstack_userid: CREDENTIALS[:user_id],
# openstack_username: CREDENTIALS[:username],
# openstack_domain_name: CREDENTIALS[:domain_name],
openstack_region: CREDENTIALS[:region],
openstack_auth_omit_default_port: true
}
config.fog_directory = 'test_uploads'
config.fog_public = false
end
class TestUploader < CarrierWave::Uploader::Base
end
puts 'Generating ...'
`dd bs=1M count=1 if=/dev/urandom of=test_file.bin 2>&1 >/dev/null`
puts 'Uploading ...'
File.open('./test_file.bin') { |f| TestUploader.new.store!(f) }
puts 'Retrieving ...'
File.open('./test_file.2.bin', 'w') do |f|
uploader = TestUploader.new
uploader.retrieve_from_store!('test_file.bin')
f << uploader.file.read
end
puts 'Comparing ..'
puts `diff -s test_file.bin test_file.2.bin`
source 'https://rubygems.org'
gem 'fog-openstack', github: 'fog/fog-openstack'
gem 'fog'
gem 'carrierwave'
gem 'paperclip'
gem 'pry-byebug'
require 'bundler/setup'
require 'active_support'
require 'active_support/core_ext'
require 'fog'
require 'paperclip'
CREDENTIALS = YAML.load_file('./credentials.yml')
class TestModel
extend ActiveModel::Callbacks
include ActiveModel::Model
include Paperclip::Glue
# Paperclip required callbacks
define_model_callbacks :commit, only: [:after]
define_model_callbacks :save, only: [:after]
define_model_callbacks :destroy, only: [:before, :after]
attr_accessor :file_file_name, :file_content_type, :file_file_size, :file_updated_at, :id
has_attached_file :file,
storage: :fog,
path: 'uploads/:filename',
fog_directory: 'test_uploads',
fog_credentials: {
provider: 'openstack',
openstack_auth_url: CREDENTIALS[:auth_url],
openstack_api_key: CREDENTIALS[:password],
openstack_project_id: CREDENTIALS[:project_id],
###
# openstack_userid: CREDENTIALS[:user_id],
# one of these is required
openstack_username: CREDENTIALS[:username],
openstack_domain_name: CREDENTIALS[:domain_name],
###
openstack_region: CREDENTIALS[:region],
openstack_auth_omit_default_port: true
}
do_not_validate_attachment_file_type :file
def save
run_callbacks :save
true
end
end
puts 'Generating ...'
`dd bs=1M count=1 if=/dev/urandom of=test_file.bin 2>&1 >/dev/null`
puts 'Uploading ...'
File.open('./test_file.bin') do |f|
model = TestModel.new
model.file = f
model.save
end
puts 'Retrieving ...'
File.open('./test_file.2.bin', 'w') do |f|
model = TestModel.new
model.file_file_name = 'test_file.bin'
model.file.copy_to_local_file('original', 'test_file.2.bin')
end
puts 'Comparing ..'
puts `diff -s test_file.bin test_file.2.bin`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment