File: resumable_download.rb

package info (click to toggle)
ruby-aliyun-sdk 0.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 792 kB
  • sloc: ruby: 7,909; ansic: 204; makefile: 4
file content (42 lines) | stat: -rw-r--r-- 1,211 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- encoding: utf-8 -*-

$LOAD_PATH.unshift(File.expand_path("../../../../lib", __FILE__))
require 'yaml'
require 'aliyun/oss'

# 初始化OSS Bucket
Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
conf_file = '~/.oss.yml'
conf = YAML.load(File.read(File.expand_path(conf_file)))
bucket = Aliyun::OSS::Client.new(
  :endpoint => conf['endpoint'],
  :cname => conf['cname'],
  :access_key_id => conf['access_key_id'],
  :access_key_secret => conf['access_key_secret']).get_bucket(conf['bucket'])

# 辅助打印函数
def demo(msg)
  puts "######### #{msg} ########"
  puts
  yield
  puts "-------------------------"
  puts
end

demo "Resumable download" do
  # 下载一个100M的文件
  cpt_file = '/tmp/y.cpt'
  File.delete(cpt_file) if File.exist?(cpt_file)
  start = Time.now
  puts "Start download: resumable => /tmp/y"
  bucket.resumable_download(
    'resumable', '/tmp/y', :cpt_file => cpt_file) do |progress|
    puts "Progress: #{(progress * 100).round(2)} %"
  end
  puts "Download complete. Cost: #{Time.now - start} seconds."

  # 测试方法:
  # 1. ruby examples/resumable_download.rb
  # 2. 过几秒后用Ctrl-C中断下载
  # 3. ruby examples/resumable_download.rb恢复下载
end