Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 78 lines (60 sloc) 2.7 kB
866e6b5 中文注释演示
mangege authored
1 # -*- encoding : utf-8 -*-
2 require 'aliyun/oss'
3 include Aliyun::OSS
4
5 #连接信息
6 Aliyun::OSS::Base.establish_connection!(
7 :server => 'oss.aliyuncs.com', #可不填,默认为此项
8 :access_key_id => 'qhK37WBkar7QikAq',
9 :secret_access_key => 'zHGQkww2w14u27lOvht3eeqROEs5QS'
10 )
11
12 #bucket
13 Service.buckets #罗列Bucket
14 Bucket.list
15
16 Bucket.create('kitty') #创建Bucket
17 Bucket.create('kitty', access: 'public-read') #创建Bucket时指定权限,如果此Bucket,则只修改权限
18
7c282b0 @purplebamboo Update demo.rb
purplebamboo authored
19 kitty = Bucket.find('kitty') #查找Bucket
866e6b5 中文注释演示
mangege authored
20
21 kitty.objects #罗列此Bucket的所有文件 TODO size
22 Bucket.objects('kitty')
23 kitty.objects(:marker => 'm', :max_keys => 2, :prefix => 'jazz') #可选的参数
24
25 obj = kitty.new_object #在此Bucket新建Object
26 obj.key = 'test.txt'
27 obj.value = 'hello world'
28 obj.store
29
30 kitty.find('test.txt')
31 kitty['test.txt'] #根据object name获取此Bucket的object
32 kitty.each{ |o| puts o} #迭代此Bucket下的所有object
33
34 kitty.acl #获取权限
35 kitty.acl(:public_read_wirte) #修改权限
36
37 kitty.delete #删除此bucket,不为空报错
38 kitty.delete_all #清空bucket
39
40 require 'open-uri'
41 aliyun_logo = open('http://static.aliyun.com/images/www-summerwind/logo.gif')
42
43 #object
44 OSSObject.store('aliyun_logo.gif', aliyun_logo, 'kitty') #上传新文件
45 OSSObject.store('aliyun_logo.gif', aliyun_logo, 'kitty', content_type: 'application/octet-stream') #手动指定Content-Type
46
47 OSSObject.exists?('aliyun_logo.gif', 'kitty') #判断文件是否存在
48 logo = OSSObject.find('aliyun_logo.gif', 'kitty') #查找文件,不下载文件内容
49 logo.value #下载文件
50 OSSObject.value('aliyun_logo.gif', 'kitty') #直接下载文件
51
52 open('song.mp3', 'w') do |file| #边下载边保存
53 OSSObject.stream('song.mp3', 'jukebox') do |chunk|
54 file.write chunk
55 end
56 end
57
58 logo.about #获取文件信息,content-type,content-length等
59 logo.metadata #获取用户自定义metadata
60 logo.metadata[:v] = 1 #设置自定义metadata
61 logo.store
62 OSSObject.store('aliyun_logo.gif', aliyun_logo, 'kitty', 'x-oss-meta-v2' => 2) #上传文件时就设置metadata
63
64 OSSObject.copy('aliyun_logo.gif', 'logo.gif', 'kitty') #复制文件
65 OSSObject.rename('logo.gif', 'aliyun.gif', 'kitty') #重命名文件, 支持实例方法 logo.rename('aliyun.gif')
66 OSSObject.delete('aliyun.gif', 'kitty') #删除文件
67
68 logo.url #url签名
69 logo.url(:authenticated => false) #不包含签名信息 http://oss.aliyuncs.com/kitty/aliyun_logo.gif"
70 OSSObject.url_for('aliyun_logo.gif', 'kitty', :expires_in => 30) #30秒过期
71
72 #设置默认的bucket
73 class JukeBoxSong < Aliyun::OSS::OSSObject
74 set_current_bucket_to 'jukebox'
75 end
76 other_song = 'baby-please-come-home.mp3'
77 JukeBoxSong.store(other_song, open(other_song))
Something went wrong with that request. Please try again.