Class: Timet::S3Supabase

Inherits:
Object
  • Object
show all
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’).

Examples:

Basic usage

s3_supabase = S3Supabase.new
s3_supabase.create_bucket('my-bucket')
s3_supabase.upload_file('my-bucket', '/path/to/local/file.txt', 'file.txt')

Advanced operations

s3_supabase.list_objects('my-bucket')
s3_supabase.download_file('my-bucket', 'file.txt', '/path/to/download/file.txt')
s3_supabase.delete_object('my-bucket', 'file.txt')
s3_supabase.delete_bucket('my-bucket')

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

Constructor Details

#initializeS3Supabase

Initializes a new instance of the S3Supabase class. Sets up the AWS S3 client with the configured credentials and endpoint.

Raises:

  • (CustomError)

    If required environment variables are missing



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.

Examples:

create_bucket('my-new-bucket')

Parameters:

  • bucket_name (String)

    The name of the bucket to create

Returns:

  • (Boolean)

    true if bucket was created successfully, false otherwise



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.message}"
  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.

Examples:

delete_bucket('bucket-to-delete')

Parameters:

  • bucket_name (String)

    The name of the bucket to delete



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.message}"
  raise e
end

#delete_object(bucket_name, object_key) ⇒ void

This method returns an undefined value.

Deletes an object from the specified bucket.

Examples:

delete_object('my-bucket', 'file-to-delete.txt')

Parameters:

  • bucket_name (String)

    The name of the bucket containing the object

  • object_key (String)

    The key of the object to delete



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.message}"
  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.

Examples:

download_file('my-bucket', 'remote-file.txt', '/path/to/local/file.txt')

Parameters:

  • bucket_name (String)

    The name of the bucket to download from

  • object_key (String)

    The key of the object to download

  • download_path (String)

    The local path where the file should be saved



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.message}"
end

#list_objects(bucket_name) ⇒ Array<Hash>, ...

Lists all objects in the specified bucket.

nil if error occurs

Examples:

list_objects('my-bucket') #=> [{key: 'example.txt', last_modified: '2023-01-01', ...}, ...]
list_objects('empty-bucket') #=> false
list_objects('invalid-bucket') #=> nil

Parameters:

  • bucket_name (String)

    The name of the bucket to list objects from

Returns:

  • (Array<Hash>, false, nil)

    Array of object hashes if objects found, false if bucket is empty,

Raises:

  • (Aws::S3::Errors::ServiceError)

    if there’s an error accessing the S3 service



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.message}"
  nil
end

#upload_file(bucket_name, file_path, object_key) ⇒ void

This method returns an undefined value.

Uploads a file to the specified bucket.

Examples:

upload_file('my-bucket', '/path/to/local/file.txt', 'remote-file.txt')

Parameters:

  • bucket_name (String)

    The name of the bucket to upload to

  • file_path (String)

    The local path of the file to upload

  • object_key (String)

    The key (name) to give the object in the 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.message}"
end