@@ -3,14 +3,20 @@ module EnvelopeDumps
33 class Base # rubocop:todo Metrics/ClassLength
44 attr_reader :envelope_download , :last_dumped_at
55
6- delegate :envelope_community , to : :envelope_download
6+ delegate :envelope_community , :url , to : :envelope_download
77
88 def initialize ( envelope_download , last_dumped_at )
99 @envelope_download = envelope_download
1010 @last_dumped_at = last_dumped_at
1111 end
1212
1313 def bucket
14+ @bucket ||= Aws ::S3 ::Resource
15+ . new ( region : ENV . fetch ( 'AWS_REGION' ) )
16+ . bucket ( bucket_name )
17+ end
18+
19+ def bucket_name
1420 raise NotImplementedError
1521 end
1622
@@ -40,15 +46,12 @@ def dirname
4046 end
4147
4248 def download_file # rubocop:todo Metrics/AbcSize
43- return unless envelope_download . url?
44-
45- log ( "Downloading the existing dump from #{ envelope_download . url } " )
49+ return unless url . present?
4650
47- File . open ( filename , 'wb' ) do |file |
48- URI . parse ( envelope_download . url ) . open do |data |
49- file . write ( data . read )
50- end
51- end
51+ log ( "Downloading the existing dump from #{ url } " )
52+ previous_filename = url . split ( '/' ) . last
53+ object = bucket . object ( previous_filename )
54+ object . get ( response_target : filename )
5255
5356 log ( "Unarchiving the downloaded dump into #{ dirname } " )
5457 system ( "unzip -qq #{ filename } -d #{ dirname } " , exception : true )
@@ -83,10 +86,6 @@ def published_envelopes
8386 end
8487 end
8588
86- def region
87- ENV . fetch ( 'AWS_REGION' )
88- end
89-
9089 def remove_entries
9190 log ( 'Removing recently deleted envelopes from the dump' )
9291
@@ -116,7 +115,7 @@ def run
116115 end
117116
118117 def up_to_date?
119- envelope_download . url? && published_envelopes . none? && destroy_envelope_events . none?
118+ url . present ? && published_envelopes . none? && destroy_envelope_events . none?
120119 end
121120
122121 def upload_file
@@ -129,7 +128,7 @@ def upload_file
129128
130129 log ( 'Uploading the updated dump to S3.' )
131130
132- object = Aws :: S3 :: Resource . new ( region : ) . bucket ( bucket ) . object ( filename )
131+ object = bucket . object ( filename )
133132 object . upload_file ( filename )
134133 object . public_url
135134 end
0 commit comments