Last active
August 29, 2015 13:58
-
-
Save ehlyzov/10362334 to your computer and use it in GitHub Desktop.
Migrate local assets to S3 #paperclip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| related_path = ->(poster) { '/system/data/' + q.poster.send(:hashed_path) } | |
| remote_path = -> (p) { '/srv/tulp-application/shared' + related_path[p] } | |
| local_path = ->(p) {'/srv/tulp-application/shared/public' + related_path[p] } | |
| %x[mkdir -p #{local_path[q]} && scp -r tulp@backend02.tulp.ru:#{remote_path[a]} #{local_path[q]}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # BusinessImagesAux( id status ) | |
| ActiveRecord::Base.connection.execute(<<SQL) | |
| drop table assets_aux; | |
| SQL | |
| ActiveRecord::Base.connection.execute(<<SQL) | |
| create table assets_aux ( | |
| id serial, | |
| asset_id integer, | |
| status character varying(255), | |
| asset_klass character varying(255), | |
| asset_style character varying(255), | |
| asset_path character varying(512), | |
| asset_date timestamp without time zone, | |
| content_type character varying(255), | |
| target_path character varying(255) | |
| ); | |
| UPDATE assets_aux SET id = nextval(pg_get_serial_sequence('assets_aux','id')); | |
| ALTER TABLE assets_aux ADD PRIMARY KEY (id); | |
| create unique index on assets_aux (asset_path); | |
| SQL | |
| class AssetsAux < ActiveRecord::Base; | |
| self.table_name = 'assets_aux' | |
| end | |
| AssetsAux.reset_column_information | |
| SECRET = "MY_SECRET" | |
| Asset.offset(3377).find_each do |image| | |
| (image.data.styles.keys << 'original').each do |key| | |
| AssetsAux.create do |row| | |
| row.asset_id = image.id | |
| row.status = 'unprocessed' | |
| row.asset_klass = image.class.to_s.tableize | |
| row.asset_style = key.to_s | |
| row.asset_path = image.data.path(key) | |
| row.asset_date = image.created_at.to_s(:db) | |
| row.content_type = image.data_content_type | |
| # interpolate = ":class/:attachment/:id/:style/:updated_at" | |
| interpolate = "#{row.asset_klass}/data/#{image.id}/#{key}/#{image.data_updated_at.to_i}" | |
| extension = image.data.send(:interpolate, ":extension") | |
| hash_key = OpenSSL::HMAC.hexdigest('SHA1', SECRET, interpolate) | |
| row.target_path = "#{row.asset_klass}/#{hash_key}.#{extension}" | |
| end | |
| end | |
| end | |
| require 'aws/s3' | |
| AWS.config({ | |
| access_key_id: 'MY_ACCESS_KEY', | |
| secret_access_key: 'MY_SECRET_ACCESS_KEY' | |
| }) | |
| bucket = AWS::S3.new.buckets['MY_BUCKET'] | |
| # https://github.com/aws/aws-sdk-ruby/issues/241 | |
| Net::HTTP::IDEMPOTENT_METHODS_.delete("PUT") | |
| errs = [] | |
| AssetsAux.where(status: 'unprocessed').find_each do |asset| | |
| begin | |
| bucket.objects[asset.target_path].write( | |
| Pathname.new( asset.asset_path ), | |
| { | |
| acl: :public_read, | |
| content_type: asset.content_type | |
| }) | |
| asset.update_attribute(:status, 'uploaded') | |
| asset.save | |
| rescue => e | |
| errs << e.message | |
| asset.update_attribute(:status, 'error') | |
| asset.save | |
| end | |
| end | |
| puts errs.size |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment