Skip to content

Instantly share code, notes, and snippets.

@hryk
Created March 18, 2013 14:25
Show Gist options
  • Save hryk/5187508 to your computer and use it in GitHub Desktop.
Save hryk/5187508 to your computer and use it in GitHub Desktop.
list, get files from openstack swift with fog.
#!/usr/bin/env ruby
# encoding: utf-8
require 'rubygems'
require 'fog'
connection = Fog::Storage.new(
:provider => 'OpenStack',
:openstack_auth_url => ENV['OS_AUTH_URL'] + '/tokens',
:openstack_tenant => ENV['OS_TENANT_NAME'],
:openstack_username => ENV['OS_USERNAME'],
:openstack_api_key => ENV['OS_PASSWORD'],
)
#
# List files.
#
connection.directories.each do |directory|
puts "d #{directory.key}/"
directory.files.each do |file|
puts " f #{file.key}"
end
end
#
# Save a file.
#
content = ''
connection.get_object('test-bucket', 'test') do |chunk, remaining, total|
content << chunk
end
open('test.jpg', 'w') do |f|
f.write content
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment