Class: Outhad::Integrations::Source::AmazonS3::Client

Inherits:
UnstructuredSourceConnector
  • Object
show all
Defined in:
lib/outhad/integrations/source/amazon_s3/client.rb

Instance Method Summary collapse

Instance Method Details

#check_connection(connection_config) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/outhad/integrations/source/amazon_s3/client.rb', line 9

def check_connection(connection_config)
  connection_config = connection_config.with_indifferent_access
  @session_name = "connection-#{connection_config[:region]}-#{connection_config[:bucket]}"

  if unstructured_data?(connection_config)
    create_s3_connection(connection_config)
    @s3_resource.bucket(connection_config[:bucket]).objects.limit(1).first
  else
    conn = create_connection(connection_config)
    path = build_path(connection_config)
    get_results(conn, "DESCRIBE SELECT * FROM '#{path}';")
  end
  ConnectionStatus.new(status: ConnectionStatusType["succeeded"]).to_outhad_message
rescue StandardError => e
  ConnectionStatus.new(status: ConnectionStatusType["failed"], message: e.message).to_outhad_message
end

#discover(connection_config) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/outhad/integrations/source/amazon_s3/client.rb', line 26

def discover(connection_config)
  connection_config = connection_config.with_indifferent_access
  @session_name = "discover-#{connection_config[:region]}-#{connection_config[:bucket]}"

  streams = if unstructured_data?(connection_config)
              [create_unstructured_stream]
            else
              conn = create_connection(connection_config)
              # If pulling from multiple files, all files must have the same schema
              path = build_path(connection_config)
              records = get_results(conn, "DESCRIBE SELECT * FROM '#{path}';")
              columns = build_discover_columns(records)
              [Outhad::Integrations::Protocol::Stream.new(name: path, action: StreamAction["fetch"], json_schema: convert_to_json_schema(columns))]
            end
  catalog = Catalog.new(streams: streams)
  catalog.to_outhad_message
rescue StandardError => e
  handle_exception(e, { context: "AMAZONS3:DISCOVER:EXCEPTION", type: "error" })
end

#read(sync_config) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/outhad/integrations/source/amazon_s3/client.rb', line 46

def read(sync_config)
  connection_config = sync_config.source.connection_specification.with_indifferent_access
  @session_name = "#{sync_config.sync_id}-#{sync_config.source.name}-#{sync_config.destination.name}"

  return handle_unstructured_data(sync_config) if unstructured_data?(connection_config)

  conn = create_connection(connection_config)
  query = sync_config.model.query
  query = batched_query(query, sync_config.limit, sync_config.offset) unless sync_config.limit.nil? && sync_config.offset.nil?
  query(conn, query)
rescue StandardError => e
  handle_exception(e, {
                     context: "AMAZONS3:READ:EXCEPTION",
                     type: "error",
                     sync_id: sync_config.sync_id,
                     sync_run_id: sync_config.sync_run_id
                   })
end