Class: Aliyun::OSS::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/aliyun/oss/client.rb

Overview

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

Examples:

创建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')

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Client

构造OSS client,用于操作buckets。

Examples:

标准endpoint

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

用户绑定的域名

my-domain.com
foo.bar.com

Parameters:

  • opts (Hash)

    构造Client时的参数选项

Options Hash (opts):

  • :endpoint (String)

    [必填]OSS服务的地址,可以是以 oss-cn-hangzhou.aliyuncs.com的标准域名,也可以是用户绑定的域名

  • :access_key_id (String)

    [可选]用户的ACCESS KEY ID, 如果不填则会尝试匿名访问

  • :access_key_secret (String)

    [可选]用户的ACCESS KEY SECRET,如果不填则会尝试匿名访问

  • :cname (Boolean)
    可选

    指定endpoint是否是用户绑

    定的域名

  • :upload_crc_enable (Boolean)

    [可选]指定上传处理 是否开启CRC校验,默认为开启(true)

  • :download_crc_enable (Boolean)

    [可选]指定下载处理 是否开启CRC校验,默认为不开启(false)

  • :sts_token (String)
    可选

    指定STS的

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

  • :open_timeout (Fixnum)
    可选

    指定建立连接的超时

    时间,默认为10秒

  • :read_timeout (Fixnum)
    可选

    指定等待响应的超时

    时间,默认为120秒



47
48
49
50
51
52
# File 'lib/aliyun/oss/client.rb', line 47

def initialize(opts)
  fail ClientError, "Endpoint must be provided" unless opts[:endpoint]

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

Instance Method Details

#bucket_exists?(name) ⇒ Boolean Also known as: bucket_exist?

判断一个bucket是否存在

Parameters:

  • name (String)

    Bucket名字

Returns:

  • (Boolean)

    如果Bucket存在则返回true,否则返回false



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/aliyun/oss/client.rb', line 86

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

#create_bucket(name, opts = {}) ⇒ Object

创建一个bucket

Parameters:

  • name (String)

    Bucket名字

  • opts (Hash) (defaults to: {})

    创建Bucket的属性(可选)

Options Hash (opts):

  • [String] (:location)

    指定bucket所在的区域,默认为oss-cn-hangzhou



72
73
74
# File 'lib/aliyun/oss/client.rb', line 72

def create_bucket(name, opts = {})
  @protocol.create_bucket(name, opts)
end

#delete_bucket(name) ⇒ Object

Note:

如果要删除的Bucket不为空(包含有object),则删除会失败

删除一个bucket

Parameters:

  • name (String)

    Bucket名字



79
80
81
# File 'lib/aliyun/oss/client.rb', line 79

def delete_bucket(name)
  @protocol.delete_bucket(name)
end

#get_bucket(name) ⇒ Bucket

获取一个Bucket对象,用于操作bucket中的objects。

Parameters:

  • name (String)

    Bucket名字

Returns:



104
105
106
# File 'lib/aliyun/oss/client.rb', line 104

def get_bucket(name)
  Bucket.new({:name => name}, @protocol)
end

#list_buckets(opts = {}) ⇒ Enumerator<Bucket>

列出当前所有的bucket

Parameters:

  • opts (Hash) (defaults to: {})

    查询选项

Options Hash (opts):

  • :prefix (String)

    如果设置,则只返回以它为前缀的bucket

  • :marker (String)

    如果设置,则只返回名字在它之后 (字典序,不包含marker)的bucket

Returns:

  • (Enumerator<Bucket>)

    Bucket的迭代器



60
61
62
63
64
65
66
# File 'lib/aliyun/oss/client.rb', line 60

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