注意
本文档适用于 Ceph 的开发版本。
Ruby Swift 示例
创建连接
这将创建一个连接,以便您可以与服务器交互
require 'cloudfiles'
username = 'account_name:user_name'
api_key = 'your_secret_key'
conn = CloudFiles::Connection.new(
:username => username,
:api_key => api_key,
:auth_url => 'http://objects.dreamhost.com/auth'
)
创建容器
这将创建一个名为 my-new-container 的新容器
container = conn.create_container('my-new-container')
创建对象
这会从名为 my_hello.txt 的文件创建文件 hello.txt
obj = container.create_object('hello.txt')
obj.load_from_filename('./my_hello.txt')
obj.content_type = 'text/plain'
列出拥有的容器
这会获取您拥有的容器列表,并打印出容器名称
conn.containers.each do |container|
puts container
end
输出将类似于
mahbuckat1
mahbuckat2
mahbuckat3
列出容器内容
这会获取容器中对象的列表,并打印出每个对象的名称、文件大小和上次修改日期
require 'date' # not necessary in the next version
container.objects_detail.each do |name, data|
puts "#{name}\t#{data[:bytes]}\t#{data[:last_modified]}"
end
输出将类似于
myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
myphoto2.jpg 262518 2011-08-08T21:38:01.000Z
检索对象
这会下载对象 hello.txt 并将其保存在 ./my_hello.txt 中
obj = container.object('hello.txt')
obj.save_to_filename('./my_hello.txt')
删除对象
这会删除对象 goodbye.txt
container.delete_object('goodbye.txt')
删除容器
注意
容器必须为空!否则请求将无法工作!
container.delete_container('my-new-container')