I am trying to implement direct upload to amazonS3 on my app following these instructions : https://devcenter.heroku.com/articles/direct-to-s3-image-uploads-in-rails
I have created a bucket on S3 and downloaded the credentials. Following the tutorial I have added an initializer to my rails app :
Aws.config.update({
region: 'Ireland',
credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']),
})
S3_BUCKET = Aws::S3::Resource.new.bucket(ENV['S3_BUCKET'])
I have set a before_action on my hotel_controller :
before_action :set_s3_direct_post, only: [:new, :edit, :create, :update]
private
def set_s3_direct_post
@s3_direct_post = S3_BUCKET.presigned_post(key: "uploads/#{SecureRandom.uuid}/${filename}", success_action_status: '201', acl: 'public-read')
end
and I have customized my form with the appropriate JS :
<%= form_for(@hotel, url: account_hotels_path(params[:account_id]), html: { class: 'directUpload', data: { 'form-data' => (@s3_direct_post.fields), 'url' => @s3_direct_post.url, 'host' => URI.parse(@s3_direct_post.url).host } }) do |f| %>
<%= f.hidden_field :account_id, :value => params[:account_id] %>
<div class="form-group">
<%= f.label :city %>
<%= f.text_field :city %>
</div>
<div class="form-group">
<%= f.label :postcode %>
<%= f.text_field :postcode %>
</div>
<div class="form-group">
<%= label_tag "Number of rooms" %>
<%= number_field_tag "room_number"%>
</div>
<div class="form-group">
<%= label_tag "Number of Beds in each room" %>
<%= number_field_tag "bed_number"%>
</div>
<div class="field">
<%= f.label "Photo" %><br>
<%= f.file_field "photo[image_url]" %>
</div>
<div class="form-group">
<%= f.submit %>
</div>
<% end %>
<% content_for(:after_js) do %>
<script>
$('.directUpload').find("input:file").each(function(i, elem) {
var fileInput = $(elem);
var form = $(fileInput.parents('form:first'));
var submitButton = form.find('input[type="submit"]');
var progressBar = $("<div class='bar'></div>");
var barContainer = $("<div class='progress'></div>").append(progressBar);
fileInput.after(barContainer);
console.log(form);
console.log(form.data('form-data'));
console.log(form.data('url'));
console.log(fileInput)
fileInput.fileupload({
fileInput: fileInput,
url: form.data('url'),
type: 'POST',
autoUpload: true,
formData: form.data('form-data'),
paramName: 'file', // S3 does not like nested name fields i.e. name="user[avatar_url]"
dataType: 'XML', // S3 returns XML if success_action_status is set to 201
replaceFileInput: false,
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
progressBar.css('width', progress + '%')
},
start: function (e) {
submitButton.prop('disabled', true);
progressBar.
css('background', 'green').
css('display', 'block').
css('width', '0%').
text("Loading...");
},
done: function(e, data) {
submitButton.prop('disabled', false);
progressBar.text("Uploading done");
// extract key and generate URL from response
var key = $(data.jqXHR.responseXML).find("Key").text();
var url = '//' + form.data('host') + '/' + key;
// create hidden field
var input = $("<input />", { type:'hidden', name: fileInput.attr('name'), value: url })
form.append(input);
},
fail: function(e, data) {
submitButton.prop('disabled', false);
progressBar.
css("background", "red").
text("Failed");
}
});
});
</script>
<% end -%>
I have also changed the CORS SETTINGS of my bucket as was suggested in the tutorial.
However everytime I start uploading a file, the request fails, I get :
query.self-c64a74367bda6ef8b860f19e74df08927ca99d2be2ac934e9e92d5fd361e0da4.js?body=1:10244 OPTIONS https://quickbed.s3.ireland.amazonaws.com/ net::ERR_NAME_NOT_RESOLVED
and
What is going wrong here ?
Ireland
is not a valid region, in the sense that you need here... which is a regional endpoint.
You're looking for eu-west-1
.
http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region