0

I'm working on deploying my Rails app to Heroku with Aws S3 to store images, but so far I've failed upload images. My S3 bucket name follows guidelines, and is written like bucket-name. I've checked and my name is the same in heroku config, and in my credential, storage, and production files.

in rails console:

Photo.third.image_file.attach io: File.open("/Users/userName/Downloads/IMG.jpg"),
filename: "image.jpg", content_type: "image/jpg"

returns:

TRANSACTION (2.2ms)  COMMIT
  S3 Storage (109.0ms) Uploaded file to key: fz63yb7xfj0861sqmdad3x4x2k3z 
(checksum: hk9mcGlRlxW7YwkC4uHbEQ==)
Traceback (most recent call last):
        1: from (irb):1
Aws::S3::Errors::InvalidBucketName (The specified bucket is not valid.)

and Heroku log shows:

2021-11-28T10:46:57.315492+00:00 heroku[router]: at=info method=GET
path="/api/v1/photos" host=bucket-name-api.herokuapp.com 
request_id=36d62d53-4870-40ff-a9bb-1764262d4781 
fwd="98.113.64.2" dyno=web.1 connect=0ms service=1288ms status=304 bytes=445 
protocol=https

and if I make the request from my React front end Heroku log returns:

2021-11-28T10:56:54.864356+00:00 heroku[router]: at=info method=OPTIONS 
path="/api/v1/photos/3/" host=bucket-name-api.herokuapp.com
request_id=2622d278-1b4a-4e77-a6a1-da8a98961fb8 fwd="98.113.64.2" dyno=web.1 
connect=0ms service=27ms status=200 bytes=300 protocol=https 
2021-11-28T10:56:54.908875+00:00 heroku[router]: at=info method=PUT 
path="/api/v1/photos/3/" host=bucket-name-api.herokuapp.com 
request_id=d7fe2cb8-8943-4751-b3e3-5779d63f4f83 fwd="98.113.64.2" dyno=web.1 
connect=0ms service=17ms status=422 bytes=679 protocol=https
  • I am using Shrine to upload files to AWS, though have little knowledge of other techniques. Yet it seems you are not even passing the bucket name, nor the region in this bit of code : `Photo.third.image_file.attach io: File.open("/Users/userName/Downloads/IMG.jpg"), filename: "image.jpg", content_type: "image/jpg"` Can you elaborate a bit on how default AWS credentials are passed. Also have you created the bucket from your AWS account ? – Maxence Nov 28 '21 at 17:52
  • Both Heroku and my credentials.yml contain the bucket name, secret key and access ID, which are used in storage.yml like so. `amazon: service: S3 access_key_id: Rails.application.credentials.dig(:aws, :access_key_id) secret_access_key: Rails.application.credentials.dig(:aws, :secret_access_key) region: us-east-1 bucket: Rails.application.credentials.dig(:aws, :bucket)` I may be wrong but I think this information is implied when using active storage attachments. Yes, I have an s3 bucket. – Corey Colligan Dec 01 '21 at 22:13
  • Are you sure you are digging the value correctly ? Does our credentials file has something like : `aws: bucket:` – Maxence Dec 01 '21 at 22:45
  • yes. `Rails.application.credentials.aws[ :bucket]` and `Rails.application.credentials.dig(:aws, :bucket)` both return the bucket name. I'm absolutely stumped. – Corey Colligan Dec 02 '21 at 00:17
  • I've also ran the same commands in heroku rails console and returned the correct values – Corey Colligan Dec 02 '21 at 00:27
  • Googling "invalid bucket name" returns threads such as : https://stackoverflow.com/questions/49548451 where it is explained bucket names should comply to some specific naming rules. But if you have created your bucket directly from the AWS website, AWS should have enforced these rules directly from their website. You should not experience such error. I am really stuck. – Maxence Dec 02 '21 at 00:35
  • Additionally, I'm unable to permit the attachment. I put a byebug in my update method, and entered `params[:image_file]` which returned `permitted: false` – Corey Colligan Dec 02 '21 at 07:03

0 Answers0