aws / s3 gem与JRuby不兼容

时间:2013-07-16 09:44:34

标签: ruby file-upload amazon-s3 jruby

我在s3上编写了一个代码来上传文件,它在Ruby上运行良好。

我试图用jruby运行相同的代码,但我的代码在上传功能之前被卡住了

我遵循的步骤

  1. 安装gem: -

    jruby -S gem install aws-s3
    
  2. 我的代码

    require 'aws/s3'
    local_file = ARGV[0]
    bucket = ARGV[1]
    AWS::S3::Base.establish_connection!(
        access_key_id: 'put_access_key',
        secret_access_key: 'put_secret_access_key')
    base_name = File.basename(local_file)
    AWS::S3::S3Object.store(
        base_name,
        File.open(local_file),
        bucket,
        access: :public_read)
    puts 'Uploaded!'
    url = s3_obj.url_for(
        "#{local_file}",
        "#{bucket}",
        authenticated: false)
    puts "#{url}"
    
  3. 像这样的文件

    jruby file_name.rb upload_file_name.html bucket
    

1 个答案:

答案 0 :(得分:1)

我发现使用带有jruby的aws-s3 gem有一个问题。

所以,我尝试使用“aws-sdk”gem

  1. jruby -S gem install aws-sdk
  2. 编写代码以将文件上载到s3
  3. require 'aws-sdk'
    file_name = ARGV[0]
    bucket = ARGV[1]
    AWS.config(
      access_key_id: 'PUT ACCESS KEY',
      secret_access_key: 'PUT SECRET KEY')
    s3 = AWS::S3.new
    base_name = File.basename(file_name)
    o = s3.buckets[bucket].objects[base_name].write(File.open(file_name))
    puts '--------------Uploaded!-------------------'
    o.acl = :public_read_write
    url = o.public_url
    puts "#{url}"
    

    1. 将其保存在ruby文件中并按以下

      运行
        

      jruby file_name.rb upload_file_name.html path_bucket_where_to_upload

    2.      使用此DOC,了解如何使用“gem aws-sdk”