Sha256: 7aaea69531fc40db0a4a4ebc394621129be1357dbfb0b5111cb975d4e2df9e1f

Contents?: true

Size: 1.87 KB

Versions: 6

Compression:

Stored size: 1.87 KB

Contents

# encoding: utf-8

require 'java'
require 'logstash-input-google_cloud_storage_jars.rb'

module LogStash
  module Inputs
    module CloudStorage
      # BlobAdapter exposes parts of `com.google.cloud.storage.Blob` for use in
      # the plugin for easier mocking and future-proofing.
      class BlobAdapter
        def initialize(blob)
          @blob = blob
        end

        def name
          @blob.getName()
        end

        def attributes
          {
            'bucket' => @blob.getBucket(),
            'metadata' => @blob.getMetadata(),
            'name' => @blob.getName(),
            'md5' => @blob.getMd5(),
            'crc32c' => @blob.getCrc32c(),
            'generation' => @blob.getGeneration()
          }
        end

        java_import 'com.google.cloud.storage.Blob$BlobSourceOption'
        def delete!
          @blob.delete(BlobSourceOption.generationMatch())
        end

        def set_metadata!(key, value)
          new_metadata = { key => value }
          @blob.toBuilder().setMetadata(new_metadata).build().update()
        end

        def metadata
          @blob.getMetadata()
        end

        def generation
          @blob.getGeneration()
        end

        def line_attributes(line_number)
          attrs = attributes

          attrs['line'] = line_number
          attrs['line_id'] = "#{uri}:#{line_number}@#{generation}"

          attrs
        end

        def uri
          "gs://#{@blob.getBucket()}/#{name}"
        end

        java_import 'java.nio.file.Paths'
        def download_to(path)
          temp_path = Paths.get(path)
          @blob.downloadTo(temp_path)
        end

        def with_downloaded(temp_directory)
          temp_file = ::File.join(temp_directory, SecureRandom.uuid)
          download_to(temp_file)

          yield temp_file

          FileUtils.remove_entry_secure(temp_file, true)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
logstash-input-google_cloud_storage-0.15.0-java lib/logstash/inputs/cloud_storage/blob_adapter.rb
logstash-input-google_cloud_storage-0.14.0-java lib/logstash/inputs/cloud_storage/blob_adapter.rb
logstash-input-google_cloud_storage-0.13.0-java lib/logstash/inputs/cloud_storage/blob_adapter.rb
logstash-input-google_cloud_storage-0.12.0-java lib/logstash/inputs/cloud_storage/blob_adapter.rb
logstash-input-google_cloud_storage-0.11.1-java lib/logstash/inputs/cloud_storage/blob_adapter.rb
logstash-input-google_cloud_storage-0.10.0-java lib/logstash/inputs/cloud_storage/blob_adapter.rb