inova_aws_s3 0.1.0

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
+ SHA256:
3
+ metadata.gz: ae5e45edf2719c8ba5427ce45288ce661dfc558175f51afdfb9d6ab4f6a8896d
4
+ data.tar.gz: d02eafde3bba0be789778945d208021c4c03f9a3d03134450e873068004ae29d
5
+ SHA512:
6
+ metadata.gz: '029466e5f2dbe14a8f735a8bb0b313f8d775e5e292f24dbd4b700a62988fbb6543c91a498a6d40973c5deee869c6bce9e26897da0a395e02b0bd2a4125768057'
7
+ data.tar.gz: 2bdc1416cf19a7414ea5cb6b0c5efdc59d0eed0998a75bb091b7915e4761bc9cb4e11f49d524a56912306a1173b64e958891cb0e8d93e2da057b9674010e8c0f
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0] - 2023-11-23
2
+
3
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ahmed.mosad@inovaeg.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Ahmed Mos`ad
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # InovaAwsS3
2
+ InovaAwsS3 is a Ruby gem designed to simplify interactions with Amazon S3, providing seamless integration for uploading, managing, and retrieving files from your S3 buckets. Whether you're building a web application, handling backups, or managing assets, InovaAwsS3 offers a clean and efficient interface to harness the power of Amazon S3 within your Ruby projects.
3
+ ## Installation
4
+
5
+ To use this gem in your project, add it to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'inova_aws_s3', '~> 0.1.0'
9
+ ```
10
+
11
+ Alternatively, you can install the gem directly using:
12
+
13
+ ```bash
14
+ gem install inova_aws_s3 -v '~> 0.1.0'
15
+ ```
16
+ ## Requirments
17
+
18
+ - Ruby >= 3.0.0
19
+ - Rails >= 6.1.3
20
+
21
+ ## Usage
22
+
23
+ ```bash
24
+ rails generate inova_aws_s3:install
25
+ ```
26
+
27
+ This will create a configuration file at config/initializers/inova_aws_s3.rb. Open the file and set your AWS credentials:
28
+
29
+ ```ruby
30
+ InovaAwsS3.configure do |config|
31
+ config.acl = 'public-read'
32
+ config.bucket_name = 'your_bucket_name'
33
+ config.region = 'your_region'
34
+ config.access_key = 'your_access_key'
35
+ config.secret_key = 'your_secret_key'
36
+ config.folder_name = 'your_folder_name'
37
+ end
38
+ ```
39
+
40
+ Replace the placeholder values with your actual AWS credentials.
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ahmedmos3ad/inova_aws_s3/pulls. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/ahmedmos3ad/inova_aws_s3/blob/master/CODE_OF_CONDUCT.md).
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
49
+
50
+ ## Code of Conduct
51
+
52
+ Everyone interacting in the InovaAwsS3 project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ahmedmos3ad/inova_aws_s3/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "standard/rake"
13
+
14
+ task default: %i[test standard]
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/inova_aws_s3/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "inova_aws_s3"
7
+ spec.version = InovaAwsS3::VERSION
8
+ spec.authors = ["Ahmed Mos`ad"]
9
+ spec.email = ["ahmed.mosad@inovaeg.com"]
10
+
11
+ spec.summary = "Ruby Gem for uploading files to AWS S3."
12
+ spec.description = "This gem is used to upload files to AWS S3."
13
+ spec.homepage = "https://inovaeg.com"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.0.0"
16
+ spec.add_dependency 'rails', '>= 6.0'
17
+ spec.add_dependency 'aws-sdk-s3', '~> 1'
18
+ spec.files = Dir['lib/**/*', 'config/**/*', 'lib/generators/**/*']
19
+
20
+ # spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://github.com/ahmedmos3ad/inova_aws_s3"
24
+ spec.metadata["changelog_uri"] = "https://github.com/ahmedmos3ad/inova_aws_s3/blob/master/CHANGELOG.md"
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(__dir__) do
29
+ `git ls-files -z`.split("\x0").reject do |f|
30
+ (File.expand_path(f) == __FILE__) ||
31
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
32
+ end
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ # Uncomment to register a new dependency of your gem
39
+ # spec.add_dependency "example-gem", "~> 1.0"
40
+
41
+ # For more information and examples about making a new gem, check out our
42
+ # guide at: https://bundler.io/guides/creating_gem.html
43
+ end
@@ -0,0 +1,118 @@
1
+ module Aws
2
+ class S3Service
3
+ require 'aws-sdk-s3'
4
+ PART_SIZE = 1024 * 1024 * 10
5
+
6
+ def initialize(acl: InovaAwsS3.configuration.acl)
7
+ @acl = acl
8
+ @bucket_name = InovaAwsS3.configuration.bucket_name
9
+ @region = InovaAwsS3.configuration.region
10
+ @access_key = InovaAwsS3.configuration.access_key
11
+ @secret_key = InovaAwsS3.configuration.secret_key
12
+ @folder_name = InovaAwsS3.configuration.folder_name
13
+ set_s3_client
14
+ set_s3_resource
15
+ end
16
+
17
+ def set_s3_client
18
+ @s3_client = Aws::S3::Client.new(region: @region, access_key_id: @access_key, secret_access_key: @secret_key)
19
+ end
20
+
21
+ def set_s3_resource
22
+ @s3_resource = Aws::S3::Resource.new(region: @region, access_key_id: @access_key, secret_access_key: @secret_key)
23
+ end
24
+
25
+ def multipart_upload(filename: nil, file: nil, acl: "public-read")
26
+ logger ||= Logger.new("#{Rails.root}/log/uploads.log")
27
+ key = @folder_name + filename
28
+ resp = @s3_client.create_multipart_upload({
29
+ bucket: @bucket_name,
30
+ key: key, acl: acl })
31
+ current_part = 1
32
+ parts = []
33
+ file = File.open(file, 'rb')
34
+ index = 1
35
+ until file.eof?
36
+ logger.info("#{key} Part #{current_part}")
37
+ part = file.read(PART_SIZE)
38
+ resp_upload = @s3_client.upload_part({
39
+ body: part,
40
+ bucket: @bucket_name,
41
+ key: key,
42
+ part_number: current_part,
43
+ upload_id: resp.upload_id,
44
+ })
45
+
46
+ parts << { etag: resp_upload.etag, part_number: current_part }
47
+ current_part = current_part + 1
48
+ end
49
+ resp_complete = @s3_client.complete_multipart_upload({
50
+ bucket: @bucket_name, # required
51
+ key: key, # required
52
+ multipart_upload: {
53
+ parts: parts
54
+ },
55
+ upload_id: resp.upload_id # required
56
+ })
57
+ file.close()
58
+ set_object(key).public_url
59
+ end
60
+
61
+ def onepart_upload(file_name: nil, tempfile: nil)
62
+ file_name = "#{SecureRandom.urlsafe_base64}_#{Time.now.strftime('%s')}_#{File.extname(file_name)}".gsub(/\s+/, "")
63
+ key = @folder_name + file_name
64
+ obj = set_object(key)
65
+ obj.upload_file(tempfile, { acl: @acl })
66
+ obj.public_url
67
+ end
68
+
69
+ def set_object(key)
70
+ @s3_resource.bucket(@bucket_name).object(key)
71
+ end
72
+
73
+ def change_acl(url: nil, new_acl: 'authenticated-read')
74
+ return if url.nil?
75
+ key = get_key_from_url(url)
76
+ @s3_client.put_object_acl({ acl: new_acl, bucket: @bucket_name, key: key })
77
+ rescue Exception
78
+ puts "error in #{url}"
79
+ end
80
+
81
+ def get_key_from_url(url)
82
+ # uri = URI.parse(url)
83
+ # File.basename(uri.path)
84
+ arr = url.split("/")
85
+ arr[3..-1]&.join("/")
86
+ end
87
+
88
+ def valid_s3_url?(url)
89
+ arr = url.split("/")
90
+ host_name = "#{@bucket_name}.s3.#{@region}.amazonaws.com"
91
+ return arr[2] == host_name
92
+ end
93
+
94
+ def get_key(filename)
95
+ key = @folder_name + filename
96
+ return key
97
+ end
98
+
99
+ def generate_get_presigned(url)
100
+ key = get_key_from_url(url)
101
+ object = @s3_resource.bucket(@bucket_name).object(key)
102
+ presigned_url = object.presigned_url(:get, expires_in: 10800) # 3 hours
103
+ presigned_url
104
+ end
105
+
106
+ def get_presigned_url(file_name)
107
+ object = @s3_resource.bucket(@bucket_name).object(@folder_name.to_s + file_name)
108
+ presigned_url = object.presigned_url(:put, { acl: "public-read" }) #, expires: 10*60)
109
+ { presigned_url: presigned_url, file_url: object.public_url }
110
+ end
111
+
112
+ def delete_file(url)
113
+ key = get_key_from_url(url)
114
+ return if key.blank?
115
+ @s3_client.delete_object({ bucket: @bucket_name, key: key })
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,9 @@
1
+ YourGemName.configure do |config|
2
+ # Set your configuration options here
3
+ config.acl = 'public-read'
4
+ config.bucket_name = 'your_bucket_name'
5
+ config.region = 'your_region'
6
+ config.access_key = 'your_access_key'
7
+ config.secret_key = 'your_secret_key'
8
+ config.folder_name = 'your_folder_name'
9
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/generators'
2
+
3
+ module InovaAwsS3
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('templates', __dir__)
7
+
8
+ def create_initializer_file
9
+ copy_file 'inova_aws_s3_initializer.rb', 'config/initializers/inova_aws_s3.rb'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module InovaAwsS3
2
+ class Configuration
3
+ attr_accessor :acl, :bucket_name, :region, :access_key, :secret_key, :folder_name
4
+
5
+ def initialize
6
+ # Set default values or raise an error if required values are not set
7
+ @acl = 'public-read'
8
+ @bucket_name = nil
9
+ @region = nil
10
+ @access_key = nil
11
+ @secret_key = nil
12
+ @folder_name = nil
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ # test_config.rb
2
+
3
+ module InovaAwsS3
4
+
5
+ class Configuration
6
+ require 'dotenv'
7
+ Dotenv.load('.env')
8
+ attr_accessor :acl, :bucket_name, :region, :access_key, :secret_key, :folder_name
9
+
10
+ def initialize
11
+ # Set test-specific configuration values here
12
+ @acl= ENV['TEST_ACL']
13
+ @bucket_name= ENV['TEST_BUCKET']
14
+ @region= ENV['TEST_REGION']
15
+ @access_key= ENV['TEST_ACCESS_KEY']
16
+ @secret_key= ENV['TEST_SECRET_KEY']
17
+ @folder_name= ENV['TEST_FOLDER']
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module InovaAwsS3
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "inova_aws_s3/version"
4
+
5
+ module InovaAwsS3
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+
9
+ class << self
10
+ attr_accessor :configuration
11
+ end
12
+
13
+ def self.configure
14
+ self.configuration ||= Configuration.new
15
+ yield(configuration)
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ module InovaAwsS3
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inova_aws_s3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ahmed Mos`ad
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-11-23 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: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-s3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ description: This gem is used to upload files to AWS S3.
42
+ email:
43
+ - ahmed.mosad@inovaeg.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".standard.yml"
49
+ - CHANGELOG.md
50
+ - CODE_OF_CONDUCT.md
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - inova_aws_s3.gemspec
55
+ - lib/aws/s3_service.rb
56
+ - lib/generators/inova_aws_s3/install/templates/inova_aws_s3_initializer.rb
57
+ - lib/generators/inova_aws_s3/install_generator.rb
58
+ - lib/inova_aws_s3.rb
59
+ - lib/inova_aws_s3/configuration.rb
60
+ - lib/inova_aws_s3/test_config.rb
61
+ - lib/inova_aws_s3/version.rb
62
+ - sig/inova_aws_s3.rbs
63
+ homepage: https://inovaeg.com
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ homepage_uri: https://inovaeg.com
68
+ source_code_uri: https://github.com/ahmedmos3ad/inova_aws_s3
69
+ changelog_uri: https://github.com/ahmedmos3ad/inova_aws_s3/blob/master/CHANGELOG.md
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 3.0.0
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.2.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Ruby Gem for uploading files to AWS S3.
89
+ test_files: []