注意

本文档适用于 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')

创建对象

这会创建一个文件hello.txt来自名为my_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')

删除容器

Note

容器必须为空!否则请求将不会工作!

container.delete_container('my-new-container')

由 Ceph 基金会带给您

Ceph 文档是一个社区资源,由非盈利的 Ceph 基金会资助和托管Ceph Foundation. 如果您想支持这一点和我们的其他工作,请考虑加入现在加入.