ali.rb 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # encoding:utf-8
  2. require 'open-uri'
  3. require 'net/http'
  4. require 'uri'
  5. require 'json'
  6. require 'digest/md5'
  7. require 'digest/sha1'
  8. require 'aliyun/oss'
  9. module Ali
  10. class Oss
  11. BUCKET_NAME_PUBLIC_READ = CONFIG_FILE["ali_oss_bucket"]
  12. URL_FOR_HOST = "#{CONFIG_FILE["ali_oss_bucket"]}.#{CONFIG_FILE["ali_oss_data_center"]}.aliyuncs.com"
  13. CDN_URL_FOR_HOST = URL_FOR_HOST
  14. @client = Aliyun::OSS::Client.new(
  15. :endpoint => "#{CONFIG_FILE["ali_oss_data_center"]}.aliyuncs.com",
  16. :access_key_id => CONFIG_FILE["ali_access_id"],
  17. :access_key_secret => CONFIG_FILE["ali_access_secret"])
  18. class << self
  19. def create_bucket(name, access = Aliyun::OSS::ACL::PUBLIC_READ)
  20. begin
  21. bucket = @client.create_bucket(name)
  22. bucket.acl = access
  23. rescue => e
  24. p e
  25. end
  26. return bucket
  27. end
  28. def get_bucket(name)
  29. buckets = @client.list_buckets
  30. buckets.each do |item|
  31. if item.name == name
  32. return item
  33. end
  34. end
  35. return nil
  36. end
  37. def store(bucket_name, object_key, data)
  38. bucket = self.get_bucket(bucket_name)
  39. if bucket.blank?
  40. bucket = self.create_bucket(bucket_name)
  41. end
  42. bucket.put_object(object_key){ |stream| stream << data }
  43. end
  44. def object_exist?(bucket_name, object_key)
  45. bucket = self.get_bucket(bucket_name)
  46. if not bucket.blank?
  47. objects = bucket.list_objects
  48. objects.each do |obj|
  49. if obj.key == object_key
  50. return true
  51. end
  52. end
  53. return false
  54. else
  55. raise "Bucket is not found with name: #{bucket_name}"
  56. end
  57. end
  58. def delete_object(bucket_name, object_key)
  59. bucket = self.get_bucket(bucket_name)
  60. if not bucket.blank?
  61. begin
  62. bucket.delete_object(object_key)
  63. rescue Exception => e
  64. p e
  65. end
  66. else
  67. raise "Bucket is not found with name: #{bucket_name}"
  68. end
  69. end
  70. end
  71. end
  72. class Css
  73. # attr_reader :version, :host
  74. # attr_accessor :client_id, :secret_key
  75. ACCESS_KEY_ID = "HNPD1U7r1eWPzB82"
  76. SECRET_ACCESS_KEY = "yhmPqp3jBndWf8D9b5p40BRyNyqYG6"
  77. #内网
  78. # HOST = "http://intranet.opensearch-cn-hangzhou.aliyuncs.com"
  79. #外网
  80. HOST = {
  81. "production" => "http://opensearch-cn-hangzhou.aliyuncs.com",
  82. "development" => "http://opensearch-cn-qingdao.aliyuncs.com"
  83. }
  84. class << self
  85. #根据参数创建签名信息
  86. def _signature(params, method)
  87. _params = {}
  88. params.each do |k,v|
  89. _params[k.to_s] = v
  90. end
  91. _params = _params.sort
  92. data = []
  93. _params.each do |p|
  94. next if p[1].size <= 0
  95. # p "before encode:#{p[1]}" if p[0] == "items"
  96. _p = URI.encode_www_form_component(p[1])
  97. _p = _p.gsub("+","%20").gsub("*","%2A").gsub("%7E","~")
  98. # p "after encode:#{_p}" if p[0] == "items"
  99. data << "#{URI.encode_www_form_component(p[0])}=#{_p}"
  100. end
  101. data = URI.encode_www_form_component(data.join("&"))
  102. data = [method.upcase, URI.encode_www_form_component("/"), data].join("&")
  103. # p "stringtosign=#{data}"
  104. data = data.gsub("%7E","~")
  105. signature = Base64::encode64(HMAC::SHA1.digest("#{SECRET_ACCESS_KEY}&", data))
  106. # p "signature=#{signature[0..-2]}"
  107. return signature[0..-2]
  108. end
  109. def _api_call(url, method = 'GET', params = {})
  110. begin
  111. params['Version'] = "v2"
  112. params['AccessKeyId'] = ACCESS_KEY_ID
  113. params['SignatureMethod'] = "HMAC-SHA1"
  114. t = Time.now.to_i - 8*3600
  115. params['Timestamp'] = "#{Time.at(t).strftime("%Y-%m-%dT%H:%M:%SZ")}"
  116. params['SignatureVersion'] = "1.0"
  117. params['SignatureNonce'] = "#{Time.now.to_i}#{'%04d'%rand(9999)}"
  118. params['Signature'] = _signature(params, method)
  119. url = self::HOST[Rails.env] + url
  120. if method.upcase == 'GET'
  121. response = RestClient.get url, {:params => params}
  122. else
  123. response = RestClient.post url, params
  124. end
  125. return JSON.parse(response.body)
  126. rescue => e
  127. p e
  128. end
  129. end
  130. def list_index()
  131. uri = "/index"
  132. _api_call(uri, "get",{})
  133. end
  134. def create_index(index_name, template = "xikego_product")
  135. uri = "/index/#{index_name}"
  136. _api_call(uri, "post", {:action => "create", :template => template})
  137. end
  138. def delete_index(index_name)
  139. uri = "/index/#{index_name}"
  140. _api_call(uri, "post",{:action => "delete"})
  141. end
  142. def show_index(index_name)
  143. uri = "/index/#{index_name}"
  144. _api_call(uri, "get",{:action => "status"})
  145. end
  146. #items是个数组
  147. def create_doc(index_name, table_name, fields)
  148. uri = "/index/doc/#{index_name}"
  149. # t = (Time.now.to_i - 8*3600)*1000
  150. # items = [{"cmd" => "add", "timestamp" => t, "fields" => fields}]
  151. items = [{"cmd" => "add", "fields" => fields}]
  152. items = items.to_json
  153. ret = _api_call(uri, "post",{"action" => "push", "table_name" => table_name, "items" => items})
  154. return ret
  155. end
  156. def delete_doc(index_name, table_name, id)
  157. uri = "/index/doc/#{index_name}"
  158. items = [{"cmd" => "delete", "fields" => {"id" => id.to_s}}]
  159. items = items.to_json
  160. _api_call(uri, "post",{"action" => "push", "table_name" => table_name, "items" => items})
  161. end
  162. def update_doc(index_name, table_name, fields)
  163. uri = "/index/doc/#{index_name}"
  164. # t = (Time.now.to_i - 8*3600)*1000
  165. # items = [{"cmd" => "update", "timestamp" => t, "fields" => fields}]
  166. items = [{"cmd" => "update", "fields" => fields}]
  167. items = items.to_json
  168. _api_call(uri, "post",{"action" => "push","table_name" => table_name, "items" => items})
  169. end
  170. def search_doc(index_names, query)
  171. if index_names.class == Array
  172. index_name = index_names.join(";")
  173. else index_names.class == String
  174. index_name = index_names
  175. end
  176. uri = "/search"
  177. _api_call(uri,"get",{:query => query, :index_name => index_name, :summary_field => "title" })
  178. end
  179. end
  180. end
  181. end
  182. if __FILE__ == $0
  183. end