Reputation: 39
I have the logic to create a CSV and serve it as a download to the user, but what I am wanting to do is automatically create the CSV once a week during off hours and upload it to S3 and serve it from there. The hope is to drastically decrease the download time for the user since the CSV takes a couple minutes to generate.
The only step I'm missing is how I can go about saving this CSV to the host. What I plan on doing is saving it to the host, upload it to S3, and then deleting it from the host.
Any ideas how I can go about doing this? Thank you.
Upvotes: 0
Views: 2210
Reputation: 2134
How about a simple bash script on your server:
#!/bin/bash
#
# Save this file to /home/user/upload.sh
curl -o /tmp/csv_file.csv http://localhost/path/to/csv
aws s3 mv /tmp/csv_file.csv s3://bucket/
And then schedule the script via cron
# /etc/crontab
# Run every day at 08.15
15 08 * * * user /home/user/upload.sh
Note: http://localhost/path/to/csv
would be the same url your button links to.
Upvotes: 1
Reputation: 1608
You can create and save CSV in your host server like this.
CSV.open(Rails.root.join('tmp', 'csv_cache', "#{csv_name}"), "wb") do |csv|
# your logic
end
This will create a CSV in the tmp/csv_cache
folder with the name specified in csv_name.
Then you can upload this into the s3 like this
file_url = Utils.new().upload_to_s3(csv_name, Rails.root.join('tmp', 'csv_cache', csv_name))
class Utils
def upload_to_s3(file_name, source)
obj = AMAZON_S3_CLIENT.bucket(S3_BUCKET).object('csv_cache/'+file_name)
obj.upload_file source, {acl: 'public-read'}
return obj.public_url
end
end
Then you can delete the file from the server.
Upvotes: 4
Reputation: 1333
Try using sidekiq or cron that will run your CSV-task(job) weekly. Within this task, CSV is saved to the host using 'CSV.open' and after that upload to S3.
You might need a table to store the S3 path of the file. Or I guess you can just overwrite the old file and reuse the S3 path.
Upvotes: 0