Reputation: 523
I am new to S3 and Rails so got stuck. I am wondering if I can directly pass object to S3.
My controller:
def start_upload
@forecasts = Forecast.all
Forecast.export_to_s3(@forecasts)
end
Model:
def self.export_to_s3(data)
---AWS configs ---
Aws.config = { :access_key_id => aws_access_key, :secret_access_key => aws_secret_access_key, :region => aws_region }
s3 = Aws::S3::Client.new(region:aws_region)
resp = s3.put_object(
:bucket => aws_bucket,
:key => aws_bucket_key_forcast,
:body => IO.read(data)
)
end
Upvotes: 1
Views: 677
Reputation: 45
What are you trying to do? What is you goal?
You should try to serialize your object before you push it to your bucket.
def start_upload
@forecasts = Forecast.all
Forecast.export_to_s3(@forecasts.to_json)
end
def self.export_to_s3(data)
# ---AWS configs ---
Aws.config = { :access_key_id => aws_access_key, :secret_access_key => aws_secret_access_key, :region => aws_region }
tmpfile = Tempfile.new('forecast')
tmpfile.write(data)
tmpfile.close
s3 = Aws::S3::Client.new(region:aws_region)
resp = s3.put_object(
:bucket => aws_bucket,
:key => aws_bucket_key_forcast,
:body => IO.read(tmpfile)
)
tmpfile.unlink
end
Upvotes: 1