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

发布于 2024-12-06 03:26:33 字数 2072 浏览 0 评论 0原文

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

我知道如何使用回形针执行此操作,但是在必须打开文件时如何完成此操作?

我的助手

            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 

更新

 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

我在视图中收到错误:

uninitialized constant AWS::S3::Base

http://pastie.org/2587071

更新:

而不是 gem "aws-s3"

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

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别念他 2024-12-13 03:26:33

您的问题是如何将文件写入S3?如果是这样,使用 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)

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)
清秋悲枫 2024-12-13 03:26:33

为什么不使用 aws-s3 gem?

Why not use the aws-s3 gem?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文