I'm just starting to learn to program and have set up an uploader in Rails 4 using Carrierwave, Fog and Amazon S3, by following railstutorial.org. This works.
Now I would like to expand by adding that when a new user signs up for an account, the application automatically copies a default file for that user to an S3 bucket.
How to do this? Should I do so by creating a new uploader, and then add a controller method that copies to the S3 bucket? And how should I adjust the carrier_wave initializer?
Update: I think as @jethroo suggests I need to use the aws-s3 gem and found this relevant post. The post however is still a bit difficult to read for me, especially such as where exactly to place the suggested code. So what have I done:
-
I established a 1:1 relationship between the model Drawing and the model Organization. Drawing contains 2 variables:
organization_idandfile_path.File_pathshould include the string to the file that we will save to the S3 bucket. -
To my Gemfile I have added
gem 'aws-sdk', '~> 2'Am I right to include this gem rather than aws-s3? 'Aws-s3' seems a bit old and I understand that 'aws-sdk' is the main gem of Amazon Aws and includes the s3 gem? -
To the organizations controller I have added the 3rd line in
def createas well as the private method below.
.
def create
@organization = Organization.new(organizationnew_params)
if @organization.save
upload_empty_xml(@organization.id)
redirect_to root_url
end
end
private
def upload_empty_graph(id)
s3 = Aws::S3::Resource.new(
credentials: Aws::Credentials.new(ENV['S3_ACCESS_KEY'], ENV['S3_SECRET_KEY']), # These environmental variables I had already set for the uploader I have previously created.
region: 'eu-west-1')
xmlfile = 'other/graph.xml' # This file is located in app/assets/other/graph.xml
directory = "graphs/#{model.class.to_s.underscore}/#{mounted_as}/#{id}"
s3.bucket('mybucketname').object(ENV['S3_ACCESS_KEY']).upload_file(xmlfile)
# I would like to give the file a custom name: "graph#{id)". How to specify this?
# I would like it to save to the specified directory. How to give this order?
end
- I don't yet understand how to create the association between
file_pathin the Drawing model and the uploaded file in the S3 bucket. With the uploader I created earlier this was achieved usingmount_uploader :avatar, AvatarUploaderbut in this new set up I don't use an uploader.
Any ideas to help me move forward / suggestion to improve the code?
Aucun commentaire:
Enregistrer un commentaire