Skip to content

Commit

Permalink
Merge pull request #19 from xiaozhu36/region
Browse files Browse the repository at this point in the history
 change location to region id and improve endpoint
  • Loading branch information
dengqinsi committed May 17, 2018
2 parents c359b28 + a859fdb commit a4c0568
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
13 changes: 8 additions & 5 deletions README.md
Expand Up @@ -32,9 +32,7 @@ Since it's a bad practice to have your credentials in source code, you should lo
default:
:aliyun_accesskey_id: <YOUR_ACCESS_KEY_ID>,
:aliyun_accesskey_secret: <YOUR_SECRET_ACCESS_KEY>,
:aliyun_oss_endpoint: <YOUR_OSS_ENDPOINT>,
:aliyun_oss_location: <YOUR_OSS_LOACTION>,
:aliyun_oss_bucket: <YOUR_OSS_BUCKET>
:aliyun_region_id: <YOUR_TARGET_REGION>
```

### Connecting to OSS
Expand All @@ -48,12 +46,17 @@ opt = {
:provider => 'aliyun',
:aliyun_accesskey_id => <YOUR_ACCESS_KEY_ID>,
:aliyun_accesskey_secret => <YOUR_SECRET_ACCESS_KEY>,
:aliyun_oss_endpoint => <YOUR_OSS_ENDPOINT>,
:aliyun_oss_location => <YOUR_OSS_LOACTION>,
:aliyun_oss_bucket => <YOUR_OSS_BUCKET>,
:aliyun_region_id => <YOUR_TARGET_REGION>,
:aliyun_oss_endpoint => <YOUR_OSS_ENDPOINT>,
}
conn = Fog::Storage.new(opt)
```
**-> Note:** `:aliyun_region_id` is optional and default to "cn-hangzhou".
**-> Note:** `:aliyun_oss_endpoint` is optional. If it is not specified, it will be generated automatically by `:aliyun_region_id`.
Its basic format is "oss-<region-id>.aliyuncs.com" and with default schema "http" and default port "80".
If you want to use https or 443 port, you can use a format "<schema>://oss-<region-id>.aliyuncs.com:<port>".


## Fog::Aliyun Abstractions

Expand Down
52 changes: 38 additions & 14 deletions lib/fog/aliyun/storage.rb
Expand Up @@ -3,11 +3,21 @@
module Fog
module Storage
class Aliyun < Fog::Service

DEFAULT_REGION = 'cn-hangzhou'

DEFAULT_SCHEME = 'http'
DEFAULT_SCHEME_PORT = {
'http' => 80,
'https' => 443
}

recognizes :aliyun_oss_endpoint,
:aliyun_oss_location,
:aliyun_oss_bucket
:aliyun_region_id
requires :aliyun_accesskey_id,
:aliyun_accesskey_secret
:aliyun_accesskey_secret,
:aliyun_oss_bucket

model_path 'fog/aliyun/models/storage'
model :directory
Expand Down Expand Up @@ -37,9 +47,9 @@ class Real
# Initialize connection to OSS
#
# ==== Notes
# options parameter must include values for :aliyun_oss_endpoint, :aliyun_accesskey_id,
# :aliyun_secret_access_key, :aliyun_oss_location and :aliyun_oss_bucket in order to create a connection.
# if you haven't set these values in the configuration file.
# options parameter must include values for :aliyun_accesskey_id, :aliyun_secret_access_key and :aliyun_oss_bucket in order to create a connection.
# :aliyun_oss_location will be replaced by :aliyun_region_id, and it has a default value cn-hangzhou
# if :aliyun_oss_endpoint is not specified, it will be generated by method region_to_endpoint
#
# ==== Examples
# sdb = Fog::Storage.new(:provider=>'aliyun',
Expand All @@ -55,32 +65,37 @@ class Real
attr_reader :aliyun_accesskey_id
attr_reader :aliyun_accesskey_secret
attr_reader :aliyun_oss_endpoint
attr_reader :aliyun_oss_location
attr_reader :aliyun_region_id
attr_reader :aliyun_oss_bucket

def initialize(options = {})
# initialize the parameters
@aliyun_oss_endpoint = options[:aliyun_oss_endpoint]
@aliyun_oss_location = options[:aliyun_oss_location]
@aliyun_region_id = options[:aliyun_region_id] || options[:aliyun_oss_location] || DEFAULT_REGION
@aliyun_oss_endpoint = options[:aliyun_oss_endpoint] || region_to_endpoint(@aliyun_region_id)
@aliyun_accesskey_id = options[:aliyun_accesskey_id]
@aliyun_accesskey_secret = options[:aliyun_accesskey_secret]
@aliyun_oss_bucket = options[:aliyun_oss_bucket]

# check for the parameters
missing_credentials = []
missing_credentials << :aliyun_oss_endpoint unless @aliyun_oss_endpoint
missing_credentials << :aliyun_oss_location unless @aliyun_oss_location
missing_credentials << :aliyun_oss_bucket unless @aliyun_oss_bucket
missing_credentials << :aliyun_accesskey_id unless @aliyun_accesskey_id
missing_credentials << :aliyun_accesskey_secret unless @aliyun_accesskey_secret
raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?

@connection_options = options[:connection_options] || {}

endpoint = @aliyun_oss_endpoint

if !endpoint.start_with?(DEFAULT_SCHEME)
@aliyun_oss_endpoint = "#{DEFAULT_SCHEME}://#{endpoint}"
end

uri = URI.parse(@aliyun_oss_endpoint)
@host = uri.host
@path = uri.path
@port = uri.port
@scheme = uri.scheme
@scheme = uri.scheme || DEFAULT_SCHEME
@port = uri.port || DEFAULT_SCHEME_PORT[@scheme]

@persistent = options[:persistent] || false
end
Expand All @@ -89,6 +104,15 @@ def reload
@connection.reset
end

def region_to_endpoint(region=nil)
case region.to_s
when ''
"oss-#{DEFAULT_REGION}.aliyuncs.com"
else
"oss-#{region}.aliyuncs.com"
end
end

def request(params)
method = params[:method]
time = Time.new.utc
Expand Down Expand Up @@ -181,14 +205,14 @@ def sign(method, date, contentType, resource = nil, headers = nil)
class Mock
def initialize(options = {})
@aliyun_oss_endpoint = options[:aliyun_oss_endpoint]
@aliyun_oss_location = options[:aliyun_oss_location]
@aliyun_region_id = options[:aliyun_region_id]
@aliyun_accesskey_id = options[:aliyun_accesskey_id]
@aliyun_accesskey_secret = options[:aliyun_accesskey_secret]
@aliyun_oss_bucket = options[:aliyun_oss_bucket]

# missing_credentials = Array.new
# missing_credentials << :aliyun_oss_endpoint unless @aliyun_oss_endpoint
# missing_credentials << :aliyun_oss_location unless @aliyun_oss_location
# missing_credentials << :aliyun_region_id unless @aliyun_region_id
# missing_credentials << :aliyun_accesskey_id unless @aliyun_accesskey_id
# missing_credentials << :aliyun_accesskey_secret unless @aliyun_accesskey_secret
# raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?
Expand Down

0 comments on commit a4c0568

Please sign in to comment.