s3_asset_sync 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e8791d286affb173eb4d5e3940e2d31104a134fd
4
+ data.tar.gz: 047cc71a9f62d00bdfdffc978b2369a1d35a7331
5
+ SHA512:
6
+ metadata.gz: fad6979d7fd385cc2114471ece7719e935e95a69b1cdc7e9010bfb72fcc11dfbd26523c0dccaa1fa7bade61d3eee086964223d48929e274f44db23e45cf00628
7
+ data.tar.gz: 0b71d72e6c543ae1d37c3b426d3259bc5b3b86797a65e78cfb34dffca3b36229b82fd083ce9de3193768296b1a9e2ccb6ba7f6908aca0156e37b2a41078987d7
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Neil Turner
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,35 @@
1
+ = S3 Asset Sync
2
+
3
+ This gem provides a simple way of syncronizing your Rails 4 assets with an AWS S3 Bucket.
4
+
5
+ == Installation
6
+
7
+ gem 's3_asset_sync'
8
+
9
+ == Configuration
10
+
11
+ Run the following command to generate a configuration file in your Rails /config/initializers folder.
12
+
13
+ rake s3_asset_sync:setup
14
+
15
+ Edit that file and replace the default values with your own AWS details.
16
+
17
+ == Usage
18
+
19
+ By default, assets will be syncronized automatically after precompilation:
20
+
21
+ rake assets:precompile
22
+
23
+ If this is not the desired behaviour then simply edit the configuration value as below:
24
+
25
+ Rails.application.config.s3_asset_sync.run_after_precompile = false
26
+
27
+ To manually syncronize assets run:
28
+
29
+ rake assets:sync_to_s3
30
+
31
+ == Deleting Assets
32
+
33
+ Expired assets won't be automatically removed from S3 as some nodes within a load balanced setup may temporarily require older assets. To delete expired assets from S3 run:
34
+
35
+ rake assets:purge_s3
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'S3AssetSync'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
@@ -0,0 +1,104 @@
1
+ require 'colorize'
2
+ require 'aws-sdk-core'
3
+ require 's3_asset_sync/railtie' if defined?(Rails)
4
+
5
+ module S3AssetSync
6
+
7
+ ##
8
+ # Loops through /public/assets directory and sync's each
9
+ # file with the specified S3 Bucket.
10
+ #
11
+ def self.sync
12
+ puts "Syncing assets to S3...".yellow
13
+
14
+ Aws.config.update({
15
+ credentials: Aws::Credentials.new(
16
+ Rails.application.config.s3_asset_sync.s3_access_key,
17
+ Rails.application.config.s3_asset_sync.s3_secret_access_key
18
+ ),
19
+ region: Rails.application.config.s3_asset_sync.s3_region
20
+ })
21
+
22
+ s3 = Aws::S3::Client.new
23
+
24
+ Dir.foreach(Rails.root.join('public','assets')) do |file|
25
+ next if file == '.' || file == '..'
26
+ puts "SYNC: #{file}"
27
+ self.s3_upload_object(s3, file) unless self.s3_object_exists?(s3, file)
28
+ end
29
+
30
+ puts "Asset sync successfully completed...".green
31
+ end
32
+
33
+ ##
34
+ # Loops through specified S3 Bucket and checks to see if the object
35
+ # exists in our /public/assets folder. Deletes it from the
36
+ # bucket if it doesn't exist.
37
+ #
38
+ def self.purge
39
+ puts "Cleaning assets in S3...".yellow
40
+
41
+ Aws.config.update({
42
+ credentials: Aws::Credentials.new(
43
+ Rails.application.config.s3_asset_sync.s3_access_key,
44
+ Rails.application.config.s3_asset_sync.s3_secret_access_key
45
+ ),
46
+ region: Rails.application.config.s3_asset_sync.s3_region
47
+ })
48
+
49
+ s3 = Aws::S3::Client.new
50
+
51
+ keys = []
52
+
53
+ s3.list_objects(bucket:Rails.application.config.s3_asset_sync.s3_bucket).each do |response|
54
+ keys += response.contents.map(&:key)
55
+ end
56
+
57
+ keys.each do |key|
58
+ if !File.exists?(Rails.root.join('public', 'assets', key))
59
+ self.s3_delete_object(s3, key)
60
+ puts "DELETED: #{key}"
61
+ end
62
+ end
63
+
64
+ puts "Asset clean successfully completed...".green
65
+ end
66
+
67
+ ##
68
+ # Check if a key exists in the specified S3 Bucket.
69
+ #
70
+ def self.s3_object_exists?(client, key)
71
+ begin
72
+ client.head_object(
73
+ bucket: Rails.application.config.s3_asset_sync.s3_bucket,
74
+ key: key
75
+ )
76
+ return true
77
+ rescue
78
+ return false
79
+ end
80
+ end
81
+
82
+ ##
83
+ # Uploads an object to the specified S3 Bucket.
84
+ #
85
+ def self.s3_upload_object(client, key)
86
+ resp = client.put_object(
87
+ acl: "public-read",
88
+ bucket: Rails.application.config.s3_asset_sync.s3_bucket,
89
+ body: File.open(Rails.root.join('public','assets', key)),
90
+ key: key
91
+ )
92
+ end
93
+
94
+ ##
95
+ # Deletes an object from the specified S3 Bucket.
96
+ #
97
+ def self.s3_delete_object(client, key)
98
+ resp = client.delete_object(
99
+ bucket: Rails.application.config.s3_asset_sync.s3_bucket,
100
+ key: key
101
+ )
102
+ end
103
+
104
+ end
@@ -0,0 +1,25 @@
1
+ module S3AssetSync
2
+ class Railtie < Rails::Railtie
3
+ config.s3_asset_sync = ActiveSupport::OrderedOptions.new
4
+
5
+ config.s3_asset_sync.run_after_precompile = true
6
+ config.s3_asset_sync.s3_bucket = "YOUR_BUCKET_NAME"
7
+ config.s3_asset_sync.endpoint = "YOUR_END_POINT"
8
+ config.s3_asset_sync.s3_access_key = "YOUR_ACCESS_KEY"
9
+ config.s3_asset_sync.s3_secret_access_key = "YOUR_SECRET_ACCESS_KEY"
10
+
11
+ rake_tasks do
12
+ load "tasks/s3_asset_sync.rake"
13
+ namespace :s3_asset_sync do
14
+ desc "Initialize S3AssetSync configuration."
15
+ task :setup do
16
+ abort("Already found config file. Have you already run this task?.") if File.exist?("config/initializers/s3_asset_sync.rb")
17
+
18
+ cp(
19
+ File.expand_path("../../../templates/s3_asset_sync.rb", __FILE__), "config/initializers/s3_asset_sync.rb"
20
+ )
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module S3AssetSync
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ namespace :assets do
2
+
3
+ desc 'Synchronize all compiled assets to Amazon S3'
4
+ task :sync_to_s3 => :environment do
5
+ S3AssetSync.sync
6
+ end
7
+
8
+ desc 'Remove any expired assets stored on Amazon S3'
9
+ task :purge_s3 => :environment do
10
+ S3AssetSync.purge
11
+ end
12
+
13
+ end
14
+
15
+ if Rake::Task.task_defined?("assets:precompile")
16
+ Rake::Task["assets:precompile"].enhance do
17
+ Rake::Task["assets:sync_to_s3"].invoke if defined?(Rails) && Rails.application.config.s3_asset_sync.run_after_precompile
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3_asset_sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Neil Turner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: colorize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2'
55
+ description: Simple way to syncronise your Rails 4 assets with an AWS S3 Bucket.
56
+ email:
57
+ - neil@neilturner.me
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.rdoc
64
+ - Rakefile
65
+ - lib/s3_asset_sync.rb
66
+ - lib/s3_asset_sync/railtie.rb
67
+ - lib/s3_asset_sync/version.rb
68
+ - lib/tasks/s3_asset_sync.rake
69
+ homepage: https://github.com/neilturner77/s3_asset_sync
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.2.2
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Simple way to syncronise your Rails 4 assets with an AWS S3 Bucket.
93
+ test_files: []