Last active
December 23, 2015 05:19
-
-
Save krames/6586327 to your computer and use it in GitHub Desktop.
This is code entire code sample presented in the Ruby SDK training on 9/19/2013
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # require the Fog library | |
| require 'fog' | |
| # Create a service object to access Cloud Files | |
| service = Fog::Storage.new :provider => 'rackspace', | |
| :rackspace_username => USER, # replace USER with your username | |
| :rackspace_api_key => API_KEY, # replace API_KEY with your key | |
| :rackspace_region => :ord # We are using the Chicago data center, by default Fog will use the Dallas data center | |
| # Create a container - Note that Fog refers to containers as directories | |
| dir = service.directories.create :key => 'sdk-demo' | |
| puts "Container #{dir.key} has been created\n" | |
| # upload sam.jpg and store it into the sdk-demo directory with the name dog.jpg | |
| obj = dir.files.create :key => 'dog.jpg', :body => File.open('sam.jpg', "r") | |
| puts "Object #{obj.key} has been uploaded to container #{dir.key}\n" | |
| # Enable CDN | |
| dir.public = true | |
| dir.save | |
| puts "Container #{dir.key} is now CDN enabled\n" | |
| # retrieve the CDN url for the dog.jpg object | |
| obj.public_url | |
| puts "#{obj.key} is available at #{obj.public_url}\n" | |
| # Download dog.jpg stored in the sdk-demo directory and store it in a file called sam.jpg | |
| File.open('sam.jpg', 'w') do | f | | |
| dir.files.get(obj.key) do | data | | |
| f.write data | |
| end | |
| end | |
| puts "#{obj.key} has been downloaded to sam.jpg\n" | |
| # delete the dog.jpg object | |
| obj.destroy | |
| puts "#{obj.key} has been deleted!\n" | |
| # delete the sdk-demo container | |
| dir.destroy | |
| puts "#{dir.key} has been deleted!" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment