Last active
December 18, 2015 15:39
-
-
Save krames/5805593 to your computer and use it in GitHub Desktop.
Cloud File Upload
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
| #!/usr/bin/env ruby | |
| # This example demonstrates creating a file on the CDN network with the Rackpace Open Cloud | |
| require 'rubygems' #required for Ruby 1.8.x | |
| require 'fog' | |
| def get_user_input(prompt) | |
| print "#{prompt}: " | |
| gets.chomp | |
| end | |
| # Use username defined in ~/.fog file, if absent prompt for username. | |
| # For more details on ~/.fog refer to http://fog.io/about/getting_started.html | |
| def rackspace_username | |
| Fog.credentials[:rackspace_username] || get_user_input("Enter Rackspace Username") | |
| end | |
| # Use api key defined in ~/.fog file, if absent prompt for api key | |
| # For more details on ~/.fog refer to http://fog.io/about/getting_started.html | |
| def rackspace_api_key | |
| Fog.credentials[:rackspace_api_key] || get_user_input("Enter Rackspace API key") | |
| end | |
| def select_directory(directories) | |
| abort "\nThere are not any directories in DFW\n\n" if directories.empty? | |
| puts "\nSelect Directory To Upload To:\n\n" | |
| directories.each_with_index do |dir, i| | |
| puts "\t #{i}. #{dir.key} [#{dir.count} objects]" | |
| end | |
| select_str = get_user_input "\nEnter Directory Number" | |
| directories[select_str.to_i] | |
| end | |
| # create Cloud Files service | |
| service = Fog::Storage.new({ | |
| :provider => 'Rackspace', | |
| :rackspace_username => rackspace_username, | |
| :rackspace_api_key => rackspace_api_key, | |
| :rackspace_region => :dfw | |
| }) | |
| # prompt for directory name | |
| directory = select_directory(service.directories) | |
| # prompt for file name | |
| file_str = get_user_input "Enter name of file to upload" | |
| filename = File.basename(file_str) | |
| # upload file | |
| file = directory.files.create :key => filename, :body => File.open(file_str, "r") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment