Class: OmniAI::Google::Bucket

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

Constructor Details

#initialize(client:, io:, bucket_name: nil) ⇒ Bucket

Returns a new instance of Bucket.

Parameters:

  • client (Client)
  • io (File, String)
  • bucket_name (String) (defaults to: nil)

    optional - bucket name



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

Parameters:

  • client (Client)
  • io (IO, String)
  • bucket_name (String) (defaults to: nil)

    optional - bucket name (defaults to project_id-speech-audio)



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).

Returns:

  • (String)

    GCS URI (gs://bucket/object)

Raises:



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
  timestamp = Time.now.strftime("%Y%m%d_%H%M%S")
  random_suffix = SecureRandom.hex(4)
  filename = "audio_#{timestamp}_#{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.message}"
end