AWS Ruby SDK核心:将文件上传到S3 [英] AWS Ruby SDK CORE: Upload files to S3

查看:134
本文介绍了AWS Ruby SDK核心:将文件上传到S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用这是我的代码:

require 'aws-sdk-core'
Aws.config = {
  :access_key_id => MY_ACCESS_KEY
  :secret_access_key => MY_SECRET_KEY,
  :region => 'us-west-2'
}

s3 = Aws::S3.new
resp = s3.put_object(
  :bucket => "mybucket",
  :key => "myfolder/upload_me.sql",
  :body => "./upload_me.sql"
)

现在,上面的代码运行并创建一个密钥myfolder/upload_me.sql,该密钥仅写了一行,而./upload_me.sql是错误的.文件upload_me.sql有几行.

Now, Above code runs and creates a key myfolder/upload_me.sql which has only one line written and that is ./upload_me.sql which is wrong. The file upload_me.sql has several lines.

预期的行为是在文件S3上将文件upload_me.sql上载为mybucket/myfolder/upload_me.sql.但是相反,它只向mybucket/myfolder/upload_me.sql写了一行,即./upload_me.sql

Expected behaviour is to upload the file upload_me.sql on S3 as mybucket/myfolder/upload_me.sql. But instead it just writes one line to mybucket/myfolder/upload_me.sql and that is ./upload_me.sql

现在,如果我省略:body部分,如下所示:

Now, If I omit the :body part as below:

s3 = Aws::S3.new
resp = s3.put_object(
  :bucket => "mybucket",
  :key => "myfolder/upload_me.sql",
)

然后它只创建一个空键,称为mybucket/myfolder/upload_me.sql,该键甚至不可下载(嗯,即使下载了,它也没用)

Then it just creates and empty key called mybucket/myfolder/upload_me.sql which is not even downloadable (well, even if it gets downloaded, it is useless)

你能指出我要去哪里了吗?

Could you point me where I am going wrong?

这是put_object方法的ruby-SDK核心文档: http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/V20060301.html#put_object-instance_method

Here is ruby-SDK-core documentation for put_object Method: http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/V20060301.html#put_object-instance_method

更新: 如果我尝试使用AWS-CLI上传相同的文件,则可以正常上传.这是命令:

UPDATE: If I try to upload the same file using AWS-CLI, it gets uploaded fine. Here is the command:

aws s3api put-object --bucket mybucket --key myfolder/upload_me.sql --body ./upload_me.sql

推荐答案

因此,在度过了令人沮丧的周日下午的htis问题后,我终于破解了它.我真正需要的是 :body => IO.read("./upload_me.sql")

So, After spending a frustrating sunday afternoon on htis issue, I finally cracked it. What I really needed is :body => IO.read("./upload_me.sql")

所以我的代码如下:

s3 = Aws::S3.new
resp = s3.put_object(
  :bucket => "mybucket",
  :key => "myfolder/upload_me.sql",
  :body => IO.read("./upload_me.sql")
)

这篇关于AWS Ruby SDK核心:将文件上传到S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆