Class: Pindo::AWSS3Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pindo/client/aws3sclient.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAWSS3Client

Returns a new instance of AWSS3Client.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pindo/client/aws3sclient.rb', line 27

def initialize()


    begin

      config_file = File.join(File::expand_path(Pindoconfig.instance.pindo_common_configdir), "pgyer_client_config.json")
      config_json = JSON.parse(File.read(config_file))

      @region = config_json["region"]
      @bucket_name = config_json["bucket_name"]
      @access_key_id = config_json["access_key_id"]
      @access_key_secret = config_json["access_key_secret"]
      @default_url = config_json["default_url"]
      @attach_url = config_json["attach_url"]

    rescue => error
        raise Informative,  "加载pgyer配置文件失败, 需要执行 pindo setup 或者pindo env dreamstudio"
    end 


    http_proxy = ENV['http_proxy'] || ENV['https_proxy']
    
    if http_proxy
      puts "正在使用代理 : #{http_proxy}"
      Aws.config.update(
        http_proxy: http_proxy,
        credentials: Aws::Credentials.new(@access_key_id, @access_key_secret),
        region: @region
      )
    else
      Aws.config.update(
        credentials: Aws::Credentials.new(@access_key_id, @access_key_secret),
        region: @region
      )
    end
  
    @s3_client ||= Aws::S3::Client.new(
            {
              http_proxy: http_proxy,
              region: @region,
              credentials: Aws::Credentials.new(@access_key_id, @access_key_secret)
            }.compact.compact
          )


    # S3Client = Aws::S3::Client.new(
    #     access_key_id: 'ACCESS_KEY_ID',
    #     secret_access_key: 'SECRET_ACCESS_KEY',
    #     region: 'REGION'
    # ) 

end

Instance Attribute Details

#access_key_idObject

Returns the value of attribute access_key_id.



18
19
20
# File 'lib/pindo/client/aws3sclient.rb', line 18

def access_key_id
  @access_key_id
end

#access_key_secretObject

Returns the value of attribute access_key_secret.



19
20
21
# File 'lib/pindo/client/aws3sclient.rb', line 19

def access_key_secret
  @access_key_secret
end

#asw_s3_objectObject

Returns the value of attribute asw_s3_object.



24
25
26
# File 'lib/pindo/client/aws3sclient.rb', line 24

def asw_s3_object
  @asw_s3_object
end

#attach_urlObject

Returns the value of attribute attach_url.



22
23
24
# File 'lib/pindo/client/aws3sclient.rb', line 22

def attach_url
  @attach_url
end

#bucket_nameObject

Returns the value of attribute bucket_name.



16
17
18
# File 'lib/pindo/client/aws3sclient.rb', line 16

def bucket_name
  @bucket_name
end

#default_urlObject

Returns the value of attribute default_url.



21
22
23
# File 'lib/pindo/client/aws3sclient.rb', line 21

def default_url
  @default_url
end

#regionObject

Returns the value of attribute region.



15
16
17
# File 'lib/pindo/client/aws3sclient.rb', line 15

def region
  @region
end

Instance Method Details

#delete_file(file_name) ⇒ Object



87
88
89
90
91
# File 'lib/pindo/client/aws3sclient.rb', line 87

def delete_file(file_name)
  bucket = find_bucket!()
  file = bucket.object(file_name)
  file.delete
end

#find_bucket!Object



80
81
82
83
84
85
# File 'lib/pindo/client/aws3sclient.rb', line 80

def find_bucket!( )
  bucket = Aws::S3::Bucket.new(@bucket_name, client: @s3_client)
  raise "Bucket '#{bucket_name}' not found" unless bucket.exists?

  return bucket
end

#set_progress(index_num, total_num, char = '>') ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/pindo/client/aws3sclient.rb', line 150

def set_progress(index_num, total_num,  char ='>' )
    progress_str = sprintf("%.2f", 100.0 * index_num / total_num )
    total_size = sprintf("%.2f", 1.00 * total_num / 1024 /1024 ) 
    index = 100.0 * index_num / total_num

    # (char * (index/1).floor).ljust(100, ' ')

    upload_message = "已上传:#{progress_str}\%【" + (char * (index/1).floor).ljust(100, '_') +  "】Total: #{total_size} M"
    @upload_message = upload_message
    Funlog.instance.fancyinfo_update(upload_message)
    # @spinner.reset
    # $stdout.print "    已上传: #{progress_str} \% ", (char * (index/1).floor).ljust(100, ' '), " Total: #{total_size} M \r"
    # $stdout.flush
    # print "    已上传: #{progress_str} \% ", (char * (index/1).floor).ljust(100, ' '), " Total: #{total_size} M \r"
    # $stdout.flush
end

#upload_file(binary_file: nil, isAttach: false) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/pindo/client/aws3sclient.rb', line 94

def upload_file(binary_file:nil, isAttach:false)

  extension = File.extname(binary_file)
  filename = File.basename(binary_file)
  filesize = File.size(binary_file)
  size_level = 1024 * 1024
  bytes = File.binread(binary_file)
  checksum = Digest::MD5.hexdigest(bytes)
  file_uuid = SecureRandom.uuid
  total_m = sprintf("%.2f", 1.00 * filesize / 1024 /1024 ) 


  puts "文件路径: #{binary_file}"
  puts "文件大小: #{total_m}M"
  upload_path = @default_url
  content_disposition = nil
  if isAttach
      upload_path = @default_url + @attach_url 
      content_disposition = "attachment; filename=#{filename}"
  end

  object_key = upload_path + file_uuid + extension

  puts "上传路径: #{object_key}"
  puts
  
  upload_result = nil


  Funlog.instance.fancyinfo_start("开始上传...")

  begin

      obj = Aws::S3::Object.new(@bucket_name, object_key)
      progress = Proc.new do |bytes, totals|
        set_progress(bytes.sum, totals.sum)
      end

      if isAttach
        obj.upload_file(binary_file, progress_callback: progress, content_disposition:content_disposition)
      else
        obj.upload_file(binary_file, progress_callback: progress)
      end
      
      upload_result = object_key

      Funlog.instance.fancyinfo_success(@upload_message)
      Funlog.instance.fancyinfo_success("#{binary_file} 上传成功!")
    rescue => ex
      Funlog.instance.fancyinfo_error("#{binary_file} 上传失败!")
    end

  return upload_result
end