Created
July 5, 2013 13:13
-
-
Save krames/5934450 to your computer and use it in GitHub Desktop.
This script will create a CDN backed container named "demo-container" containing a file called demo-file" To run this install fog using `gem install fog` and the script can be run as follows: ```ruby cloud_files_publish <username> <api key>```
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 'fog' | |
| require 'tempfile' | |
| # Note: Only Rackspace, HP, and Amazon have CDN support. This example only demonstrates creating a CDN backed container with Rackspace and HP. (Amazon does something else.) | |
| begin | |
| # :provider is the only standard key all others are provider specific | |
| service = Fog::Storage.new :provider => 'rackspace', | |
| :rackspace_username => ARGV[0], | |
| :rackspace_api_key => ARGV[1] | |
| # Note: Fog refers to containers as directories | |
| # When the public attribute is set to true, Fog automatically publishes this container to the CDN | |
| puts "Create and Publish Container to CDN" | |
| dir = service.directories.create :key => 'demo-container', :public => true | |
| f = Tempfile.open 'fog-test-file' | |
| f.write "Hello Cloud Files" | |
| f.rewind | |
| puts "Create Object From File" | |
| obj = dir.files.create :key => 'demo-file', :body => f | |
| puts " Go to #{obj.public_url}" | |
| rescue => e | |
| puts e.backtrace | |
| ensure | |
| f.unlink if f | |
| end | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would have been more idiomatic to skip the temp file creation in lines 17-19 and just pass the "Hello Cloud Files" string into the file create command on line 22.
Let me know if you have any questions or need any edits.