Couldn't find file which is generated by code

Hi

Jets gem really helped me to build AWS Lambda.
It is really helpful for me.

My code writes some data to file and upload to SFTP server.
It seems it is working well since I can see files upload on the SFTP server.
But I can’t find the generated file. I checked in S3 bucket but couldn’t find.
For example, the filename is log/20190927.txt

Could you please help me where those files are located?
Thank you.

How is the ftp server and s3 connected/related in your setup? :thinking:

Are you using the managed ftp service from AWS where it’s backed by an s3 bucket?

Thanks for your reply, @tongueroo

I first generated file:

filename = "log/#{Time.now.strftime("%Y%m%d")}.txt"
open(filename, 'a') do |f|
  f.puts "name"
end

I am using net-sftp gem to upload this generated file to specific SFTP server like this.

Net::SFTP.start(ENV['HOST_1'], ENV['USERNAME_1'], { 
				:password => ENV['PASSWORD_1'], 
				:port => ENV['PORT_1'],
				:verbose => :debug,
}) do |sftp|
     sftp.upload!(filename, "upload/1.log")
end

I can check if files were uploaded on the sftp server so it seems it is working properly.
I am going to find where the generated file is in my project. But I couldn’t find.

Unsure if am following the question.

Looks like the first code snippet generates a filename and writes it and so it should be there…

"log/#{Time.now.strftime("%Y%m%d")}.txt"

Worked fine here:

test.rb:

require "fileutils"
filename = "log/#{Time.now.strftime("%Y%m%d")}.txt"
FileUtils.mkdir_p(File.dirname(filename))
open(filename, 'a') do |f|
  f.puts "name"
end

Bash terminal output:

$ ruby a.rb 
$ cat log/20190927.txt 
name
$ 

FYI, if you end up having to generate files on lambda, you will have to write the files to /tmp. The project directory on lambda is a read-only filesystem.

Thank you.
I will write files in the /tmp directory.

My question is that if I write the file in the /tmp directory, where can I find these generated files?

:thinking: It would be in /tmp . Try ls /tmp to see the files. If this is on lambda already, shell out.

Example. FYI, you need to adjust it with your current handler to make it work.

def sh(command)
  puts "=> #{command}"
  puts `#{command}`
end

def handler(event:, context:)
  sh "ls /tmp"
end

Thank you for your help.
Are there any other ways to check those files? (by downloading and check)

Oh I think I see what you’re getting at now. Maybe you’re trying to verify that the files have the same contents?

You have to upload it back to s3 or then download it from there, etc.

Or you might try an md5sum. Unsure if the command is available on the Lambda environment. If it is. Example:

$ md5sum a.txt 
d41d8cd98f00b204e9800998ecf8427e  a.txt
$

Then you can md5sum the file that was generate and also the one on the ftp server and use that as a way to compare the contents are the same.

Thank you.

Here, which file can I download?

You can download any of those files. They are in the s3 bucket associated with the parent CloudFormation stack that Jets created.

However, you won’t find the generated file you’re creating. That generated file is created as part Lambda running the function. It’ll be in /tmp because that’s where you’re writing it to. It’s not a part of the package code that you are listing in the screenshot.

I see.
I understand all.
Thank you for your help.
I really appreciate it.