Skip to content

Commit

Permalink
Merge 3f74fa7 into 794fc45
Browse files Browse the repository at this point in the history
  • Loading branch information
qixu001 committed Oct 4, 2017
2 parents 794fc45 + 3f74fa7 commit 278abcf
Show file tree
Hide file tree
Showing 16 changed files with 368 additions and 430 deletions.
29 changes: 14 additions & 15 deletions examples/aliyun/oss/bucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'yaml'
require 'aliyun/oss'

# 初始化OSS client
# Initialize OSS client
Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
conf_file = '~/.oss.yml'
conf = YAML.load(File.read(File.expand_path(conf_file)))
Expand All @@ -15,7 +15,7 @@
:access_key_secret => conf['access_key_secret'])
bucket = client.get_bucket(conf['bucket'])

# 辅助打印函数
# print helper function
def demo(msg)
puts "######### #{msg} ########"
puts
Expand All @@ -24,13 +24,13 @@ def demo(msg)
puts
end

# 列出当前所有的bucket
# list all buckets
demo "List all buckets" do
buckets = client.list_buckets
buckets.each{ |b| puts "Bucket: #{b.name}"}
end

# 创建bucket,如果同名的bucket已经存在,则创建会失败
# create bucket. If the bucket already exists, the creation will fail
demo "Create bucket" do
begin
bucket_name = 't-foo-bar'
Expand All @@ -41,7 +41,7 @@ def demo(msg)
end
end

# 向bucket中添加5个空的object:
# add 5 empty objects into bucket:
# foo/obj1, foo/bar/obj1, foo/bar/obj2, foo/xxx/obj1

demo "Put objects before list" do
Expand All @@ -52,7 +52,7 @@ def demo(msg)
bucket.put_object('中国の')
end

# list bucket下所有objects
# list bucket's all objects
demo "List first 10 objects" do
objects = bucket.list_objects

Expand All @@ -61,7 +61,7 @@ def demo(msg)
end
end

# list bucket下所有前缀为foo/bar/的object
# list bucket's all object whose name has the prefix foo/bar/
demo "List first 10 objects with prefix 'foo/bar/'" do
objects = bucket.list_objects(:prefix => 'foo/bar/')

Expand All @@ -70,18 +70,17 @@ def demo(msg)
end
end

# 获取object的common prefixcommon prefix是指bucket下所有object(也可
# 以指定特定的前缀)的公共前缀,这在object数量巨多的时候很有用,例如有
# 如下的object:
# get common prefix of the object. Common prefix object is the object whose name is the common prefix of some other objects in the bucket.
# Essentially it's a 'folder' in the bucket.
# For example, if we have the following objects:
# /foo/bar/obj1
# /foo/bar/obj2
# ...
# /foo/bar/obj9999999
# /foo/xx/
# 指定foo/为prefix,/为delimiter,则返回的common prefix为
# /foo/bar/, /foo/xxx/
# 这可以表示/foo/目录下的子目录。如果没有common prefix,你可能要遍历所
# 有的object来找公共的前缀
# Specify the prefix as foo/ and delimiter as '/', then the retirned common prefix is
# /foo/bar/ and /foo/xxx/
# They could represent the subfolder of '/foo' folder. It's a efficient way to enumerate all files under a folder by specifying the common prefix.

demo "List first 10 objects/common prefixes" do
objects = bucket.list_objects(:prefix => 'foo/', :delimiter => '/')
Expand All @@ -95,7 +94,7 @@ def demo(msg)
end
end

# 获取/设置Bucket属性: ACL, Logging, Referer, Website, LifeCycle, CORS
# get/set Bucket attributes: ACL, Logging, Referer, Website, LifeCycle, CORS
demo "Get/Set bucket properties: ACL/Logging/Referer/Website/Lifecycle/CORS" do
puts "Bucket acl before: #{bucket.acl}"
bucket.acl = Aliyun::OSS::ACL::PUBLIC_READ
Expand Down
13 changes: 6 additions & 7 deletions examples/aliyun/oss/callback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
require 'aliyun/oss'

##
# 用户在上传文件时可以指定“上传回调”,这样在文件上传成功后OSS会向用户
# 提供的服务器地址发起一个HTTP POST请求,相当于一个通知机制。用户可以
# 在收到回调的时候做相应的动作。
# 1. 如何接受OSS的回调可以参考代码目录下的
# User could specify callback when uploading a file so that OSS will issue a POST request to that callback url upon a successful file upload.
# This is one way of notification and user could do proper action on that callback request.
# 1. Check out the following file to know more about how to handle OSS's callback request.
# rails/aliyun_oss_callback_server.rb
# 2. 只有put_object和resumable_upload支持上传回调
# 2. Only put_object and resumable_upload support upload callback.

# 初始化OSS client
# Initialize OSS client
Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
conf_file = '~/.oss.yml'
conf = YAML.load(File.read(File.expand_path(conf_file)))
Expand All @@ -23,7 +22,7 @@
:access_key_id => conf['access_key_id'],
:access_key_secret => conf['access_key_secret']).get_bucket(conf['bucket'])

# 辅助打印函数
# print helper function
def demo(msg)
puts "######### #{msg} ########"
puts
Expand Down
48 changes: 24 additions & 24 deletions examples/aliyun/oss/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'yaml'
require 'aliyun/oss'

# 初始化OSS client
# Initialize OSS client
Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
conf_file = '~/.oss.yml'
conf = YAML.load(File.read(File.expand_path(conf_file)))
Expand All @@ -14,7 +14,7 @@
:access_key_id => conf['access_key_id'],
:access_key_secret => conf['access_key_secret']).get_bucket(conf['bucket'])

# 辅助打印函数
# print helper function
def demo(msg)
puts "######### #{msg} ########"
puts
Expand All @@ -23,24 +23,24 @@ def demo(msg)
puts
end

# 上传一个object
# 流式上传请参考:examples/streaming.rb
# upload an object
# Check out examples/streaming.rb for streaming upload example.
demo "Put object from input" do
bucket.put_object('files/hello') do |content|
content << 'hello world.'
end
puts "Put object: files/hello"
end

# 上传一个文件
# 断点续传请参考:examples/resumable_upload.rb
# upload an object
# Check out examples/resumable_upload.rb for resumable upload example.
demo "Put object from local file" do
File.open('/tmp/x', 'w'){ |f| f.write("hello world\n") }
bucket.put_object('files/world', :file => '/tmp/x')
puts "Put object: files/world"
end

# 创建一个Appendable object
# Create an Appendable object
demo "Create appendable object" do
size = bucket.get_object('files/appendable').size rescue 0
bucket.append_object('files/appendable', size) do |content|
Expand All @@ -49,8 +49,8 @@ def demo(msg)
puts "Append object: files/appendable"
end

# 向files/appendable中追加内容
# 首先要获取object当前的长度
# append content into files/appendable.
# Get the object's current length.
demo "Append to object" do
size = bucket.get_object('files/appendable').size
bucket.append_object('files/appendable', size) do |content|
Expand All @@ -59,7 +59,7 @@ def demo(msg)
puts "Append object: files/appendable"
end

# 使用错误的position进行追加会失败
# Append will fail if using wrong position
demo "Append with wrong pos" do
begin
bucket.append_object('files/appendable', 0) do |content|
Expand All @@ -70,7 +70,7 @@ def demo(msg)
end
end

# 向一个normal object中追加内容会失败
# Appending to normal object will fail
demo "Append to normal object(fail)" do
begin
bucket.append_object('files/hello', 0) do |content|
Expand All @@ -81,13 +81,13 @@ def demo(msg)
end
end

# 拷贝一个object
# copy an object
demo "Copy object" do
bucket.copy_object('files/hello', 'files/copy')
puts "Copy object files/hello => files/copy"
end

# 拷贝一个appendable object会失败
# copying an appendable object will fail
demo "Copy appendable object(fail)" do
begin
bucket.copy_object('files/appendable', 'files/copy')
Expand All @@ -96,8 +96,8 @@ def demo(msg)
end
end

# 下载一个object:流式处理
# 流式下载请参考:examples/streaming.rb
# download an OSS object into memory
# Check out examples/streaming.rb for streaming download examples.
demo "Get object: handle content" do
total_size = 0
bucket.get_object('files/hello') do |chunk|
Expand All @@ -106,27 +106,27 @@ def demo(msg)
puts "Total size: #{total_size}"
end

# 下载一个object:下载到文件中
# download an OSS object into local file
demo "Get object to local file" do
bucket.get_object('files/hello', :file => '/tmp/hello')
puts "Get object: files/hello => /tmp/hello"
end

# 删除一个object
# delete an object
demo "Delete object" do
bucket.delete_object('files/world')
puts "Delete object: files/world"
end

# 删除一个不存在的object返回OK
# 这意味着delete_object是幂等的,在删除失败的时候可以不断重试,直到成
# 功,成功意味着object已经不存在
# Delete an non-existing object and it should return OK.
# So delete_object is idempotent and you can retry delete_object until success if it fails.
# It means the object does not exist if delete_object return OK.
demo "Delete a non-existent object(OK)" do
bucket.delete_object('non-existent-object')
puts "Delete object: non-existent-object"
end

# 设置Object metas
# Set Object metas
demo "Put objec with metas" do
bucket.put_object(
'files/hello',
Expand All @@ -139,23 +139,23 @@ def demo(msg)
puts "Object metas: #{o.metas}"
end

# 修改Object metas
# Modify Object metas
demo "Update object metas" do
bucket.update_object_metas(
'files/hello', {'year' => '2016', 'people' => 'jack'})
o = bucket.get_object('files/hello')
puts "Meta changed: #{o.metas}"
end

# 设置Object的ACL
# Set Object的ACL

demo "Set object ACL" do
puts "Object acl before: #{bucket.get_object_acl('files/hello')}"
bucket.set_object_acl('files/hello', Aliyun::OSS::ACL::PUBLIC_READ)
puts "Object acl now: #{bucket.get_object_acl('files/hello')}"
end

# 指定条件get_object
# get_object with conditions
demo "Get object with conditions" do
o = bucket.get_object('files/hello')

Expand Down
12 changes: 6 additions & 6 deletions examples/aliyun/oss/resumable_download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'yaml'
require 'aliyun/oss'

# 初始化OSS Bucket
# Initialize OSS Bucket
Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
conf_file = '~/.oss.yml'
conf = YAML.load(File.read(File.expand_path(conf_file)))
Expand All @@ -14,7 +14,7 @@
:access_key_id => conf['access_key_id'],
:access_key_secret => conf['access_key_secret']).get_bucket(conf['bucket'])

# 辅助打印函数
# print helper function
def demo(msg)
puts "######### #{msg} ########"
puts
Expand All @@ -24,7 +24,7 @@ def demo(msg)
end

demo "Resumable download" do
# 下载一个100M的文件
# Download a 100M file
cpt_file = '/tmp/y.cpt'
File.delete(cpt_file) if File.exist?(cpt_file)
start = Time.now
Expand All @@ -35,8 +35,8 @@ def demo(msg)
end
puts "Download complete. Cost: #{Time.now - start} seconds."

# 测试方法
# test method
# 1. ruby examples/resumable_download.rb
# 2. 过几秒后用Ctrl-C中断下载
# 3. ruby examples/resumable_download.rb恢复下载
# 2. after a few seconds, type Ctrl-C to disrupt the download.
# 3. ruby examples/resumable_download.rb to recover the download
end
14 changes: 7 additions & 7 deletions examples/aliyun/oss/resumable_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'yaml'
require 'aliyun/oss'

# 初始化OSS Bucket
# Initialize OSS Bucket
Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
conf_file = '~/.oss.yml'
conf = YAML.load(File.read(File.expand_path(conf_file)))
Expand All @@ -14,7 +14,7 @@
:access_key_id => conf['access_key_id'],
:access_key_secret => conf['access_key_secret']).get_bucket(conf['bucket'])

# 辅助打印函数
# print helper function
def demo(msg)
puts "######### #{msg} ########"
puts
Expand All @@ -25,15 +25,15 @@ def demo(msg)

demo "Resumable upload" do
puts "Generate file: /tmp/x, size: 100MB"
# 生成一个100M的文件
# Create a 100M file
File.open('/tmp/x', 'w') do |f|
(1..1024*1024).each{ |i| f.puts i.to_s.rjust(99, '0') }
end

cpt_file = '/tmp/x.cpt'
File.delete(cpt_file) if File.exist?(cpt_file)

# 上传一个100M的文件
# Upload a 100M file
start = Time.now
puts "Start upload: /tmp/x => resumable"
bucket.resumable_upload(
Expand All @@ -42,8 +42,8 @@ def demo(msg)
end
puts "Upload complete. Cost: #{Time.now - start} seconds."

# 测试方法:
# Test steps:
# 1. ruby examples/resumable_upload.rb
# 2. 过几秒后用Ctrl-C中断上传
# 3. ruby examples/resumable_upload.rb恢复上传
# 2. Type Ctrl-C to distrupt the upload after a few seconds
# 3. run ruby examples/resumable_upload.rb to recover the upload
end

0 comments on commit 278abcf

Please sign in to comment.