简体   繁体   English

从AWS S3存储桶检索图像

[英]Retrieving images from AWS S3 bucket

I'm building a fairly basic Ruby on Rails app, I'll be using about 2000 images, and this is my first real dive into aws/s3. 我正在构建一个相当基本的Ruby on Rails应用程序,我将使用大约2000张图像,这是我第一次真正接触aws / s3。 The app won't have any user interaction, so I'm not sure if it's better to have all of the images on the app, and then upload them to my bucket, or add them to my bucket manually, and then download them to the app from there. 该应用程序不会与用户进行任何交互,因此我不确定将所有图像都存储在该应用程序中,然后将其上传到我的存储桶中,或者手动将它们添加到我的存储桶中,然后再将它们下载到从那里的应用程序。 The AWS documentation is a bit all over the place. AWS文档到处都是。

I currently have carrierwave installed and not sure what the next steps should be, or how to retrieve images from S3 into rails. 我目前已经安装了carrierwave,不确定下一步应该怎么做,或者不确定如何将图像从S3检索到导轨中。 I'll be using Heroku as well, but I've already set up the config with my AWS credentials. 我也将使用Heroku,但是我已经使用我的AWS凭证设置了配置。

uploaders/photo_uploader.rb uploaders / photo_uploader.rb

class PhotoUploader < CarrierWave::Uploader::Base

storage :fog

def store_dir
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

def content_type_whitelist
  /image\//
end
end

initializers/carrierwave.rb 初始值设定项/carrierwave.rb

CarrierWave.configure do |config|
 config.fog_provider = 'fog/aws'
 config.fog_credentials = {
  provider: "AWS",
  aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"],
  aws_secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"]
 }
 config.fog_directory  = ENV["S3_BUCKET"]
end

You need to upload these all images using application, after install carrierwave and fog-aws then you need to create model controller and form for uploading images. 您需要使用应用程序上传所有这些图像,在安装了carrierwavefog-aws之后,您需要创建model controller和用于上传图像的form

OK, currently you have confused how to show image after uploaded, Right? 好的,目前您对上传后如何显示图片感到困惑,对吗?

The simple is if image uploaded properly then the imagine the table is images and model is Image and column is picture because you have did not provided those names. 简单的是,如果图像上传正确,那么可以想象表是images ,模型是Image ,列是picture因为您没有提供这些名称。

images_controller images_controller

class ImagesController < ApplicationController
  def index
    @images = Image.all
  end
end

view/images/index.html.erb view / images / index.html.erb

<% @images.each do |image| %>
   <%= image_tag image.picture.url %>
<% end %>

Note 注意

This not to promote a product 这不是推广产品

If you need to see a sample with source code then this is the BitBucket repository and this is the live Heroku app and Stripe test card number a CVC code must be provided type anything like 232 etc. 如果您需要查看带有源代码的示例,那么这是BitBucket存储库 ,这是实时的Heroku应用Stripe测试卡号,必须提供CVC代码,键入232等。

First step is to integrate image uploading and you can utilize a number of libraries to make this happen. 第一步是集成图像上传,您可以利用许多库来实现这一目标。

You want to grab dotenv-rails gem so you can securely manage the credentials you will need from AWS S3. 您想要获取dotenv-rails gem,以便可以安全地管理AWS S3所需的凭证。 This is a dedicated resource for production ready RoR app. 这是用于生产就绪的RoR应用程序的专用资源。

The next gem you want is the carrierwave-aws and the carrierwave gem that will manage everything and so that's three gems thus far. 您需要的下一个宝石是可管理所有内容的载carrierwave-aws和载carrierwave宝石,因此到目前为止是三颗宝石。 Fourth and final gem is mini_magick which is a requirement in order to use the methods available by carrierwave . 第四个也是最后一个宝石是mini_magick ,这是使用mini_magick提供的方法所carrierwave

Second step is to sign up to an AWS account to use the S3 bucket. 第二步是注册一个AWS账户以使用S3存储桶。 You cannot have the images on the app because if you do, you will not be able to deploy to Heroku with the images. 您无法在应用程序上拥有图像,因为如果这样做,您将无法使用图像将其部署到Heroku。 Heroku will get rid of them. Heroku将摆脱它们。

Once you've installed these gems, you run a bundle install and then build out the basic functionality. 一旦安装了这些gem,就可以运行bundle install ,然后构建基本功能。

Here is some documentation on carrierwave : https://github.com/carrierwaveuploader/carrierwave 这是关于carrierwave一些文档: https : carrierwave

The documentation in the above link will walk you through how to properly install carrierwave . 上面链接中的文档将carrierwave您逐步了解如何正确安装carrierwave

So you will do something like: 因此,您将执行以下操作:

rails generate uploader Photo

In your photo_uploader.rb file, you want to uncomment this code: 在您的photo_uploader.rb文件中,您要取消注释以下代码:

def extension_whitelist
   %w(jpg jpeg gif png)
end

You want this uncommented to serve as a validator of the type of files you can upload. 您希望这个未注释的内容用作您可以上载的文件类型的验证器。 So if its not a jpg jpeg gif png RoR will throw an error. 因此,如果不是jpg jpeg gif png RoR,则会引发错误。 This whitelist is handy so I strongly recommend it. 此白名单非常方便,因此我强烈建议您使用。

Next, you have to set up your mapping between your uploader and the database. 接下来,您必须在上传器和数据库之间设置映射。

So, fast forwarding to the part where you need to connect AWS to your app. 因此,快速转发到您需要将AWS连接到应用程序的部分。 This is where your dotenv-rails gem comes in. By the way, all these gems can be found in rubygems.org. 这就是您的dotenv-rails宝石所在的dotenv-rails 。顺便说一下,所有这些宝石都可以在rubygems.org中找到。

In the root of your folder, you are going to create a file called .env . 在文件夹的根目录中,将创建一个名为.env的文件。

In the .env file you are going to add these: .env文件中,您将添加以下内容:

S3_BUCKET_NAME=
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=

Never push the AWS keys to any codebase versioning tool like Github. 切勿将AWS密钥推入任何代码库版本控制工具(如Github)。

You want to go into your .gitignore file and ensure the .env file is included. 您想进入.gitignore文件并确保包含.env文件。 This way git will not track that file. 这样git将不会跟踪该文件。

To get your AWS credentials, go to your name in the AWS console and click on it and you will see a dropdown with 要获取您的AWS凭证,请在AWS控制台中转到您的名称,然后单击它,您将看到一个下拉列表,其中包含

my security credentials 我的安全凭证

as an option. 作为一种选择。

Next, create your bucket. 接下来,创建您的存储桶。

To test successful integrating with your RoR app, go to rails console and run this command: 要测试与RoR应用程序的成功集成,请转到rails console并运行以下命令:

ENV.fetch('S3_BUCKET_NAME')

If you get an error at this stage, you may need to go to config/application.rb and add: 如果在此阶段出现错误,则可能需要转到config/application.rb并添加:

require "dotenv-rails"

Once having done that, go back into rails c and run ENV.fetch('S3_BUCKET_NAME') again and you should be goood to go if you followed the steps correctly. 完成此操作后,回到rails c并再次运行ENV.fetch('S3_BUCKET_NAME') ,如果您正确地遵循了这些步骤,就应该走了。

You should have an initializers folder and in there you are going to create carrierwave.rb file. 您应该有一个initializers文件夹,然后在其中创建carrierwave.rb文件。

Inside of that file you are going to paste all the code thats under the Usage section of this documentation: 在该文件的内部,您将粘贴本文档“ 用法”部分下的所有代码:

https://github.com/sorentwo/carrierwave-aws https://github.com/sorentwo/carrierwave-aws

Go back to your photo_uploader.rb file and change storage :file to storage :aws . 返回您的photo_uploader.rb文件,并将storage :file更改为storage :aws

Home stretch here, go back to carrierwave.rb file and there is one line of code you need to completely remove from what you copy and pasted from the above link and it is this line here: 在此处进行首页拉伸,返回至carrierwave.rb文件,您需要从上面的链接中复制并粘贴的代码中完全删除一行代码,此处为以下一行:

config.asset_host = "http://example.com/

Now you can start up your rails server and instead of pointing to your local file system it should now be pointing to your bucket. 现在,您可以启动rails server ,而不是指向本地文件系统,它现在应该指向您的存储桶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM