如何使用ruby openstack gem创建公共可读的对象存储容器?

时间:2016-05-10 07:38:57

标签: ruby openstack

我试图像这样创建公开可读的openstack对象存储容器:

os = OpenStack::Connection.create(...)      
container = os.create_container(container_name)
container.set_metadata({'X-Container-Read' => '.r:*'})

使用上面的代码,新创建的容器是私有的。

使用ruby openstack gem创建具有公共读取权限的容器的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

您可以尝试以下方式。

您可以重新定义create_container方法

然后

class MyStack < OpenStack::Swift::Connection
  def create_container(containername)
   super
   @connection.req("PUT", path, {:headers=>{"Content-Length"=>"0", "X-Container-Read" => ".r:*", "X-Container-Write" => ".r:*}})
   OpenStack::Swift::Container.new(self, containername)
 end
end

您需要设置的"X-Container-Read" => ".r:*", "X-Container-Write" => ".r:*"标头值。

container.set_metadata({"X-Container-Read" => ".r:*", "X-Container-Write" => ".r:*"})

答案 1 :(得分:0)

这是我最终做的事情:

module PubliclyRedableContainerMonkeyPatch
  def create_publicy_readable_container(containername)
    raise OpenStack::Exception::InvalidArgument.new("Container name cannot contain '/'") if containername.match("/")
    raise OpenStack::Exception::InvalidArgument.new("Container name is limited to 256 characters") if containername.length > 256
    path = "/#{URI.encode(containername.to_s)}"
    @connection.req("PUT", path, {:headers=>{"Content-Length"=>"0", "X-Container-Read" => ".r:*"}})
    OpenStack::Swift::Container.new(self, containername)
  end
end

OpenStack::Swift::Connection.include PubliclyRedableContainerMonkeyPatch

os = OpenStack::Connection.create(...)                
container = os.create_publicy_readable_container(container_name)

WORKSFORME。 :)