1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge pull request #1611 from rackspace/file_examples

[Rackspace|Storage] Adding Cloud Files examples
This commit is contained in:
Kyle Rames 2013-03-12 08:08:21 -07:00
commit 36166dbcd1
9 changed files with 418 additions and 47 deletions

View file

@ -1,47 +0,0 @@
# Getting Started Examples
## Download
You can find examples using the Rackspace and Fog in the [fog repository](https://github.com/fog/fog) under the `fog/lib/fog/rackspace/examples` directory.
You can download this repository by executing the following command:
git clone git://github.com/fog/fog.git
Optionally you can download a zip by clicking on this [link](https://github.com/fog/fog/archive/master.zip).
## Requirements
Examples require the following:
* Rackspace Cloud account
* Ruby 1.8.x or 1.9.x
* `fog` gem
For more information please refer to the [Getting Started with Fog and the Rackspace Cloud](https://github.com/fog/fog/blob/master/lib/fog/rackspace/docs/getting_started.md) document.
## Credentials
Examples will prompt for Rackspace Cloud credentials. You can skip prompts by creating a `.fog` file in the user's home directory. This is an example of a `.fog` file for the Rackspace Cloud:
default:
rackspace_username: RACKSPACE_USERNAME
rackspace_api_key: RACKSPACE_API_KEY
**Note:** *Replace capitalized values with the appropriate credential information.*
## Executing
To execute scripts using `bundler`:
bundle exec ruby <script>
To execute scripts without `bundler`:
ruby <script>
## Support and Feedback
Your feedback is appreciated! If you have specific issues with the **fog** SDK, you should file an [issue via Github](https://github.com/fog/fog/issues).
For general feedback and support requests, send an email to: <sdk-support@rackspace.com>.

View file

@ -0,0 +1,43 @@
#!/usr/bin/env ruby
# This example demonstrates creating a container with CDN on 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
# create Cloud Files service
service = Fog::Storage.new({
:provider => 'Rackspace',
:rackspace_username => rackspace_username,
:rackspace_api_key => rackspace_api_key
})
# prompt for directory name
directory_name = get_user_input "\nEnter name of directory to create"
# create directory
directory = service.directories.create :key => directory_name, :public => true
# reload directory to refresh information
directory.reload
puts "\nDirectory #{directory.key} was created."
puts "To delete the container please execute the delete_directory.rb script\n\n"

View file

@ -0,0 +1,45 @@
#!/usr/bin/env ruby
# This example demonstrates creating a container 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
# create Cloud Files service
service = Fog::Storage.new({
:provider => 'Rackspace',
:rackspace_username => rackspace_username,
:rackspace_api_key => rackspace_api_key
})
# prompt for directory name
directory_name = get_user_input "\nEnter name of directory to create"
# create directory
directory = service.directories.create :key => directory_name
# reload directory to refresh information
directory.reload
puts "\n Directory #{directory.key} was created."
puts "To delete the container please execute the delete_directory.rb script\n\n"

View file

@ -0,0 +1,61 @@
#!/usr/bin/env ruby
# This example demonstrates deleting a container 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
def select_directory(directories)
abort "\nThere are not any directories to delete. Try running create_directory.rb\n\n" if directories.empty?
puts "\nSelect Directory To Delete:\n\n"
directories.each_with_index do |dir, i|
puts "\t #{i}. #{dir.key} [#{dir.count} objects]"
end
delete_str = get_user_input "\nEnter Directory Number"
directories[delete_str.to_i]
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
# create Cloud Files service
service = Fog::Storage.new({
:provider => 'Rackspace',
:rackspace_username => rackspace_username,
:rackspace_api_key => rackspace_api_key
})
# retrieve directories
directories = service.directories
# prompt for directory
directory = select_directory(directories)
puts "\nNow deleting #{directory.key}"
# delete files if necessary
directory.files.each do |f|
puts "\tDeleting file #{f.key}"
f.destroy
end
# delete directory
directory.destroy
puts "\tDone\n\n"

View file

@ -0,0 +1,69 @@
#!/usr/bin/env ruby
# This example demonstrates deleting a file from a container 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
def select_directory(directories)
abort "\nThere are not any directories with files to delete. Try running create_file.rb\n\n" if directories.empty?
puts "\nSelect Directory:\n\n"
directories.each_with_index do |dir, i|
puts "\t #{i}. #{dir.key} [#{dir.count} objects]"
end
delete_str = get_user_input "\nEnter Directory Number"
directories[delete_str.to_i]
end
def select_file(files)
puts "\nSelect File:\n\n"
files.each_with_index do |file, i|
puts "\t #{i}. #{file.key}"
end
delete_str = get_user_input "\nEnter File Number"
files[delete_str.to_i]
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
# create Cloud Files service
service = Fog::Storage.new({
:provider => 'Rackspace',
:rackspace_username => rackspace_username,
:rackspace_api_key => rackspace_api_key
})
# retrieve directories with files
directories = service.directories.select {|s| s.count > 0}
# prompt for directory
directory = select_directory(directories)
# list of files for directory
files = directory.files
# prompt for file to delete
file = select_file(files)
# delete file
file.destroy
puts "\nFile #{file.key} was successfully deleted"

View file

@ -0,0 +1,74 @@
#!/usr/bin/env ruby
# This example demonstrates downloading a file 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
def select_directory(directories)
abort "\nThere are not any directories with files. Try running create_file.rb\n\n" if directories.empty?
puts "\nSelect Directory:\n\n"
directories.each_with_index do |dir, i|
puts "\t #{i}. #{dir.key} [#{dir.count} objects]"
end
delete_str = get_user_input "\nEnter Directory Number"
directories[delete_str.to_i]
end
def select_file(files)
puts "\nSelect File:\n\n"
files.each_with_index do |file, i|
puts "\t #{i}. #{file.key}"
end
delete_str = get_user_input "\nEnter File Number"
files[delete_str.to_i]
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
# create Cloud Files service
service = Fog::Storage.new({
:provider => 'Rackspace',
:rackspace_username => rackspace_username,
:rackspace_api_key => rackspace_api_key
})
# retrieve directories with files
directories = service.directories.select {|s| s.count > 0}
# prompt for directory
directory = select_directory(directories)
# list of files for directory
files = directory.files
# prompt for file to download
file = select_file(files)
# download file
filename = File.join(File.dirname(__FILE__), "downloaded-#{file.key}")
File.open(filename, 'w') do | f |
directory.files.get(file.key) do | data, remaining, content_length |
f.syswrite data
end
end
puts "\nFile #{file.key} was successfully downloaded to #{filename}"

View file

@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

View file

@ -0,0 +1,81 @@
#!/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'
# 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 print_metadata(object)
object.metadata.each_pair do |key, value|
puts "\t#{key}: #{value}"
end
puts "\n"
end
# create Cloud Files service
service = Fog::Storage.new({
:provider => 'Rackspace',
:rackspace_username => rackspace_username,
:rackspace_api_key => rackspace_api_key
})
# create directory
puts "Creating directory 'metadata-tester'"
directory = service.directories.create :key => "metadata-tester"
# initial metadata
puts "Initial Container Metadata\n"
print_metadata directory
# adding metadata
puts "Adding Container Metadata"
directory.metadata["environment"] = "demo"
directory.save
print_metadata directory
# update metadata
puts "Updating Container Metadata"
directory.metadata["environment"] = "test"
directory.save
print_metadata directory
# upload file
puts "Uploading file"
upload_file = File.join(File.dirname(__FILE__), "lorem.txt")
file = directory.files.create :key => 'sample.txt', :body => File.open(upload_file, "r")
# initial metadata
puts "Initial File Metadata\n"
print_metadata file
# adding metadata
puts "Adding File Metadata"
file.metadata["preview"] = "true"
file.save
print_metadata file
# update metadata
puts "Updating File Metadata"
file.metadata["preview"] = "false"
file.save
print_metadata file
puts "To delete the directory and file please execute the delete_directory.rb script\n\n"

View file

@ -0,0 +1,44 @@
#!/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
# create Cloud Files service
service = Fog::Storage.new({
:provider => 'Rackspace',
:rackspace_username => rackspace_username,
:rackspace_api_key => rackspace_api_key
})
# prompt for directory name
directory_name = get_user_input "\nEnter name of directory to create"
# create directory with CDN service
directory = service.directories.create :key => directory_name, :public => true
# upload file
upload_file = File.join(File.dirname(__FILE__), "lorem.txt")
file = directory.files.create :key => 'sample.txt', :body => File.open(upload_file, "r")
puts "You should not be able to view this file via CDN at #{file.public_url}"
puts "To delete the container and associated file please execute the delete_directory.rb script\n\n"