# Storing Images in the cloud

**URL:** https://meta.discourse.org/t/storing-images-in-the-cloud/6779
**Category:** Self-hosting
**Tags:** hosting
**Created:** [May 21, 2013, 11:56pm UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779 "2013-05-21T23:56:32Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![colin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/colin/32/103221_2.png) [@colin](https://meta.discourse.org/u/colin)
#### Post date: [May 21, 2013, 11:56pm UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779/1 "2013-05-21T23:56:32Z")

</div>

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.

![](https://global.discourse-cdn.com/meta/original/1X/bff794d31c291ffcb1aa4d3eee62bc1af0b01dc9.png)

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.

> <https://gist.github.com/sud0n1m/6e19440df97882ca0242>

## 2. Uploading to Imgur

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

> <https://gist.github.com/sud0n1m/60263253879ee6c953ab>

## 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.

---

<div class="post-metadata">

### Author: ![Sander78](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sander78/32/296615_2.png) [@Sander78](https://meta.discourse.org/u/Sander78)
#### Post date: [May 22, 2013, 5:40am UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779/2 "2013-05-22T05:40:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![cbonsig](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cbonsig/32/104734_2.png) [@cbonsig](https://meta.discourse.org/u/cbonsig)
#### Post date: [June 3, 2013, 12:42am UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779/3 "2013-06-03T00:42:12Z")

</div>

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](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?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [June 3, 2013, 1:02am UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779/4 "2013-06-03T01:02:22Z")

</div>

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

---

<div class="post-metadata">

### Author: ![benbeltran](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/benbeltran/32/103438_2.png) [@benbeltran](https://meta.discourse.org/u/benbeltran)
#### Post date: [June 3, 2013, 1:09am UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779/5 "2013-06-03T01:09:24Z")

</div>

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](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](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](https://github.com/benbeltran/discourse/commit/8604b3714d558e75754b64c8f29caec2338e86e0) … Sorry)

I hope this post is helpful.

---

<div class="post-metadata">

### Author: ![cbonsig](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cbonsig/32/104734_2.png) [@cbonsig](https://meta.discourse.org/u/cbonsig)
#### Post date: [June 3, 2013, 2:47am UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779/6 "2013-06-03T02:47:13Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![benbeltran](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/benbeltran/32/103438_2.png) [@benbeltran](https://meta.discourse.org/u/benbeltran)
#### Post date: [June 3, 2013, 3:15am UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779/7 "2013-06-03T03:15:18Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![cbonsig](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cbonsig/32/104734_2.png) [@cbonsig](https://meta.discourse.org/u/cbonsig)
#### Post date: [June 3, 2013, 4:26am UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779/8 "2013-06-03T04:26:15Z")

</div>

> [@benbeltran](#):
>
> maybe it’s going through more than one provider (e.g. s3 + imgur) and they’re borking each other

good thought, but I confirmed that only imgur was active

> [@benbeltran](#):
>
> Are you sure you’re doing File.size(file.tempfile)?

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

> [@benbeltran](#):
>
> Test locally w/o entering the rescues

I don’t know what that means ☹ … 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.

> [@benbeltran](#):
>
> make sure you’re uploading to a directory that is symlinked from outside of the releases dir

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!

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [June 3, 2013, 8:30pm UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779/9 "2013-06-03T20:30:33Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [June 4, 2013, 11:00pm UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779/10 "2013-06-04T23:00:01Z")

</div>

Just submitted a [pull request](https://github.com/discourse/discourse/pull/956) that should fix all of the bugs.

Props to @benbeltran for fixing `imgur` uploads 😉

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

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [June 5, 2013, 10:15pm UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779/11 "2013-06-05T22:15:19Z")

</div>

**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.

- [Imgur: The magic of the Internet](https://imgur.com/register/upgrade)
- [http://api.imgur.com/#register](http://api.imgur.com/#register)
- [Imgur: The magic of the Internet](http://imgur.com/help/images#howlong)

> 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?

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [June 10, 2013, 7:48am UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779/12 "2013-06-10T07:48:27Z")

</div>

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

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [June 10, 2013, 7:46pm UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779/13 "2013-06-10T19:46:29Z")

</div>

Ok, added on my TODO list.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [June 11, 2013, 8:48pm UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779/14 "2013-06-11T20:48:43Z")

</div>

Here it is:  
[https://github.com/discourse/discourse/pull/1012](https://github.com/discourse/discourse/pull/1012)

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [April 2, 2015, 6:23am UTC](https://meta.discourse.org/t/storing-images-in-the-cloud/6779/20 "2015-04-02T06:23:18Z")

</div>

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

S3 support is now official: [Using Object Storage for Uploads (S3 & Clones)](https://meta.discourse.org/t/using-object-storage-for-uploads-s3-clones/148916)
