Rails Paperclip Phonegap Heroku S3- How to return image files from an Ajax request through the controller? - ruby-on-rails-3

I am creating a mobile app through Phonegap as the client and using Rails as the back-end. I am deploying my app to Heroku and am planning to use S3 to store the image files, because that is what is recommended from my various readings online.
I was wondering how could the Rails controller be used to send images back from Ajax requests from Phonegap.
I am not sure how to write the back-end API code to send images to requests.
I also read that using the send_file method without x-send_file enabled will slow down the server because sending the image would block other request until it is done.
Please let me know if you have any insights.

You could use redirects to the S3 assets here, then your browser is just getting the image directly, and not holding up one of your server processes while the browser slowly downloads the images.
If you need to keep your images private you can use the signed URL feature of S3 to only give signed and time limited URL's to the appropriate users. (See my commit to Paperclip: https://github.com/thoughtbot/paperclip/pull/292)

Related

Most efficient way to send image to frontend

I have images stored in Google Cloud Storage. Whenever my frontend (Swift) requests a certain image I would like to send the image as quickly and efficiently as possible from my backend.
Conveniently, Google Cloud Storage has direct image links for every image.
Is it most efficient to send a multipart/form-data the same way I send an image captured by a user in the front end to the backend? Or is it more efficient to send the URL of the image stored in the cloud where the frontend can proceed to download the image from that URL?
This can indeed be done through a signed URL which provides limited permission and time to make a request. With signed URLs authentication information is contained in their query string, allowing users without credentials to perform specific actions on a resource.
I would like to point out however that Signed URLs can only be used to access resources in Cloud Storage through XML API endpoints.
Since you are using Swift for your frontend I would also like to direct you to explore Google APIs for iOS, such as CocoaPods.

How to download images/files by AWS S3 secured URLs on mobile app OR WEB?

Right Now,
The S3 URLs are private.(To avoid direct access from any un-authorised user)So we are not able to access S3 urls directly. To overcome this issue we are sending base64 string of attachment in API response.On web there is no such difficulties for me to handle this scene. But for mobile APP i am facing issue. So following are the some questions related to it.
1.Is it good idea to use base64 string on mobile APP? (Because payload will go in MB`s)
2. I also come to know about pre-signed urls. Are they safe? Because here we are providing a temporary url(Last for some time).
For react native mobile apps to download files we need http/https file url. Is there any other way to handle this issue?
Is there any other way to tackle this situation.

Imageresizer with c# Web API

I was checking Imageresizer S3 Reader2 plugin, and I have the following question.
My app is basically a c# REST API that has a functionality of serving
photos (resized photos).
Would it be possible to use Imageresizer+Amazon S3 with REST API so I can resize
photos in with Imageresizer in c# before serving it and without transferring original photo over network?
You'll have to transfer the original photo from S3 to your server (at least once) in order to resize it. The S3Reader2 plugin does this automatically. If you want to prevent repeat requests, look into SourceDiskCache.
Otherwise, that's exactly how ImageResizer+S3Reader2 functions.

How to implement maintenance mode for Heroku API

I want to use the maintenance mode on Heroku.
But for an API, the maintenance mode does not seem to work. My API calls are stopped in their tracks because there is no header 'Access-Control-Allow-Origin' present. So there is no way to know if the API is in maintenance modeā€”or if it's just not responding.
Is there a way to use the Heroku maintenance mode when the app is an API?
Currently, unfortunately not. When you set a custom maintenance page it gets embedded as an iframe within a Heroku-served HTML page. As you've identified, the CORS headers are not set so your API client won't even fetch the page.
See Is there a way to have a Heroku error/maintenance url served directly without the iframe

Amazon S3: how to use/load an mp3 file simultaneously

I'm using Amazon S3 to store some mp3 files.
My web application, uses the Soundmanager2 javascript library to load the files from the Amazon bucket, and play them to users.
When the first user clicks on an mp3, soundmanager starts playing the file, and as intended, caches the rest of the song as it is being played.
Problem is, if a second user clicks on the same mp3, he must wait until the first user caches the whole song, which is unacceptable for my website.
I understand that Amazon S3 somehow 'streams' the file exclusively to the first request. Is there a way to be able to use that file simultaneously, i.e. users be able to play the same mp3's at the same time?
Also, would the CloudFront functionality solve this issue?
Thank you for your help!
Alex
(By the way, my application is built on Ruby on Rails 3, and hosted on Heroku)
There is no limitation in S3 that restricts simultaneous downloads of a single object.
I would suggest that you use a tool, like Charles, to inspect the HTTP requests and see if another service is causing the second client's request to be delayed.