Ask A Question

Notifications

You’re not receiving notifications from this thread.

Direct Uploads with ActiveStorage Discussion

I'm running to a small error I think.

Alright, so when I try to get the a file to upload It seems to begin the process and then commit but then it stops. I get a completed 200 ok but the issue is that it doesn't actually get the file to upload nor does it create a new post. 

I have restarted my server and tried bundle install. I've also looked through the Github to try and see if I missed anything and so far that is a no. I have also went over the process of s3cmd again to verify if everything is working correctly and it seems as if this checks out also. 

The only difference with our code is that I'm being lazy and using my access key and secret key directly in my storage.yml file and not using <%= ENV["S3_KEY"] %> and <%= ENV["S3_SECRET"] %>.

What do you think my issue could be? Could someone please help me? 

Thank you so much ahead of time :) 
Reply

Little late to the party here but if you add localhost:3000 to CORS in your bucket it should work. example in S3:
[
{
"AllowedHeaders": [
"Authorization"
],
"AllowedMethods": [
"GET"
],
"AllowedOrigins": [
""
],
"ExposeHeaders": [],
"MaxAgeSeconds": 3000
},
{
"AllowedHeaders": [
"
"
],
"AllowedMethods": [
"GET",
"POST",
"PUT"
],
"AllowedOrigins": [
"https://localhost:3000"
],
"ExposeHeaders": [],
"MaxAgeSeconds": 3000
},
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"GET",
"POST",
"PUT"
],
"AllowedOrigins": [
"http://localhost:3000"
],
"ExposeHeaders": [],
"MaxAgeSeconds": 3000
}
]

Reply
HI, 
I am having an error in production.

In development locally everything is working perfectly but in production I am getting this error:

NoMethodError (undefined method `upload' for nil:NilClass)
Did somebody has a same or similar error?
Reply
Verify if the image exists before:

<% if post.photo.attached? %>
   <%= image_tag post.photo %>
<% end %>
Reply
When the image is uploaded to DigitalOcean Spaces, the permissions are as private. Does anyone know how to upload the image with public permissions?
Reply
Hey Chris, thanks for another great video.

I've been trying to get direct uploads to work on production (it's fine on localhost) but I'm getting a Cross Origin Error when I test live with SSL enabled.
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

Any idea why this might be happneing?

Cheers.


Reply
In case anyone bumps into this response, I found out that I had setup my domain name incorrectly on the CORS configuration in Digital Ocean. So you might want to check that config out.
Reply
IS this an alternative to using carrierwave etc? And the benefits are that this is faster? Can someone confirm this. Thanks!
Reply
It's not taht it's faster, just that it's a framework officially included in rails. This means it'll be supported and easy to integrate in future rails versions.

Reply

I'm trying to do this using AWS S3 and am getting a console error: POST http://localhost:3000/rails/active_storage/direct_uploads 500 (Internal Server Error). Any steps I should check? The only thing I can think of is that it could have something to do with the modifications I used for S3 instead of DigitalOcean?

Reply

Does anyone know if there's a way to specify the filename before uploading it to DigitalOcean or S3?

Reply

I followed this line for line - uploads work just fine except that when you update the record with new files the old ones are deleted. Is this the inteneded action or can you allow adding files to the collection?

Reply
Reply

FWIW, this is working in 6.1.4 (see my comment https://github.com/rails/rails/issues/36374#issuecomment-877695770)

To config/application.rb add:
config.active_storage.replace_on_assign_to_many = false

Reply

On a side note any thoughts on adding an update to handle the deleting of uploaded files?

Reply

Thanks for this episode, this was really interesting !
I still have a question though, what about direct upload validations ? For example what if you want to make sure that your users can't upload files that are too big or what if you want to accept only "image/png" format? I believe this should be done at AWS S3 level as file are uploaded directly to S3 and not going through the Rails application. Do you know how to do this ?

Reply

Great episode, thanks again Chris!

Reply

The link to configure s3cmd for digital ocean is broken, it goes to a 404 page

Reply
Reply

This is really cool, much appreciated!!! I made a typo which took me a minute to resolve <AllowHeaders> instead of <AllowedHeaders>. I'm on a template kick and put this video into one here: https://github.com/rlogwood/rails_addons.

Reply

I am curious about what's rails new -m ( the "-m" means) thanks!

Reply

Hey, do you know if you can upload to a folder structure within an s3 bucket? I'm planning on having users (who have UUID's) to have direct uploads behind the scenes to UUID/filename (virtual folder... would hopefully be created if it wasn't there), I've read ActiveStorage didn't support it but it was an older article. If not, I wonder how significant of a risk it would be to just have all files in a bucket.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.