Storing Images in the cloud

My project tonight is to get image hosting working. After a recent upgrade it looks like something got screwed up. When I redeploy, the images get b0rked.

There are 3 option I’m exploring and dang nabbit, I’m going to try to get one working tonight.

1. Uploading to S3.

This is my preferred solution, but it isn’t currently working.

2. Uploading to Imgur

Second best… I got an API key from them. Config seems fine. Similar failure.

3. Uploading to my server

This seems to usually work… but when I deploy, the file disappears. Seems I need to figure out how to config to save it in a different directory so I don’t deploy over it.

Wish me luck! If I figure out any answers I’ll post them here.

1 Like

I had the same error for S3. If you visit the doc that is linked in the log you’ll find out this has nothing to do with Discourse but probably that you use a non-US bucket.

I’m having no luck with S3 or Imgur either. Both seem so tantalizingly close. I’m not competent to fix the code, but at least I can point out the current state of affairs. This applies to 0.9.2

S3 Issues

It seems like the format has changed for AWS resources. I created a bucket, and the only option for region is “US Standard”. On the setup page, I entered the keys, and figured out that the region had to be set to “us-east-1”. With that, I can upload successfully, but the image URL is incorrect, the image appears broken. Discourse creates code like this:

<img src="//BUCKETNAME.s3-us-east-1.amazonaws.com/IMAGEFILENAME.png" width="690" height="690">

I can manually edit the URL to fix the image. It needs to look like this:

<img src="//s3.amazonaws.com/BUCKETNAME/IMAGEFILENAME.png" width="690" height="690">

Imgur Issues

Imgur uploads fail with an error “Sorry, but we couldn’t determine the size of the image. Maybe your image is corrupted?” … @benbeltran seems to have found a fix for this, but I haven’t been able to replicate it. More details here:

https://github.com/discourse/discourse/issues/890

Local image storage

This works fine, but I gather from @colin above that this approach doesn’t survive (re?)deployment. I’m not exactly sure what that means… Is there a problem preserving local images through an upgrade cycle? Are there any issues with starting off using local storage, then switching later when Imgur/S3 get sorted out?

@zogstrip @eviltrout can you have a look at this?

Hey, I responded in the github issue, but I’ll reply here as well. I haven’t had the time to read up / implement the changes to comply with pull request guidelines. But the fix is basically this:

• Use https for the default endpoint instead of http (imgur no longer accepts http requests)
• Rewind the file after reading it. (The library in charge of extracting the size needs to read it afterwards, if not rewound it will fail)

As shown here:
https://github.com/benbeltran/discourse/commit/967d5b59ab2d43ad17cfb50903ea9a142b67e083

Use file.tempfile for File.size when passing the options, as shown here:
https://github.com/benbeltran/discourse/commit/6ab3ef7779e121e11b39ecc8c07c04320dbdf475

( I assume all of you will have File.size(file) instead of what I have… But I kinda messed up in this commit: https://github.com/benbeltran/discourse/commit/8604b3714d558e75754b64c8f29caec2338e86e0 … Sorry)

I hope this post is helpful.

3 Likes

Thanks @benbeltran! The good news: I understood your original explanation well enough to modify my code such that it was identical to the imgur.rb that you used. Also added the https.The bad news: my revision had the same result as your version – same error. Here’s the line that gets written to production.log

Started POST “/uploads” for 75.144.241.86 at 2013-06-03 02:41:39 +0000
Processing by UploadsController#create as JSON
Parameters: {“topic_id”=>“1234”, “file”=>#<ActionDispatch::Http::UploadedFile:0x007f5b51a675a8 @content_type=“image/png”, @headers=“Content-Disposition: form-data; name=“file”; filename=“filename.png”\r\nContent-Type: image/png\r\n”, @original_filename=“filename.png”, @tempfile=#<File:/tmp/RackMultipart20130603-25805-1b7gg7l>>}
Rendered text template (0.0ms)
Completed 422 Unprocessable Entity in 404ms (Views: 0.6ms | ActiveRecord: 5.1ms)

So maybe there’s something else that is contributing. If I can figure it out, I will comment again here.

Hmm. I don’t get any errors. Those fixes actually fixed it for me. Is it possible that the 422 may be caused by inclusion of some other plugin? … Unprocessable entity means it didn’t pass validation, so maybe it’s going through more than one provider (e.g. s3 + imgur) and they’re borking each other? I’m only using imgur fwiw.

EDIT: Ahhh wait wait. Are you sure you’re doing File.size(file.tempfile)? 422 is probably caused by a FastImage Exception, you’re not seeing it because it rescues it in the controller. Test locally w/o entering the rescues to see what exception it’s bringing.

EDIT II: @cbonsig about the local file disappearing. I don’t know what you’re using to deploy, but make sure you’re uploading to a directory that is symlinked from outside of the releases dir. For example: link project/shared/uploads => project/current/uploads … If you’re not doing something like that, the next deploy will surely eat your uploads.

good thought, but I confirmed that only imgur was active

yep. i trashed my imgur.rb and replaced it with yours. and triple checked it!

I don’t know what that means :frowning: … I tried commenting out the rescue code, and there didn’t seem to be any difference. I kinda have the feeling that Imgur is just completely hosed in my installation at this point. So I’m not going to worry about it for now.

Got it. I moved the whole /htdocs/public/ folder outside to a different spot, and created a symbolic link. That seems to be working fine.

Thanks again for getting back to me!

We want S3 and Imgur to work, the problem is we don’t regularly use these features so they don’t get tested strongly.

@zogstrip will update here soon to ensure everything is working.

1 Like

Just submitted a pull request that should fix all of the bugs.

Props to @benbeltran for fixing imgur uploads :wink:

Please, try it out once merged into master and do not hesitate to provide feedbacks.

1 Like

We are thinking of pulling official Imgur support, since you’d need a Pro account and entirely different API mechanisms to guarantee your images are still there in a year or two years.

As long as images are getting at least 1 view per 6 months, they will stick around forever. If an image does not receive even 1 view in 6 months, it may be removed to make room for new images. Images on pro accounts will never be removed.

We would rather offer just S3 image hosting support for reliability, and let Imgur support come as a plugin at a later date. The current Imgur code we will leave in the vendor plugins folder as a todo if anyone wants to take it.

Any objections to this?

3 Likes

I do not see any objections so @zogstrip go ahead and pull Imgur support out for the reasons outlined above.

Ok, added on my TODO list.

Here it is:

2 Likes

This topic is now closed. New replies are no longer allowed.

S3 support is now official: Using Object Storage for Uploads (S3 & Clones)

2 Likes