从S3下载文件到Rails 4应用

时间:2023-02-06 00:00:39

I have an AWS VM that runs a daily task and generates several files. I want my Rails app to download these files and place them in a folder within the app. Is there a gem or method in Ruby that can do this?

我有一个AWS VM,运行一个日常任务并生成几个文件。我想让我的Rails应用下载这些文件,并将它们放在应用程序中的文件夹中。Ruby中是否有gem或方法可以做到这一点?

I know how to do it in bash with s3cmd and I guess I could create a script to get them this way, but looking for a more native rails way.

我知道如何使用s3cmd在bash中实现它,我想我可以创建一个脚本来实现这种方式,但是要寻找一种更原始的rails方式。

I am using the data in these files for the app, but I don't want the users to be able to download them.

我正在用这些文件中的数据做应用,但我不想让用户下载它们。

3 个解决方案

#1


12  

The aws-sdk v2 gem provides a simple interface for downloading objects from Amazon S3.

aws-sdk v2 gem为从Amazon S3下载对象提供了一个简单的接口。

require 'aws-sdk'

s3 = Aws::S3::Resource.new(
  region: 'us-east-1',
  access_key_id: '...',
  secret_access_key: '...'
)

s3.bucket('bucket-name').object('key').get(response_target: '/path/to/file')

#2


0  

There is official Amazon AWS Ruby SDK: http://aws.amazon.com/sdk-for-ruby/

亚马逊AWS Ruby SDK: http://aws.amazon.com/sdk-for ruby/

There is also an open source gem called Fog, that may be easier to use for simpler projects: http://fog.io/

还有一个名为Fog的开放源码gem,它可能更容易用于更简单的项目:http://fog.io/

#3


0  

In the v1 aws-sdk, the code to download from s3 is...

在v1 aws-sdk中,从s3下载的代码是……

File.open(local_file_path, "wb") do |file|
  s3_object(s3_key).read do |chunk|
    file.write(chunk)
  end
end

#1


12  

The aws-sdk v2 gem provides a simple interface for downloading objects from Amazon S3.

aws-sdk v2 gem为从Amazon S3下载对象提供了一个简单的接口。

require 'aws-sdk'

s3 = Aws::S3::Resource.new(
  region: 'us-east-1',
  access_key_id: '...',
  secret_access_key: '...'
)

s3.bucket('bucket-name').object('key').get(response_target: '/path/to/file')

#2


0  

There is official Amazon AWS Ruby SDK: http://aws.amazon.com/sdk-for-ruby/

亚马逊AWS Ruby SDK: http://aws.amazon.com/sdk-for ruby/

There is also an open source gem called Fog, that may be easier to use for simpler projects: http://fog.io/

还有一个名为Fog的开放源码gem,它可能更容易用于更简单的项目:http://fog.io/

#3


0  

In the v1 aws-sdk, the code to download from s3 is...

在v1 aws-sdk中,从s3下载的代码是……

File.open(local_file_path, "wb") do |file|
  s3_object(s3_key).read do |chunk|
    file.write(chunk)
  end
end