Skip to content

Adding possibility to include file name as a part of prefix #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/logstash/outputs/s3/temporary_file_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ class TemporaryFileFactory
TXT_EXTENSION = "txt"
STRFTIME = "%Y-%m-%dT%H.%M"

attr_accessor :counter, :tags, :prefix, :encoding, :temporary_directory, :current
attr_accessor :counter, :tags, :prefix, :encoding, :temporary_directory, :current, :fname

def initialize(prefix, tags, encoding, temporary_directory)
@counter = 0
@prefix = prefix
lsl = prefix.rindex('/')
if lsl.nil? or lsl == prefix.size - 1 then
@prefix = prefix
@fname = nil
else
@prefix = prefix.slice(0, lsl + 1)
@fname = prefix.slice(lsl + 1, prefix.size)
end

@tags = tags
@encoding = encoding
Expand Down Expand Up @@ -75,7 +82,7 @@ def generate_name

def new_file
uuid = SecureRandom.uuid
name = generate_name
name = fname.nil? ? generate_name : fname
path = ::File.join(temporary_directory, uuid)
key = ::File.join(prefix, name)

Expand Down
12 changes: 12 additions & 0 deletions spec/outputs/s3/temporary_file_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@
expect(subject.current.path).to include(uuid)
end

context "with prefix contains /" do
subject { described_class.new('prefix/filename.ext', tags, encoding, temporary_directory) }
it "is initialized with filename" do
expect(subject.prefix).to eq('prefix/')
expect(subject.fname).to eq('filename.ext')
end
it "should return file name as current filename" do
file = subject.current
expect(file.path).to end_with "prefix/filename.ext"
end
end

context "with tags supplied" do
let(:tags) { ["secret", "service"] }

Expand Down