class AliyunSDK::OSS::Client

OSS服务的客户端,用于获取bucket列表,创建/删除bucket。Object相关 的操作请使用{OSS::Bucket}。 @example 创建Client

endpoint = 'oss-cn-hangzhou.aliyuncs.com'
client = Client.new(
  :endpoint => endpoint,
  :access_key_id => 'access_key_id',
  :access_key_secret => 'access_key_secret')
buckets = client.list_buckets
client.create_bucket('my-bucket')
client.delete_bucket('my-bucket')
bucket = client.get_bucket('my-bucket')

Public Class Methods

new(opts) click to toggle source

构造OSS client,用于操作buckets。 @param opts [Hash] 构造Client时的参数选项 @option opts [String] :endpoint [必填]OSS服务的地址,可以是以

oss-cn-hangzhou.aliyuncs.com的标准域名,也可以是用户绑定的域名

@option opts [String] :access_key_id [可选]用户的ACCESS KEY ID,

如果不填则会尝试匿名访问

@option opts [String] :access_key_secret [可选]用户的ACCESS

KEY SECRET,如果不填则会尝试匿名访问

@option opts [Boolean] :cname [可选] 指定endpoint是否是用户绑

定的域名

@option opts [String] :sts_token [可选] 指定STS的

SecurityToken,如果指定,则使用STS授权访问

@option opts [Fixnum] :open_timeout [可选] 指定建立连接的超时

时间,默认为10秒

@option opts [Fixnum] :read_timeout [可选] 指定等待响应的超时

时间,默认为120秒

@example 标准endpoint

oss-cn-hangzhou.aliyuncs.com
oss-cn-beijing.aliyuncs.com

@example 用户绑定的域名

my-domain.com
foo.bar.com
# File lib/aliyun_sdk/oss/client.rb, line 42
def initialize(opts)
  fail ClientError, "Endpoint must be provided" unless opts[:endpoint]

  @config = Config.new(opts)
  @protocol = Protocol.new(@config)
end

Public Instance Methods

bucket_exist?(name)
Alias for: bucket_exists?
bucket_exists?(name) click to toggle source

判断一个bucket是否存在 @param name [String] Bucket名字 @return [Boolean] 如果Bucket存在则返回true,否则返回false

# File lib/aliyun_sdk/oss/client.rb, line 81
def bucket_exists?(name)
  exist = false

  begin
    @protocol.get_bucket_acl(name)
    exist = true
  rescue ServerError => e
    raise unless e.http_code == 404
  end

  exist
end
Also aliased as: bucket_exist?
create_bucket(name, opts = {}) click to toggle source

创建一个bucket @param name [String] Bucket名字 @param opts [Hash] 创建Bucket的属性(可选) @option opts [:location] [String] 指定bucket所在的区域,默认为oss-cn-hangzhou

# File lib/aliyun_sdk/oss/client.rb, line 67
def create_bucket(name, opts = {})
  @protocol.create_bucket(name, opts)
end
delete_bucket(name) click to toggle source

删除一个bucket @param name [String] Bucket名字 @note 如果要删除的Bucket不为空(包含有object),则删除会失败

# File lib/aliyun_sdk/oss/client.rb, line 74
def delete_bucket(name)
  @protocol.delete_bucket(name)
end
get_bucket(name) click to toggle source

获取一个Bucket对象,用于操作bucket中的objects。 @param name [String] Bucket名字 @return [Bucket] Bucket对象

# File lib/aliyun_sdk/oss/client.rb, line 99
def get_bucket(name)
  Bucket.new({:name => name}, @protocol)
end
list_buckets(opts = {}) click to toggle source

列出当前所有的bucket @param opts [Hash] 查询选项 @option opts [String] :prefix 如果设置,则只返回以它为前缀的bucket @option opts [String] :marker 如果设置,则只返回名字在它之后

(字典序,不包含marker)的bucket

@return [Enumerator<Bucket>] Bucket的迭代器

# File lib/aliyun_sdk/oss/client.rb, line 55
def list_buckets(opts = {})
  if @config.cname
    fail ClientError, "Cannot list buckets for a CNAME endpoint."
  end

  Iterator::Buckets.new(@protocol, opts).to_enum
end