简体   繁体   English

如何从亚马逊s3手动上传的Rails(图像/文本)中读取文件?

[英]How do I read files, in Rails, (images/text) from Amazon s3 that were uploaded manually?

First, I'm very new to Rails, and Stack Overflow, and currently bushwhacking my way through the learning curve, so I apologize if my post is laughably noobish. 首先,我对Rails和Stack Overflow都很陌生,目前在学习曲线上一路狂奔,所以如果我的帖子是可笑的,那么我很抱歉。 Here's my problem: 这是我的问题:

I'm trying to build a blog from scratch using Amazon s3 to store resources, such as all the images and text. 我正在尝试使用Amazon s3从头开始构建博客来存储资源,例如所有图像和文本。 I have no need for Users uploading stuff to the site, so I figure I can just upload everything manually to s3 using their console at console.aws.amazon.com. 我不需要用户将内容上传到网站,所以我想我可以使用console.aws.amazon.com上的控制台手动上传所有内容到s3。 I'm trying to code Rails to read folders from my s3 bucket and then point the images/text to the right spots in the database. 我正在尝试编写Rails来读取我的s3存储桶中的文件夹,然后将图像/文本指向数据库中的正确位置。 For example, I could make a folder called "Post1" in s3, then my Rails app reads the URL of everything from inside that folder and points it to post1 in the database, then I can simply call up the image URL, or text, in the view and, theoretically, display it. 例如,我可以在s3中创建一个名为“Post1”的文件夹,然后我的Rails应用程序从该文件夹中读取所有内容的URL并将其指向数据库中的post1,然后我可以简单地调用图像URL或文本,在视图中,理论上,显示它。 My question is, how do I get my app to read s3 folders? 我的问题是,如何让我的应用程序读取s3文件夹?

I know I'm probably reinventing the wheel here, but my goal is also to learn Rails. 我知道我可能在这里重新发明轮子,但我的目标也是学习Rails。 Thanks everyone in advance. 提前谢谢大家。 You can find my project on GitHub. 你可以在GitHub上找到我的项目

My blog model: 我的博客模型:

class BlogPost < ActiveRecord::Base
  has_attached_file :download,
                    :storage => :s3,
                    :s3_credentials => Proc.new{|a| a.instance.s3_credentials }

  def s3_credentials
    {:bucket => "S3_BUCKET_NAME", :access_key_id => "AWS_ACCESS_KEY_ID", :secret_access_key => "AWS_SECRET_ACCESS_KEY"}
  end

end

Here is my blog controller: 这是我的博客控制器:

class BlogController < ApplicationController
    before_action :load_posts


      def index  #this method puts all the stuff pulled from 'load_posts' and packages it nicely in a instanced variable
    @blogPosts = BlogPost.all
    end

    def load_posts  #this method should pull posts from AWS s3 and add them to the database (like magic)

    end

end

I'm sure there are gaping holes in my code...like something to actually talk to s3. 我确信我的代码中存在大量漏洞......就像实际与s3交谈一样。 I know the bulk of what I want to do probably goes in my model. 我知道我想做的大部分内容都可能出现在我的模型中。 I feel really stuck. 我觉得很困惑。 I'm hoping there's some simple, magical Ruby method that I can use, like s3.file.read or something. 我希望我可以使用一些简单,神奇的Ruby方法,比如s3.file.read或者其他东西。 Another option would be to create a form for myself, as though I'm a User, that connects everything together in the database, then uploads to s3. 另一种选择是为我自己创建一个表单,就像我是一个用户一样,将数据库中的所有内容连接在一起,然后上传到s3。 Yet, I've read people run into issues, like dynos timing out, when they try to upload large files (like big images) through heroku and onto s3. 然而,当他们尝试通过heroku上传大文件(如大图像)到s3时,我已经读过人们遇到问题,比如dynos超时。 I've read tutorials on how to upload directly to s3, but I might as well skip that process and just click the 'upload' button on the s3 console. 我已经阅读了有关如何直接上传到s3的教程,但我也可以跳过这个过程,只需点击s3控制台上的“上传”按钮即可。 Anything will help. 一切都会有所帮助。 Thanks again all. 再次感谢所有人。

You are using the aws-sdk, good, I'll hit you with the s3.file.read equivalent then: Aws::S3::Object#get . 你正在使用aws-sdk,好的,我会用s3.file.read等同于你:Aws Aws::S3::Object#get

# create your bucket first
s3_file = bucket.object['myfile.txt'].get({response_target: '/tmp/myfile.txt'})

I think you can then do s3_file.body . 我想你可以做s3_file.body

Lastly, you are re-inventing the wheel and should check-out carrierwave with aws-sdk . 最后,你正在重新发明轮子,应该用aws-sdk检查载波

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

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