• Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 9 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Aliyun OSS SDK for Ruby

Alibaba Cloud OSS SDK for Ruby

Gem Version Build Status Coverage Status

README of Chinese

About

Alibaba Cloud OSS SDK for Ruby is a Ruby client program for convenient access to Alibaba Cloud OSS (Object Storage Service) RESTful APIs. For more information about OSS, visit the OSS official website.

Run environment

  • Ruby 2.0 or above. For Ruby 1.9, please use v0.5.0.
  • Windows, Linux or OS X system that supports Ruby.

To learn how to install Ruby, refer to: ruby-lang.

Quick start

Activate an OSS account

Log onto the official website and click Activate Now. Follow the prompts to activate OSS. After the service is activated, go to Console to view your AccessKeyId and AccessKeySecret. These two information items are required when you use Alibaba Cloud OSS SDK.

Install Alibaba Cloud OSS SDK for Ruby

gem install aliyun-sdk

Include the following in your project or 'irb' command:

require 'aliyun/oss'

Note:

  • Some gems on which the SDK depends are local extensions, and you need to install ruby-dev to compile locally extended gems after you install Ruby.
  • The environment for running the SDK-dependent gem (nokogiri) for processing XML must have the zlib library.

The following method is used to install the preceding dependencies taking Ubuntu as an example:

sudo apt-get install ruby-dev
sudo apt-get install zlib1g-dev

The practices for other systems are similar.

Create a client

client = Aliyun::OSS::Client.new(
  :endpoint => 'endpoint',
  :access_key_id => 'access_key_id',
  :access_key_secret => 'access_key_secret')

In specific, the endpoint is the OSS service address. The address may vary based on different regions for the node. For example, the address for a Hangzhou node is: http://oss-cn-hangzhou.aliyuncs.com. For addresses for other nodes, see: Node List.

The access_key_id and access_key_secret are credentials for your service. You can view them in Console on the official website. Please keep your AccessKeySecret safe. Disclosing the AccessKeySecret may compromise your data security.

Use a bound domain as the endpoint

OSS supports binding a custom domain name and allows you to direct your domain name to the OSS service address (CNAME) of Alibaba Cloud. In this way, you don't need to change the resource path in your app when migrating your data to the OSS. The bound domain name points to a bucket in the OSS. The domain name binding operation can only be carried out in the OSS console. For more information about binding a custom domain name, visit the official website: Binding Custom Domain Names in OSS.

After you have bound a custom domain name, you can use the standard OSS service address as the specified endpoint of the OSS, or use the bound domain name:

client = Aliyun::OSS::Client.new(
  :endpoint => 'http://img.my-domain.com',
  :access_key_id => 'access_key_id',
  :access_key_secret => 'access_key_secret',
  :cname => true)

Note:

  • You must set the cname to true when initializing the client.
  • The custom domain name is bound to a bucket of the OSS, so the client created in this method does not support List_buckets operations.
  • You still need to specify the bucket name during the {Aliyun::OSS::Client#get_bucket} operation and the bucket name should be the same as that bound to the domain name.

Create a client using STS

OSS supports access via STS. For more information about STS, refer to Alibaba Cloud STS. Before using STS, you must apply for a temporary token from the STS. Alibaba Cloud Ruby SDK contains the STS SDK, and you only need to require 'aliyun/sts' for usage:

require 'aliyun/sts'
sts = Aliyun::STS::Client.new(
  access_key_id: 'access_key_id',
  access_key_secret: 'access_key_secret')

token = sts.assume_role('role-arn', 'my-app')

client = Aliyun::OSS::Client.new(
  :endpoint => 'http://oss-cn-hangzhou.aliyuncs.com',
  :access_key_id => token.access_key_id,
  :access_key_secret => token.access_key_secret,
  :sts_token => token.security_token)

Note: the :sts_token parameter must be specified for using STS. You can also apply for a token with a policy through STS::Client, for details, refer to API Documentation.

List all the current buckets

buckets = client.list_buckets
buckets.each{ |b| puts b.name }

The list_buckets command returns an iterator for you to get the information of each bucket in order. Bucket For the object structure, see {Aliyun::OSS::Bucket} in the API documentation.

Create a bucket

bucket = client.create_bucket('my-bucket')

List all the objects in a bucket

bucket = client.get_bucket('my-bucket')
objects = bucket.list_objects
objects.each{ |o| puts o.key }

The list_objects command returns an iterator for you to get the information of each object in order. Object For the object structure, see {Aliyun::OSS::Object} in the API documentation.

Create an object in the bucket

bucket.put_object(object_key){ |stream| stream << 'hello world' }

You can also create an object by uploading a local file:

bucket.put_object(object_key, :file => local_file)

Download an object from the bucket

bucket.get_object(object_key){ |content| puts content }

You can also download the object to a local file:

bucket.get_object(object_key, :file => local_file)

Copy an object

bucket.copy_object(from_key, to_key)

Identify whether an object exists

bucket.object_exists?(object_key)

For more operations on buckets, refer to {Aliyun::OSS::Bucket} in the API documentation.

Simulate the directory structure

OSS is a storage service for objects and does not support the directory structure. All objects are flatly structured. But you can simulate the directory structure by setting the object key in the format "foo/bar/file". Suppose there are several objects as follows:

foo/x
foo/bar/f1
foo/bar/dir/file
foo/hello/file

Listing all the objects under the "foo/" directory means to perform the list_objects operation with "foo/" as the prefix. But this method will also list all the objects under "foo/bar/". That's why we need the delimiter parameter. This parameter means to stop processing at the first delimiter after the prefix. The key during the process acts as the common prefix of objects, objects with the prefix will be included in the list_objects result.

objs = bucket.list_objects(:prefix => 'foo/', :delimiter => '/')
objs.each do |i|
  if i.is_a?(Aliyun::OSS::Object) # a object
    puts "object: #{i.key}"
  else
    puts "common prefix: #{i}"
  end
end
# output
object: foo/x
common prefix: foo/bar/
common prefix: foo/hello/

Common prefixes free you from traversing all the objects (the number of objects may be huge) to determine the prefix, and is quite helpful for simulating the directory structure.

Upload callback

You can specify a callback for put_object and resumable_upload so that after the file is successfully uploaded to the OSS, the OSS will initiate an HTTP POST request to the server address you provided to notify you that the corresponding event has occurred. You can perform desired actions after receiving the notification, such as updating the database and making statistics. For more details about upload callback, refer to OSS Upload Callback.

The example below demonstrates how to use the upload callback:

callback = Aliyun::OSS::Callback.new(
  url: 'http://10.101.168.94:1234/callback',
  query: {user: 'put_object'},
  body: 'bucket=${bucket}&object=${object}'
)

begin
  bucket.put_object('files/hello', callback: callback)
rescue Aliyun::OSS::CallbackError => e
  puts "Callback failed: #{e.message}"
end

Note:

  • The callback URL must not contain the query string which must be specified in the :query parameter.
  • In the event that the file is successfully uploaded but callback execution fails, the client will throw CallbackError. To ignore the error, you need to explicitly catch the exception.
  • For detailed examples, refer to callback.rb.
  • For servers that support callback, refer to callback_server.rb.

Resumable upload/download

OSS supports the storage of large objects. If the upload/download task of a large object is interrupted (due to network transient disconnections, program crashes, or machine power-off), the re-upload/re-download is taxing on system resources. The OSS supports multipart upload/download to divide a large object into multiple parts for upload/download. Alibaba Cloud OSS SDK provides the resumable upload/download feature based on this principle. If an interruption occurs, you can resume the upload/download task beginning with the interrupted part. Resumable upload/download is recommended for objects larger than 100MB.

Resumable upload

bucket.resumable_upload(object_key, local_file, :cpt_file => cpt_file)

In specific, :cpt_file specifies the location of the checkpoint object which stores the intermediate state of the upload. If no object is specified, the SDK will generate a local_file.cpt in the directory of the local_file. After the upload interruption, you only need to provide the same cpt object for the upload task to resume from the interrupted part. The typical upload code is:

retry_times = 5
retry_times.times do
  begin
    bucket.resumable_upload(object_key, local_file)
  rescue => e
    logger.error(e.message)
  end
end

Notes:

  • The SDK records the upload intermediate states in the cpt object. Therefore, ensure that you have write permission on the cpt object.
  • The cpt object records the intermediate state information of the upload and has a self-checking function. You cannot edit the object. Upload will fail if the cpt object is corrupted. When the upload is completed, the checkpoint file will be deleted.

Resumable download

bucket.resumable_download(object_key, local_file, :cpt_file => cpt_file)

In specific, :cpt_file specifies the location of the checkpoint object which stores the intermediate state of the download. If no object is specified, the SDK will generate a local_file.cpt in the directory of the local_file. After the download interruption, you only need to provide the same cpt object for the download task to resume from the interrupted part. The typical download code is:

retry_times = 5
retry_times.times do
  begin
    bucket.resumable_download(object_key, local_file)
  rescue => e
    logger.error(e.message)
  end
end

Notes:

  • During the download process, a temporary object of local_file.part.N will be generated in the directory of the local_file for each part downloaded. When the download is completed, the objects will be deleted. You cannot edit or delete the part objects, otherwise the download will not proceed.
  • The SDK records the download intermediate states in the cpt object; therefore, ensure that you have write permission on the cpt object.
  • The cpt object records the intermediate state information of the download and has a self-checking function. You cannot edit the object. Download will fail if the cpt object is corrupted. When the download is completed, the checkpoint object will be deleted.

Appendable object

Objects in Alibaba Cloud OSS can be divided into two types: Normal and Appendable.

  • A normal object functions as a whole for every upload. If an object already exists, the later uploaded object will overwrite the previous object with the same key.
  • An appendable object is created through append_object for the first time. The later uploaded object through append_object will not overwrite the previous one, but will append content to the end of the object.
  • You cannot append content to a normal object.
  • You cannot copy an appendable object.

Create an appendable object

bucket.append_object(object_key, 0){ |stream| stream << "hello world" }

The second parameter indicates the position to append the content. This parameter is 0 for the first append to the object. In later append operations, the value of this parameter is the length of the object before the append.

Of course, you can also read the appended content from the object:

bucket.append_object(object_key, 0, :file => local_file)

Append content to the object

pos = bucket.get_object(object_key).size
next_pos = bucket.append_object(object_key, pos, :file => local_file)

During the first append, you can use {Aliyun::OSS::Bucket#get_object} to get the object length. For later append operations, you can refer to the response of {Aliyun::OSS::Bucket#append_object} to determine the length value for next append.

Note: Concurrent append_object and next_pos operations do not always produce correct results.

Object meta information

Besides the object content, the OSS also allows you to set some meta information for the object during object uploading. The meta information is a key-value pair to identify the specific attributes of the object. The meta information will be stored together with the object and returned to users in get_object and get_object_meta operations.

bucket.put_object(object_key, :file => local_file,
                  :metas => {
                    'key1' => 'value1',
                    'key2' => 'value2'})

obj = bucket.get_object(object_key, :file => localfile)
puts obj.metas

Note:

  • The key and value of the meta information can only be simple ASCII non-newline characters and the total size must not exceed 8KB.
  • In the copy object operation, the meta information of the source object will be copied by default. If you don't want this, explicitly set the :meta_directive to {Aliyun::OSS::MetaDirective::REPLACE}.

Permission control

OSS allows you to set access permissions for buckets and objects respectively, so that you can conveniently control external access to your resources. A bucket is enabled with three types of access permissions:

  • public-read-write: Anonymous users are allowed to create/retrieve/delete objects in the bucket.
  • public-read: Anonymous users are allowed to retrieve objects in the bucket.
  • private: Anonymous users are not allowed to access the bucket. Signature is required for all accesses.

When a bucket is created, the private permission applies by default. You can use 'bucket.acl=' to set the ACL of the bucket.

bucket.acl = Aliyun::OSS::ACL::PUBLIC_READ
puts bucket.acl # public-read

An object is enabled with four types of access permissions:

  • default: The object inherits the access permissions of the bucket it belongs to, that is, the access permission of the object is the same as that of the bucket where the object is stored.
  • public-read-write: Anonymous users are allowed to read/write the object.
  • public-read: Anonymous users are allowed to read the object.
  • private: Anonymous users are not allowed to access the object. Signature is required for all accesses.

When an object is created, the default permission applies by default. You can use 'bucket.set_object_acl' to configure the ACL of the object.

acl = bucket.get_object_acl(object_key)
puts acl # default
bucket.set_object_acl(object_key, Aliyun::OSS::ACL::PUBLIC_READ)
acl = bucket.get_object_acl(object_key)
puts acl # public-read

Notes:

  • If an object is configured with an ACL policy, the object ACL takes priority during permission authentication when the object is accessed. The bucket ACL will be ignored.

  • If anonymous access is allowed (public-read or public-read-write is configured for the object), you can directly access the object using a browser. For example,

      http://bucket-name.oss-cn-hangzhou.aliyuncs.com/object.jpg
    
  • A bucket or an object with the public permission can be accessed by an anonymous client which is created with the following code:

      # If access_key_id and access_key_secret are not specified, an anonymous client will be created. The client can only access 
      # the buckets and objects with the public permission.
      client = Client.new(:endpoint => 'oss-cn-hangzhou.aliyuncs.com')
      bucket = client.get_bucket('public-bucket')
      obj = bucket.get_object('public-object', :file => local_file)
    

Run examples

Some example projects are provided in the examples/ directory of the SDK to demonstrate the SDK features. You can run the examples after some configuration. The permission information and the bucket information required by the examples are available in the ~/.oss.yml configuration file under the HOME directory. The information should include the following fields (Note the space after the colon):

endpoint: oss-cn-hangzhou.aliyuncs.com
cname: false
access_key_id: <ACCESS KEY ID>
access_key_secret: <ACCESS KEY SECRET>
bucket: <BUCKET NAME>

You need to create (if not in existence) or modify the content and run the example project:

ruby examples/aliyun/oss/bucket.rb

Run test

bundle exec rake spec

export RUBY_SDK_OSS_ENDPOINT=endpoint
export RUBY_SDK_OSS_ID=AccessKeyId
export RUBY_SDK_OSS_KEY=AccessKeySecret
export RUBY_SDK_OSS_BUCKET=bucket-name

bundle exec rake test

License

  • MIT

More

For more documentation, see:

More Repositories

1

oss-browser

OSS Browser 提供类似windows资源管理器功能。用户可以很方便的浏览文件,上传下载文件,支持断点续传等。
JavaScript
3,175
star
2

aliyun-openapi-java-sdk

Alibaba Cloud SDK for Java
Java
1,379
star
3

aliyun-oss-java-sdk

Aliyun OSS SDK for Java
Java
1,216
star
4

alibaba-cloud-sdk-go

Alibaba Cloud SDK for Go
Go
1,104
star
5

alicloud-android-demo

Java
990
star
6

aliyun-openapi-python-sdk

Alibaba Cloud SDK for Python
Python
980
star
7

aliyun-oss-php-sdk

Aliyun OSS SDK for PHP
PHP
975
star
8

aliyun-oss-go-sdk

Aliyun OSS SDK for Go
Go
951
star
9

aliyun-oss-python-sdk

Aliyun OSS SDK for Python
Python
935
star
10

darabonba

Darabonba 是一种用于 OpenAPI 的 DSL 语言,可以用来生成多语言的 SDK、Code Sample、Test Case 等代码
JavaScript
894
star
11

alibabacloud-alfa

阿里云微前端解决方案
TypeScript
845
star
12

aliyun-oss-android-sdk

Android SDK for aliyun object storage service
Java
793
star
13

aliyun-cli

Alibaba Cloud CLI
Go
770
star
14

ossfs

Export s3fs for aliyun oss.
C++
735
star
15

aliyun-openapi-php-sdk

[Abandoned] Open API SDK for PHP developers
PHP
605
star
16

terraform-provider-alicloud

Terraform AliCloud provider
Go
590
star
17

aliyun-openapi-net-sdk

Alibaba Cloud SDK for .NET
C#
535
star
18

rds_dbsync

围绕 PostgreSQL Greenplum ,实现易用的数据的互迁功能项目
C
528
star
19

openapi-sdk-php

Alibaba Cloud SDK for PHP
PHP
501
star
20

iotkit-embedded

高速镜像: https://code.aliyun.com/linkkit/c-sdk
C
492
star
21

ossutil

A user friendly command line tool to access AliCloud OSS.
Go
456
star
22

aliyun-oss-ios-sdk

iOS SDK for aliyun object storage service
Objective-C
450
star
23

aliyun-odps-python-sdk

ODPS Python SDK and data analysis framework
Python
433
star
24

alicloud-ios-demo

Demos for AMS iOS SDKs
Objective-C
431
star
25

alibabacloud-microservice-demo

An Alibaba Cloud native microservice demo powered by Apache Dubbo and Spring Cloud Alibaba
Java
379
star
26

api-gateway-demo-sign-java

aliyun api gateway request signature demo by java
Java
371
star
27

NeWCRFs

Python
365
star
28

aliyun-oss-csharp-sdk

Aliyun OSS SDK for C#
C#
360
star
29

surftrace

surftrace is a tool that allows you to surf the linux kernel
Python
332
star
30

conditional-lane-detection

Python
328
star
31

aliyun-log-jaeger

Go
294
star
32

coolbpf

C
240
star
33

tablestore-examples

Example code for aliyun tablestore.
Java
238
star
34

tablestore-timeline

TableStore-Timeline Model for Social scene
Java
236
star
35

aliyun-log-c-sdk

Aliyun LOG Producer for C/C++
C
215
star
36

openapi-sdk-php-client

Official repository of the Alibaba Cloud Client for PHP
PHP
214
star
37

aliyun-log-logback-appender

Java
186
star
38

aliyun-log-android-sdk

Java
179
star
39

alibabacloud-jindodata

alibabacloud-jindodata
176
star
40

openapi-core-nodejs-sdk

OpenAPI POP core SDK for Node.js
JavaScript
175
star
41

aliyun-emapreduce-datasources

Extended datasource support for Spark/Hadoop on Aliyun E-MapReduce.
Scala
168
star
42

aliyun-log-python-sdk

Use python to manage, produce and consume data with Aliyun Log Service.
Python
166
star
43

data-mapping-component

A React Component which focus on Data-Mapping & Table-Field-Mapping.(基于React的数据/表字段映射组件)
JavaScript
155
star
44

aliyun-oss-react-native

Objective-C
148
star
45

aliyun-apsaradb-hbase-demo

C++
146
star
46

django-oss-storage

Django storage backends for AliCloud OSS.
Python
144
star
47

aliyun-oss-c-sdk

Aliyun OSS SDK for C
C
144
star
48

react-visual-modeling

A DAG React Component for visualization modeling, suitable for UML, database modeling, data warehouse construction.(一个基于React的数据可视化建模的DAG图,适用于UML,数据库建模,数据仓库建设等业务)
JavaScript
138
star
49

ram-policy-editor

AliCloud RAM Policy Editor for OSS
JavaScript
136
star
50

aliyun-log-java-sdk

Java
135
star
51

serverless-aliyun-function-compute

Serverless Alibaba Cloud Function Compute Plugin – Add Alibaba Cloud Function Compute support to the Serverless Framework
JavaScript
134
star
52

alibabacloud-console-components

阿里云企业云管理平台 UI 组件库
TypeScript
133
star
53

aliyun-log-java-producer

Aliyun LOG Java Producer
Java
131
star
54

fc-nodejs-sdk

The Node.js SDK of FunctionCompute.
JavaScript
130
star
55

aliyun-cms-grafana

JavaScript
127
star
56

aliyun-odps-jdbc

JDBC Driver for ODPS
Java
125
star
57

alibabacloud-quantization-networks

alibabacloud-quantization-networks
Python
122
star
58

aliyun-emapreduce-demo

Java
121
star
59

aliyun-maxcompute-data-collectors

Java
119
star
60

alicloud-ams-demo

C#
117
star
61

alibabacloud-iot-device-sdk

alibaba cloud for iot device javascript SDK , connect with linkplatform , run at node/broswer/winxin min program /ali min program. 阿里云IoT物联网平台javascript版本sdk,可以运行在node/broswer/winxin min program /ali min program. 阿里云IoT物联网平台javascript版本sdk,可以运行在node/broswer/winxin min program /ali min program
JavaScript
110
star
62

MaxCompute-Spark

MaxCompute spark demo for building a runnable application.
Scala
106
star
63

api-gateway-nodejs-sdk

The API Gateway SDK for Node.js
JavaScript
104
star
64

cloud-design

阿里云前端组件库,由专有云&公有云前端团队共建
CSS
99
star
65

gm-jsse

开源国密通信纯 Java JSSE 实现
Java
95
star
66

aliyun-odps-console

ODPS Console Source Code.
Java
93
star
67

aliyun-openapi-cpp-sdk

Alibaba Cloud SDK for C++
C++
90
star
68

aliyun-odps-java-sdk

ODPS SDK for Java Developers
Java
89
star
69

aliyun-tablestore-nodejs-sdk

Aliyun TableStore(原OTS) SDK for Node.js
JavaScript
88
star
70

algorithm-base

让算法工程化更简单
Python
86
star
71

aliyun-log-ios-sdk

Aliyun LOG iOS SDK
Swift
84
star
72

iotx-api-demo

PHP
82
star
73

plugsched

Live upgrade Linux kernel scheduler subsystem
Python
82
star
74

DCT-Mask

Python
81
star
75

aliyun-openapi-nodejs-sdk

Alibaba Cloud SDK for Node.js
JavaScript
80
star
76

aliyun-specs

Aliyun Mobile Service CocoaPods specs.
Ruby
77
star
77

alibabacloud-console-design

阿里云管平台研发解决方案
TypeScript
77
star
78

alibabacloud-redis-training-demo

Java
76
star
79

aliyun-oss-php-sdk-laravel

A Laravel service provider for the AliCloud OSS SDK for PHP
PHP
75
star
80

aliyun-tablestore-go-sdk

TableStore SDK for Golang
Go
75
star
81

alibabacloud-sdk

Tea
75
star
82

fc-docker

Dockerfiles for local building or running function of FC
Dockerfile
74
star
83

elasticsearch-repository-oss

Java
74
star
84

dro-sfm

Python
74
star
85

packagist-mirror

Alibaba Cloud Packagist Mirror
Go
73
star
86

react-monitor-dag

A React-based operation/monitoring DAG diagram.(基于React的运维/监控DAG图)
JavaScript
69
star
87

aliyun_assist_client

Aliyun Assist Client 阿里云 云助手
Go
67
star
88

aliyun-log-php-sdk

PHP
67
star
89

alibabacloud-hologres-connectors

alibabacloud-hologres-connectors
Java
66
star
90

aliyun-tsdb-java-sdk

Aliyun TSDB SDK for Java
Java
64
star
91

aliyun-log-log4j-appender

aliyun-log-log4j-appender
Java
63
star
92

fc-java-sdk

The Java SDK of FunctionCompute.
Java
61
star
93

oss-ftp

The ftp proxy for Aliyun OSS.
Python
61
star
94

react-lineage-dag

JavaScript
61
star
95

aliyun-log-cli

Command Line Interface for Aliyun Log Service
Python
60
star
96

csb-sdk

The CSB-SDK is a client-side invocation SDK for HTTP or Web Service API opened by the CSB (Cloud Service Bus) product. It is responsible for invoking the open API and signing the request information.
Java
58
star
97

ossimport

Data migration tool
58
star
98

aliyun-log-flink-connector

flink log connector
Java
58
star
99

oss-emulator

OSS Emulator
Ruby
58
star
100

aliyun-log-dotnetcore-sdk

C#
55
star