On my Rails 3 application, I would like to export static JSON data to an Amazon S3 bucket, which can be later retrieved and parsed by an AJAX call from said application.
在我的Rails 3应用程序中,我想将静态JSON数据导出到Amazon S3存储桶,稍后可以通过来自所述应用程序的AJAX调用来检索和解析该数据库。
The JSON will be generated from the app's database.
JSON将从应用程序的数据库生成。
My design requirements probably will only need something like a rake task to initiate the export to S3. Every time the rake task is initiated, it'll overwrite the files. Preferably the file name will correspond to the ID number of the record where the JSON data is generated from.
我的设计要求可能只需要像rake任务那样启动导出到S3。每次启动rake任务时,它都会覆盖文件。优选地,文件名将对应于生成JSON数据的记录的ID号。
Does anyone have any experience with this and can point me in the right direction?
有没有人有这方面的经验,可以指出我正确的方向?
1 个解决方案
#1
4
This can be accomplished with the aws-sdk gem.
这可以通过aws-sdk gem实现。
Your task could be broken into two basic steps: 1) generate a temporary local file with your json data, 2) upload to S3. A very basic, procedural example of this:
您的任务可分为两个基本步骤:1)使用您的json数据生成临时本地文件,2)上传到S3。这是一个非常基本的程序性例子:
require 'aws-sdk'
# generate local file
record = Record.find(1)
file_name = "my-json-data-#{record.id}"
local_file_path = "/tmp/#{file_name}"
File.open(local_file_path, 'w') do |file|
file.write(record.to_json)
end
# upload to S3
s3 = AWS::S3.new(
:access_key_id => 'YOUR_ACCESS_KEY_ID',
:secret_access_key => 'YOUR_SECRET_ACCESS_KEY')
bucket = s3.buckets['my-s3-bucket-key']
object = bucket.objects[file_name]
object.write(Pathname.new(local_file_path))
Check out the S3Object docs for more info.
查看S3Object文档以获取更多信息。
#1
4
This can be accomplished with the aws-sdk gem.
这可以通过aws-sdk gem实现。
Your task could be broken into two basic steps: 1) generate a temporary local file with your json data, 2) upload to S3. A very basic, procedural example of this:
您的任务可分为两个基本步骤:1)使用您的json数据生成临时本地文件,2)上传到S3。这是一个非常基本的程序性例子:
require 'aws-sdk'
# generate local file
record = Record.find(1)
file_name = "my-json-data-#{record.id}"
local_file_path = "/tmp/#{file_name}"
File.open(local_file_path, 'w') do |file|
file.write(record.to_json)
end
# upload to S3
s3 = AWS::S3.new(
:access_key_id => 'YOUR_ACCESS_KEY_ID',
:secret_access_key => 'YOUR_SECRET_ACCESS_KEY')
bucket = s3.buckets['my-s3-bucket-key']
object = bucket.objects[file_name]
object.write(Pathname.new(local_file_path))
Check out the S3Object docs for more info.
查看S3Object文档以获取更多信息。