Class: EasyS3
- Inherits:
-
Object
- Object
- EasyS3
- Defined in:
- lib/easy-s3.rb,
lib/easy-s3/version.rb
Defined Under Namespace
Classes: DirectoryDoesNotExist, FileNotFound, MissingOptions
Constant Summary collapse
- VERSION =
'0.1.0'
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#fog ⇒ Object
readonly
Returns the value of attribute fog.
Instance Method Summary collapse
-
#create_file(file_path, public = false) ⇒ Object
Create private or public file into directory.
-
#delete_file(url) ⇒ Object
Delete file by url.
-
#files ⇒ Object
Get all files into directory.
-
#initialize(dir_name) ⇒ EasyS3
constructor
A new instance of EasyS3.
Constructor Details
#initialize(dir_name) ⇒ EasyS3
Returns a new instance of EasyS3.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/easy-s3.rb', line 11 def initialize(dir_name) begin #Fog.credentials = { aws_access_key_id: 'XXX', aws_secret_access_key: 'XXXX' } @fog = Fog::Storage.new(provider: 'AWS') @dir = @fog.directories.get(dir_name) rescue ArgumentError raise MissingOptions, 'aws_access_key_id or aws_secret_access_key' rescue Excon::Errors::MovedPermanently, 'Expected(200) <=> Actual(301 Moved Permanently)' raise DirectoryDoesNotExist, dir_name end raise DirectoryDoesNotExist, dir_name if @dir.nil? true end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
9 10 11 |
# File 'lib/easy-s3.rb', line 9 def dir @dir end |
#fog ⇒ Object (readonly)
Returns the value of attribute fog.
9 10 11 |
# File 'lib/easy-s3.rb', line 9 def fog @fog end |
Instance Method Details
#create_file(file_path, public = false) ⇒ Object
Create private or public file into directory
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/easy-s3.rb', line 27 def create_file(file_path, public = false) begin file = File.open(file_path) rescue raise(FileNotFound, file_path) end filename = "#{File.basename(file_path)}_#{Digest::SHA1.hexdigest(File.basename(file_path))}" file = @dir.files.create( key: filename, body: file, public: public ) file.url((Time.now + 3600).to_i) # 1 hour end |
#delete_file(url) ⇒ Object
Delete file by url
45 46 47 48 49 |
# File 'lib/easy-s3.rb', line 45 def delete_file(url) file_name = get_filename_by_url(url) return false unless @dir.files.head(file_name) @dir.files.get(file_name).destroy end |
#files ⇒ Object
Get all files into directory
52 53 54 |
# File 'lib/easy-s3.rb', line 52 def files @dir.files.all end |