简体   繁体   English

Ruby和Amazon S3如何打开文件并进行身份验证?

[英]Ruby and Amazon S3 how to open file and authenticate?

How do I open a file on Amazon S3 and authenticate? 如何在Amazon S3上打开文件并进行身份验证?

I know how to do this with paperclip, but how is it done when having to open file? 我知道如何用回形针做到这一点,但是当必须打开文件时怎么做?

My helper 我的助手

            File.open("#{RAILS_ROOT}/public/xml/#{output}.xml", "w") do |f|

               f.puts("<?xml version='1.0' encoding='UTF-8'?>")            
               f.puts("<site>")     

               f.puts("<general name='general' type='general'><imagePath>photographer/image/</imagePath><moviePath>../photographer/flv/</moviePath></general>")             
               f.puts("#{xmlmenu.to_xml}")
               f.puts("#{xmlmovies.to_xml}")                           
               f.puts("#{xmltextpages.to_xml}")

               f.puts("</site>")
            end 

UPDATE 更新

 My helper file:
    module Admin::XmlHelper
    require 'builder'
    require 'aws/s3'

    def update_xml(output) 
        AWS::S3::Base.establish_connection!(
        :access_key_id     => 'mykey',
        :secret_access_key => 'mykey'
      )

    file = "xml/#{output}.xml"

    content = "#{
                   f.puts("<?xml version='1.0' encoding='UTF-8'?>")            
                   f.puts("<site>")     
                   f.puts("<general name='general' type='general'><imagePath>photographer/image/</imagePath><moviePath>../photographer/flv/</moviePath></general>")             
                   f.puts("#{xmlmenu.to_xml}")
                   f.puts("#{xmlmovies.to_xml}")                           
                   f.puts("#{xmltextpages.to_xml}")
                   f.puts("</site>")}"

    AWS::S3::S3Object.store(file, content, "mybucket", :access => :public_read)

        end         

    end

I get the error in view: 我看到错误:

uninitialized constant AWS::S3::Base

http://pastie.org/2587071 http://pastie.org/2587071

Update: 更新:

Instead of gem "aws-s3" 代替gem "aws-s3"

Should it be: gem 'aws-s3', :require => 'aws/s3' 应该是: gem 'aws-s3', :require => 'aws/s3'

Is your question how to write a file to S3? 您的问题是如何将文件写入S3吗? If so, using the aws-s3 gem, you would do as follows: 如果是这样,使用aws-s3 gem,您将执行以下操作:

AWS::S3::Base.establish_connection!(
  :access_key_id => MY_ACCESS_KEY,
  :secret_access_key => MY_SECRET_ACCESS_KEY
 )

 content = "this is the content";

AWS::S3::S3Object.store("any_file_name.html", content, "my_bucket_name", :access => :public_read)

为什么不使用aws-s3 gem?

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

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