Skip to main content

Refile S3, presigned, multiple files + Cloudfront - generating images via javascript

General • Asked by Dean Fields

Hello,

I have followed along with both videos, but have come up with a little issue while attempting to generate a thumbnail within the success event. It seems to be automatically requiring the token which is not required in these videos.

I have not set a secret_key within my initialiser as the documentation suggests. I have discovered you can generate and check a token via these commands:

https://github.com/refile/refile/blob/master/lib/refile.rb#L296-L316

example url: (/store/fill/400/200/a7d1406d633ce73f124a33598690d146f8fd422dbf66d9f56b6a12eb2d31/image)

I paste these in to the js line for displaying the image:

$(fileupload_id + ' img').attr(
src: "/attachments/[token here]/[url here]"
)

This now works and shows the image. - of course it's not the one being uploaded, but one I took from s3 from a previous upload

is there a way to generate the token via javascript?


I believe the secret_key is a new security feature added recently and you probably just need to follow it's instructions. https://github.com/refile/refile#retrieving-files

This wasn't something that existed when I recorded those videos originally.


Thanks Chris,

I will take a second glance, but I don't think it's explained in the instructions


It looks like when the error gets thrown, they give you a random secret key that you should copy paste into an initializer then restart your app and you should be good.


Ah right!, ok thank you very much


Let me know if that works for you. I'll have to update the video soon.

That's always the hard part of making screencasts for gems like this!


Ok so in my view I outputted refile.secret_key and it seems to be set somehow.

I copied and pasted this in to the initializer just to see if it would make a difference. When I took the token out of the url, no luck. I'm not receiving any errors in my logs or anything when I don't set secret_key so it must being set by the system itself?

I made sure I restarted.

Using <%= attachment_image_tag(photo, :image, :fill, 300, 300) %>

Seems to set the token in the url - but I haven't got a secret_key set?

But no errors seem to be occurring?

I can only imagine, it's almost like you need to make micro videos for each thing so that it can be modularly updated when things change :)


It might be. I didn't see anything requiring you to set it. I just updated the code from the GoRails episode and everything is working fine without the token(and it didn't require me to put the token in the URL).

You're doing this with S3 presigned uploads and that seems to be the piece that's requiring the token?


<%= f.attachment_field :photos_images, multiple: true, direct: true, presigned: true, host: '' %>

This is my attachment field currently. I'm going to attempt to try a couple of things out, i.e removing presigned and attempt to see if I can figure out where the issue is, by looking at the differences between mine and yours.

I've also set my Refile.host to use Cloudfront. I had to set host: '' on the attachment field otherwise the URL when uploading gets messed up:

[cf url].cloudfront.net/attachments/cache/presign 404 (Not Found)

If I remove // on the front of Refile.host

the images for attachment_image_tag work, but do not if // is added (have to overwrite the host again)

I'm not sure what is factoring this or if it's connected


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.