aliyun / aliyun-oss-ruby-sdk

Aliyun OSS SDK for Ruby
http://www.rubydoc.info/gems/aliyun-sdk/
MIT License
140 stars 67 forks source link

rest-client 升级到 ~> 2.0.0 #35

Closed li-thy-um closed 7 years ago

li-thy-um commented 7 years ago

我的项目需要同时用到 阿里云 和 七牛 的 SDK,bundle 的时候报以下错误

Fetching gem metadata from https://gems.ruby-china.org/.......
Fetching version metadata from https://gems.ruby-china.org/..
Resolving dependencies....
Bundler could not find compatible versions for gem "rest-client":
  In snapshot (Gemfile.lock):
    rest-client (= 2.0.0)

  In Gemfile:
    rest-client

    aliyun-sdk was resolved to 0.1.1, which depends on
      rest-client (~> 1.8)

    qiniu (>= 6.8.0) was resolved to 6.8.1, which depends on
      rest-client (>= 2.0.0, ~> 2.0)

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.

建议将 rest-client 升级到 2.0.0 以上

rockuw commented 7 years ago

好的,不过当初rest-client 2.0.0还没有发布,我们在1.8上面做了一些hack。我们会在下一个版本中提升rest-client的版本。

li-thy-um commented 7 years ago

我现在只需要使用 STS 创建临时图片上传 token 的功能,fork 了一个 master 分支,改 gem spec

-  spec.add_dependency 'rest-client', '~> 1.8'
+  spec.add_dependency 'rest-client', '~> 2.0.0'

请问是否可行?

li-thy-um commented 7 years ago

调用 STS::Client#assume_role

     ArgumentError:
       wrong number of arguments (given 2, expected 0)
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/rest-client-2.0.0/lib/restclient/abstract_response.rb:87:in `return!'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/bundler/gems/aliyun-oss-ruby-sdk-a3a074d8c502/lib/aliyun/sts/protocol.rb:98:in `block in do_request'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/rest-client-2.0.0/lib/restclient/request.rb:858:in `process_result'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/rest-client-2.0.0/lib/restclient/request.rb:776:in `block in transmit'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/rest-client-2.0.0/lib/restclient/request.rb:766:in `transmit'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/rest-client-2.0.0/lib/restclient/request.rb:215:in `execute'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/rest-client-2.0.0/lib/restclient/request.rb:52:in `execute'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/bundler/gems/aliyun-oss-ruby-sdk-a3a074d8c502/lib/aliyun/sts/protocol.rb:87:in `do_request'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/bundler/gems/aliyun-oss-ruby-sdk-a3a074d8c502/lib/aliyun/sts/protocol.rb:45:in `assume_role'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/bundler/gems/aliyun-oss-ruby-sdk-a3a074d8c502/lib/aliyun/sts/client.rb:32:in `assume_role'
     # ./app/services/generate_cdn_upload_token_service.rb:51:in `generate_token'
     # ./app/services/generate_cdn_upload_token_service.rb:11:in `block in token'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/dalli-2.7.6/lib/active_support/cache/dalli_store.rb:113:in `block in fetch'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/dalli-2.7.6/lib/active_support/cache/dalli_store.rb:371:in `block in instrument'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.0.1/lib/active_support/notifications.rb:166:in `instrument'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/dalli-2.7.6/lib/active_support/cache/dalli_store.rb:371:in `instrument'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/dalli-2.7.6/lib/active_support/cache/dalli_store.rb:112:in `fetch'
     # ./app/services/generate_cdn_upload_token_service.rb:10:in `token'
     # ./spec/services/generate_cdn_upload_token_service_spec.rb:18:in `block (4 levels) in <top (required)>'
     # ./spec/services/generate_cdn_upload_token_service_spec.rb:21:in `block (4 levels) in <top (required)>'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/zeus-0.15.10/lib/zeus/rails.rb:207:in `test'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/zeus-0.15.10/lib/zeus.rb:147:in `block in command'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/zeus-0.15.10/lib/zeus.rb:134:in `fork'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/zeus-0.15.10/lib/zeus.rb:134:in `command'
     # /Users/lithium/.rvm/gems/ruby-2.3.1/gems/zeus-0.15.10/lib/zeus.rb:50:in `go'
     # -e:1:in `<main>'
li-thy-um commented 7 years ago

查了一下发现 rest-client 改接口了

return! 这里不收 request 和 result 了

改掉以后能正常获取 token

diff --git a/lib/aliyun/sts/protocol.rb b/lib/aliyun/sts/protocol.rb
index 5031887..fff3711 100644
--- a/lib/aliyun/sts/protocol.rb
+++ b/lib/aliyun/sts/protocol.rb
@@ -95,7 +95,7 @@ module Aliyun
             logger.error(e.to_s)
             raise e
           else
-            response.return!(request, result, &blk)
+            response.return!(&blk)
           end
         end
li-thy-um commented 7 years ago

调用

@client.get_bucket(bucket_name).put_object(file_name, file: file_path)

报错

ArgumentError: wrong number of arguments (given 2, expected 0..1)
from /Users/lithium/.rvm/gems/ruby-2.3.1/bundler/gems/aliyun-oss-ruby-sdk-8602c6e7ca53/lib/aliyun/oss/http.rb:128:in `read'
li-thy-um commented 7 years ago

修改后上传接口返回 true,但是 bucket 中相关图片大小为 0 KB

diff --git a/lib/aliyun/oss/http.rb b/lib/aliyun/oss/http.rb
index d33f2f8..7fa207f 100644
--- a/lib/aliyun/oss/http.rb
+++ b/lib/aliyun/oss/http.rb
@@ -125,7 +125,7 @@ module Aliyun
           @stream = StreamWriter.new(crc_enable, init_crc, &block)
         end

-        def read(bytes = nil)
+        def read(*args)
           @stream
         end

@@ -296,7 +296,7 @@ module Aliyun
             logger.error(e.to_s)
             raise e
           else
-            response.return!(request, result, &blk)
+            response.return!(&blk)
           end
         end
li-thy-um commented 7 years ago

https://github.com/rest-client/rest-client/blob/2.0.x/lib/restclient/payload.rb

因为这里

  class Streamed < Base
      def build_stream(params = nil)
        @stream = params
      end

      def size
        if @stream.respond_to?(:size)
          @stream.size
        elsif @stream.is_a?(IO)
          @stream.stat.size
        end
      end

      alias :length :size
    end
@stream 是一个 Aliyun::OSS::HTTP::StreamPayload

size 返回 nil
li-thy-um commented 7 years ago
diff --git a/lib/aliyun/oss/http.rb b/lib/aliyun/oss/http.rb
index d33f2f8..dd1e7d6 100644
--- a/lib/aliyun/oss/http.rb
+++ b/lib/aliyun/oss/http.rb
@@ -105,6 +105,9 @@ module Aliyun
           false
         end

+        def close
+        end
+
         def inspect
           "@buffer: " + @buffer[0, 32].inspect + "...#{@buffer.size} bytes"
         end
@@ -125,7 +128,7 @@ module Aliyun
           @stream = StreamWriter.new(crc_enable, init_crc, &block)
         end

-        def read(bytes = nil)
+        def read(*args)
           @stream
         end

@@ -296,7 +299,7 @@ module Aliyun
             logger.error(e.to_s)
             raise e
           else
-            response.return!(request, result, &blk)
+            response.return!(&blk)
           end
         end
li-thy-um commented 7 years ago
diff --git a/lib/aliyun/oss/protocol.rb b/lib/aliyun/oss/protocol.rb
index d47a857..25d9349 100644
--- a/lib/aliyun/oss/protocol.rb
+++ b/lib/aliyun/oss/protocol.rb
@@ -535,7 +535,7 @@ module Aliyun
           headers[CALLBACK_HEADER] = opts[:callback].serialize
         end

-        payload = HTTP::StreamPayload.new(@config.crc_enable, opts[:init_crc], &block)
+        payload = HTTP::StreamPayload.new(@config.crc_enable, opts[:init_crc], &block).read
         r = @http.put(
           {:bucket => bucket_name, :object => object_name},
           {:headers => headers, :body => payload})
@@ -546,7 +546,7 @@ module Aliyun
           raise e
         end
li-thy-um commented 7 years ago

这样可以上传了

https://github.com/rest-client/rest-client/commit/59af2eba463110b7b3c6bb0a76ca02e25927d383

hanyong37 commented 7 years ago

请问下个版本什么时候发?我也碰到同样的问题! @li-thy-um 你是下载源码自己改吗?具体怎么弄的呢?

li-thy-um commented 7 years ago

@hanyong37 参考一下这个 https://github.com/TapasTech/aliyun-oss-ruby-sdk

我只测试了

三个接口

defp commented 7 years ago

image

+1

Xuhao commented 7 years ago

Implemented here #46

rockuw commented 7 years ago

Fixed @0.6.0