Ruby and Amazon S3 how to open file and authentica

2019-08-17 07:21发布

问题:

How do I open a file on Amazon S3 and authenticate?

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

Update:

Instead of gem "aws-s3"

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

回答1:

Is your question how to write a file to S3? If so, using the aws-s3 gem, you would do as follows:

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)


回答2:

Why not use the aws-s3 gem?