在Heroku上部署 - 上传后图像消失 [英] Deploying on Heroku - Images disappears after upload

查看:213
本文介绍了在Heroku上部署 - 上传后图像消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ruby on Rails上写了一个Heroku应用程序。这是一个电影评论应用程序。



我可以将图像从我的电脑上传到网络应用程序。一切都符合我的期望。



一天后图像消失。我的要求是让图像继续呈现。



我正在从rails使用Paperclip gem。这只发生在部署的版本上,而不是在本地主机上。



解决方案

p> Heroku的默认上传位置是临时存储。这是因为您每次部署时都会收到一名不同的网络工作人员。



您需要使用S3或其他位置来存储您的文件。幸运的是,这是 Heroku上的回形针的文档。



主要的配置不同之处在于。将 gem'aws-sdk'添加到您的Gemfile中,然后在 config / environments / production.rb 中调整配置文件:

  config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV ['S3_BUCKET_NAME'],
:access_key_id => ENV ['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV ['AWS_SECRET_ACCESS_KEY']
}
}


I deployed an application on Heroku which I wrote on Ruby on Rails. It is a movie review app.

I am able to upload images from my computer, to the web app online. Everything else works as per my expectations.

The images disappear after a day. My requirement is to have the image continue to render.

I am using Paperclip gem from rails. This only happens on the deployed version and not on localhost.

解决方案

The default upload location on Heroku is into temporary storage. This is because you will get a different web worker each time you deploy.

You need to use S3 or another location to store your files. Luckily this is well documented for Paperclip on Heroku.

The main configuration difference is this. Add gem 'aws-sdk' to your Gemfile and then adjust your config file in config/environments/production.rb:

config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
    :bucket => ENV['S3_BUCKET_NAME'],
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
  }
}

这篇关于在Heroku上部署 - 上传后图像消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆