AWS S3 direct upload from rails backend results in S3 bucket items with hex names and no file extensions

494 Views Asked by At

Anyone have this issue with S3 where the files you store from your rails backend don't show up with the filename you specify in the attach method? For example:

    book.epub.attach(io: File.open("<filepath>/tangrenjie.epub"), filename: "rangrenjie.epub", content_type: 'application/epub+zip')

This results in the file having its name as "qsqyksxtpq66uk6vvyn6mtam17cp" on S3:

here's a screenshot of what the bucket looks like; the bucket is public and all the links download when you click each one, but anything requiring a src url doesn't work

I uploaded one manually on the S3 console and everything actually worked. its name wasn't changed which leads to me to think it's an issue from rails to S3.

I tried adding to the CORS tab on S3; but I'm not sure what I would put since

  1. rails sends credentials and the files do get to S3 and
  2. I don't think localhost counts as a domain

It wouldn't be a problem, but the dependencies I'm using need a public url -- the url for the file I uploaded manually on S3 works (of course), but everything from my rails app ends up getting a giant key as its file name without any file extension (see screenshot) and they don't work when I use them as src url's in my app.

I've gone through guides like https://edgeguides.rubyonrails.org/active_storage_overview.html#s3-service-amazon-s3-and-s3-compatible-apis

I've been getting help from the author of the dependency I'm using, but he has no idea as he hasn't used S3 in a while.

here's the current CORS rule I have in S3:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
    </CORSRule>
</CORSConfiguration>

I'm using rails 6.0.3 as an api with ActiveStorage along with the AWS S3 service, using the aws-sdk-s3 gem, using root user credentials to store files associated with objects on the rails backend app.

0

There are 0 best solutions below