Class: OmniAI::Google::Bucket
- Inherits:
-
Object
- Object
- OmniAI::Google::Bucket
- Defined in:
- lib/omniai/google/bucket.rb
Overview
Uploads audio files to Google Cloud Storage for transcription.
Defined Under Namespace
Classes: UploadError
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(client:, io:, bucket_name: nil) ⇒ Bucket
constructor
A new instance of Bucket.
-
#process! ⇒ String
GCS URI (gs://bucket/object).
Constructor Details
#initialize(client:, io:, bucket_name: nil) ⇒ Bucket
Returns a new instance of Bucket.
21 22 23 24 25 |
# File 'lib/omniai/google/bucket.rb', line 21 def initialize(client:, io:, bucket_name: nil) @client = client @io = io @bucket_name = bucket_name || default_bucket_name end |
Class Method Details
.process!(client:, io:, bucket_name: nil) ⇒ Object
14 15 16 |
# File 'lib/omniai/google/bucket.rb', line 14 def self.process!(client:, io:, bucket_name: nil) new(client:, io:, bucket_name:).process! end |
Instance Method Details
#process! ⇒ String
Returns GCS URI (gs://bucket/object).
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/omniai/google/bucket.rb', line 30 def process! # Create storage client with same credentials as main client credentials = @client.instance_variable_get(:@credentials) storage = ::Google::Cloud::Storage.new( project_id:, credentials: ) # Get bucket (don't auto-create if it doesn't exist) bucket = storage.bucket(@bucket_name) unless bucket raise UploadError, "Bucket '#{@bucket_name}' not found. " \ "Please create it manually or ensure the service account has access." end # Generate unique filename = Time.now.strftime("%Y%m%d_%H%M%S") random_suffix = SecureRandom.hex(4) filename = "audio_#{}_#{random_suffix}.#{file_extension}" # Upload file - create StringIO for binary content content = file_content if content.is_a?(String) && content.include?("\0") # Binary content - wrap in StringIO require "stringio" content = StringIO.new(content) end bucket.create_file(content, filename) # Return GCS URI "gs://#{@bucket_name}/#{filename}" rescue ::Google::Cloud::Error => e raise UploadError, "Failed to upload to GCS: #{e.}" end |