Uploading a file from remote url using shrine?
Hi,
I need to upload a remote file to my S3 bucket (e.g., I get a link to a pdf e-ticket and I need to upload it from its location). In addition (as a bonus) what's the best way to allow users to download these pdf's from my s3 bucket? is there a way to bundle few pdfs into one download (e.g., allow a user to download multiple stored e-tickets with one button click)
I looked into shrine and it's plugin ecosystem but I wasn't able to tell which one to use...
Any idea how to accomplish this? shrine is not mandatory, but would be nice. I saw that paperclip has something with remote url but I prefer Shrine as I want to use it for other tasks as well.
Thanks,
Roy
Hey Roy!
Do you already have Shrine initialized and have an attacher created?
If you could show us how your model is set up, and whether or not you're doing this in a form or a service object, that would be helpful.
Essentially what you want to do is open and set the remote file to a variable name, something like "tempfile," and then call your model and set the attribute to the value of the tempfile. Then save the instance of the model.
So if you have a column called pdf_data, and that's where you want to store the file, and that colum belongs to a ticket table, then you would do:
tempfile = File.open('url_to_pdf')
@ticket.pdf = tempfile
@ticket.save
I would recommend opening PDF files and anything large to a background job though.
Hey Jacob,
Thanks for your reply and sorry for the delayed response, I somehow missed your answer.
I don't have the code yet, was just wondering what would be the "right" way to do it.
From all the readings that I did, nothing seems to give a simple solution to "copy" from random URL (e.g., I am getting a link to a downloadable PDF) to S3.
The best thing I found is to use the probably use the Down
gem which is included to stream the remote file into a temp one (as you suggested, just replace the File.open
as it's not opening remote file) and then attach it.
I was pretty sure that there's something much simpler to copy a file from some URL to S3.
I guess there is another solution, I'll post it here if I find one :-)