Class: Timet::S3Supabase
- Inherits:
-
Object
- Object
- Timet::S3Supabase
- Defined in:
- lib/timet/s3_supabase.rb
Overview
S3Supabase is a class that provides methods to interact with an S3-compatible storage service. It encapsulates common operations such as creating a bucket, listing objects, uploading and downloading files, deleting objects, and deleting a bucket.
This class requires the following environment variables to be set:
-
S3_ENDPOINT: The endpoint URL for the S3-compatible storage service.
-
S3_ACCESS_KEY: The access key ID for authentication.
-
S3_SECRET_KEY: The secret access key for authentication.
-
S3_REGION: The region for the S3-compatible storage service (default: ‘us-west-1’).
Defined Under Namespace
Classes: CustomError
Constant Summary collapse
- ENV_FILE_PATH =
File.join(Dir.home, '.timet', '.env')
- S3_ENDPOINT =
ENV.fetch('S3_ENDPOINT', nil)
- S3_ACCESS_KEY =
ENV.fetch('S3_ACCESS_KEY', nil)
- S3_SECRET_KEY =
ENV.fetch('S3_SECRET_KEY', nil)
- S3_REGION =
ENV.fetch('S3_REGION', 'us-west-1')
- LOG_FILE_PATH =
File.join(Dir.home, '.timet', 's3_supabase.log')
Instance Method Summary collapse
-
#create_bucket(bucket_name) ⇒ Boolean
Creates a new bucket in the S3-compatible storage service.
-
#delete_bucket(bucket_name) ⇒ void
Deletes a bucket and all its contents.
-
#delete_object(bucket_name, object_key) ⇒ void
Deletes an object from the specified bucket.
-
#download_file(bucket_name, object_key, download_path) ⇒ void
Downloads a file from the specified bucket.
-
#initialize ⇒ S3Supabase
constructor
Initializes a new instance of the S3Supabase class.
-
#list_objects(bucket_name) ⇒ Array<Hash>, ...
Lists all objects in the specified bucket.
-
#upload_file(bucket_name, file_path, object_key) ⇒ void
Uploads a file to the specified bucket.
Constructor Details
#initialize ⇒ S3Supabase
Initializes a new instance of the S3Supabase class. Sets up the AWS S3 client with the configured credentials and endpoint.
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/timet/s3_supabase.rb', line 76 def initialize validate_env_vars @logger = Logger.new(LOG_FILE_PATH) @logger.level = Logger::INFO @s3_client = Aws::S3::Client.new( region: S3_REGION, access_key_id: S3_ACCESS_KEY, secret_access_key: S3_SECRET_KEY, endpoint: S3_ENDPOINT, force_path_style: true ) end |
Instance Method Details
#create_bucket(bucket_name) ⇒ Boolean
Creates a new bucket in the S3-compatible storage service.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/timet/s3_supabase.rb', line 95 def create_bucket(bucket_name) begin @s3_client.create_bucket(bucket: bucket_name) @logger.info "Bucket '#{bucket_name}' created successfully!" return true rescue Aws::S3::Errors::BucketAlreadyExists @logger.error "Error: The bucket '#{bucket_name}' already exists." rescue Aws::S3::Errors::BucketAlreadyOwnedByYou @logger.error "Error: The bucket '#{bucket_name}' is already owned by you." rescue Aws::S3::Errors::ServiceError => e @logger.error "Error creating bucket: #{e.}" end false end |
#delete_bucket(bucket_name) ⇒ void
This method returns an undefined value.
Deletes a bucket and all its contents. First deletes all objects in the bucket, then deletes the bucket itself.
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/timet/s3_supabase.rb', line 192 def delete_bucket(bucket_name) list_objects(bucket_name) @s3_client.list_objects_v2(bucket: bucket_name).contents.each do |object| delete_object(bucket_name, object.key) end @s3_client.delete_bucket(bucket: bucket_name) @logger.info "Bucket '#{bucket_name}' deleted successfully." rescue Aws::S3::Errors::ServiceError => e @logger.error "Error deleting bucket: #{e.}" raise e end |
#delete_object(bucket_name, object_key) ⇒ void
This method returns an undefined value.
Deletes an object from the specified bucket.
177 178 179 180 181 182 183 |
# File 'lib/timet/s3_supabase.rb', line 177 def delete_object(bucket_name, object_key) @s3_client.delete_object(bucket: bucket_name, key: object_key) @logger.info "Object '#{object_key}' deleted successfully." rescue Aws::S3::Errors::ServiceError => e @logger.error "Error deleting object: #{e.}" raise e end |
#download_file(bucket_name, object_key, download_path) ⇒ void
This method returns an undefined value.
Downloads a file from the specified bucket.
162 163 164 165 166 167 168 |
# File 'lib/timet/s3_supabase.rb', line 162 def download_file(bucket_name, object_key, download_path) response = @s3_client.get_object(bucket: bucket_name, key: object_key) File.binwrite(download_path, response.body.read) @logger.info "File '#{object_key}' downloaded successfully." rescue Aws::S3::Errors::ServiceError => e @logger.error "Error downloading file: #{e.}" end |
#list_objects(bucket_name) ⇒ Array<Hash>, ...
Lists all objects in the specified bucket.
nil if error occurs
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/timet/s3_supabase.rb', line 120 def list_objects(bucket_name) response = @s3_client.list_objects_v2(bucket: bucket_name) if response.contents.empty? @logger.info "No objects found in '#{bucket_name}'." false else @logger.info "Objects in '#{bucket_name}':" response.contents.each { |object| @logger.info "- #{object.key} (Last modified: #{object.last_modified})" } response.contents.map(&:to_h) end rescue Aws::S3::Errors::ServiceError => e @logger.error "Error listing objects: #{e.}" nil end |
#upload_file(bucket_name, file_path, object_key) ⇒ void
This method returns an undefined value.
Uploads a file to the specified bucket.
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/timet/s3_supabase.rb', line 143 def upload_file(bucket_name, file_path, object_key) @s3_client.put_object( bucket: bucket_name, key: object_key, body: File.open(file_path, 'rb') ) @logger.info "File '#{object_key}' uploaded successfully." rescue Aws::S3::Errors::ServiceError => e @logger.error "Error uploading file: #{e.}" end |