aws_s3_export 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
19
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aws_s3_export.gemspec
4
+ gemspec
5
+ gem "aws-sdk"
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Alex Dmitriev
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ # AwsS3Export
2
+ Simple gem for export a directory to AWS S3. You set directory, bucket and keys and enjoy it.
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'aws_s3_export'
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install aws_s3_export
17
+
18
+ ## Usage
19
+
20
+ creat file export.rb and pust code below
21
+
22
+ #simple script for usage
23
+ require 'rubygems'
24
+ require 'aws_s3_export'
25
+
26
+ s3 = AwsS3Export::Export.new(:export_dir => 'a/directory/on/ours/computer', :bucket_name =>'your-bucket-name',
27
+ :prefix => 'a/prefix/in/your/bucket', :access_key => "XXX",
28
+ :secret_access_key => "XXX" )
29
+
30
+ s3.run
31
+
32
+ Run from console: $ ruby export.rb
33
+ And you should will see:
34
+
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.push File.expand_path("../lib", __FILE__)
3
+
4
+ require 'aws_s3_export/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.authors = ["Alex Dmitriev"]
8
+ gem.email = ["sunchess@inbox.ru"]
9
+ gem.description = %q{Simple gem for export a directory to AWS S3. You set directory, bucket and keys and enjoy it.}
10
+ gem.summary = %q{Simple gem for export directory to AWS S3}
11
+ gem.homepage = ""
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.name = "aws_s3_export"
17
+ gem.require_paths = ["lib"]
18
+ gem.version = AwsS3Export::VERSION
19
+ gem.add_dependency("aws-sdk", ">= 1.5.2")
20
+ end
@@ -0,0 +1,19 @@
1
+ $LOAD_PATH.push File.expand_path("../../lib", __FILE__)
2
+
3
+ require "rubygems"
4
+ require 'aws-sdk'
5
+ require "aws_s3_export/version"
6
+ require "aws_s3_export/config"
7
+ require "aws_s3_export/error"
8
+ require "aws_s3_export/export"
9
+
10
+ module AwsS3Export
11
+ def self.extended base
12
+ begin
13
+ require 'aws-sdk'
14
+ rescue LoadError => e
15
+ e.message << " (You may need to install the aws-sdk gem)"
16
+ raise e
17
+ end unless defined?(AWS::Core)
18
+ end
19
+ end
@@ -0,0 +1,40 @@
1
+ module AwsS3Export
2
+ #
3
+ # Set config for export:
4
+ # s3_config = AwsS3Export::Config.new(:export_dir => '/your/dir/', :bucket_name =>'your_bucker_name', :prefix => "dir", :access_key => xxx, :secret_access_key => xxx )
5
+ #
6
+ # :export_dir - directory of files or directories for export
7
+ # :prefix - prefix for saving to s3
8
+ # :bucket_name - name of bucket s3
9
+ # :access_key - access key id of s3
10
+ # :secret_access_key - secret access key of s3
11
+ #
12
+ # Get config for example the export_dir:
13
+ # s3_config.export_dir #=> /your/dir/
14
+ #
15
+
16
+ class Config
17
+ attr_reader :access_key, :secret_access_key, :export_dir, :bucket_name, :prefix
18
+
19
+ def initialize(options = {})
20
+ validation_options.each do |o|
21
+ raise NoParams.new if options[o].empty?
22
+ end
23
+
24
+ raise NotDir.new unless File.stat(options[:export_dir]).directory?
25
+
26
+ @access_key = options[:access_key]
27
+ @secret_access_key = options[:secret_access_key]
28
+ @export_dir = options[:export_dir] || "./" # the current directory
29
+ @bucket_name = options[:bucket_name]
30
+ @prefix = options[:prefix] || ""
31
+ end
32
+
33
+ private
34
+ def validation_options
35
+ [ :access_key, :secret_access_key, :bucket_name ]
36
+ end
37
+
38
+ end
39
+ end
40
+
@@ -0,0 +1,30 @@
1
+ module AwsS3Export
2
+ # Example:
3
+ # def do_some
4
+ # if no_params?
5
+ # raise NoParams
6
+ # end
7
+ # end
8
+ #
9
+ # If you need to set custom errors you do this
10
+ # class CustomError < Error
11
+ # def initialize
12
+ # super("Your text gois here")
13
+ # end
14
+ # end
15
+ #
16
+ #
17
+ class Error < StandardError; end
18
+
19
+ class NoParams < Error
20
+ def initialize
21
+ super("You should set all params like this s3 = AwsS3Export::Export.new(:export_dir => '/your/dir/', :bucket_name =>'your_bucker_name', :prefix => 'dir', :access_key => xxx, :secret_access_key => xxx ) ")
22
+ end
23
+ end
24
+
25
+ class NotDir < Error
26
+ def initialize
27
+ super(":export_dir mast be a directory")
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,79 @@
1
+ module AwsS3Export
2
+
3
+ class Export
4
+ def initialize(options = {})
5
+ @config = Config.new(options)
6
+ end
7
+
8
+ def run
9
+ set_config
10
+
11
+ # Create the basic S3 object
12
+ @s3 = AWS::S3.new
13
+
14
+ # Load up the 'bucket' we want to store things in
15
+ @bucket = @s3.buckets[@config.bucket_name]
16
+
17
+ # If the bucket doesn't exist, create it
18
+ unless @bucket.exists?
19
+ puts "Need to make bucket #{@config.bucket_name}.."
20
+ @s3.buckets.create(@config.bucket_name)
21
+ end
22
+
23
+ Dir.entries(@config.export_dir).each do |dir_or_file|
24
+ puts "Work in #{dir_or_file}"
25
+ save_file_or_dir(dir_or_file)
26
+ end
27
+
28
+ end
29
+
30
+ private
31
+ def save_file(file)
32
+ if !@config.prefix.empty?
33
+ prefix = "#{@config.prefix}/"
34
+ else
35
+ prefix = ""
36
+ end
37
+ # Grab a reference to an object in the bucket with the name we require
38
+ object = @bucket.objects["#{prefix}#{file}"]
39
+
40
+ # Write a local file to the aforementioned object on S3
41
+ if object.write(:file => File.expand_path( file, @config.export_dir ))
42
+ puts "File '#{file}' has saved"
43
+ else
44
+ puts "Somthing wrong! File '#{file}' not save"
45
+ end
46
+ end
47
+
48
+ def save_file_or_dir(name, path = "")
49
+ return if name[0,1] == '.'
50
+ file_name_with_path = path + name
51
+ save_file(file_name_with_path) if file?(file_name_with_path)
52
+ # See if we need to recurse...
53
+ if directory?(file_name_with_path)
54
+ my_base = file_name_with_path + '/'
55
+ Dir.foreach(File.expand_path(my_base, @config.export_dir)) { |e| save_file_or_dir(e, my_base) }
56
+ end
57
+ end
58
+
59
+ def file?(file)
60
+ fstat = File.stat(File.expand_path(file, @config.export_dir))
61
+ fstat.file?
62
+ end
63
+
64
+ def directory?(dir)
65
+ fstat = File.stat(File.expand_path(dir, @config.export_dir))
66
+ fstat.directory?
67
+ end
68
+
69
+ def set_config
70
+ AWS.config(
71
+ :access_key_id => @config.access_key,
72
+ :secret_access_key => @config.secret_access_key
73
+ )
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+
@@ -0,0 +1,3 @@
1
+ module AwsS3Export
2
+ VERSION = "0.1.8"
3
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws_s3_export
3
+ version: !ruby/object:Gem::Version
4
+ hash: 11
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 8
10
+ version: 0.1.8
11
+ platform: ruby
12
+ authors:
13
+ - Alex Dmitriev
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-06-01 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: aws-sdk
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 1
31
+ - 5
32
+ - 2
33
+ version: 1.5.2
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description: Simple gem for export a directory to AWS S3. You set directory, bucket and keys and enjoy it.
37
+ email:
38
+ - sunchess@inbox.ru
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - Gemfile
48
+ - LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - aws_s3_export.gemspec
52
+ - lib/aws_s3_export.rb
53
+ - lib/aws_s3_export/config.rb
54
+ - lib/aws_s3_export/error.rb
55
+ - lib/aws_s3_export/export.rb
56
+ - lib/aws_s3_export/version.rb
57
+ homepage: ""
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options: []
62
+
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.24
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Simple gem for export directory to AWS S3
90
+ test_files: []
91
+