Ask A Question

Notifications

You’re not receiving notifications from this thread.

Uploading a file from remote url using shrine?

Roy Zinn asked in Gems / Libraries

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

Reply

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.

Reply

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 :-)

Reply

OK,
The way to go is using shrine with the remote_url plugin which does exactly what I was looking for
If anyone cares to see the solution in code, i'll be happy to post it here.

Cheers

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,464+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.