Seggellion / ericksons

0 stars 0 forks source link

There's a stupid bug where every time I push code, it deletes all the images #25

Open Seggellion opened 3 months ago

Seggellion commented 3 months ago

This issue occurs because Heroku uses an ephemeral filesystem, meaning that any files (like images) you upload to the server are not persisted across deployments. When you redeploy your app, any uploaded files that were stored locally on the Heroku server are lost.

To solve this, you need to use a persistent storage solution for your uploaded files. Typically, this is done using a cloud storage service like Amazon S3, Google Cloud Storage, or Azure Blob Storage.

Here's how you can set up your Rails app to use Amazon S3 with Active Storage on Heroku:

  1. Set Up Amazon S3 Create an S3 Bucket:

Go to the Amazon S3 Console and create a new bucket. Make a note of the bucket name, region, and your AWS access keys. Configure Bucket Permissions:

Ensure that your bucket has the correct permissions to allow your app to upload and access files.

  1. Update Your Rails App for S3 Add the aws-sdk-s3 gem: In your Gemfile, add:

ruby Copy code gem 'aws-sdk-s3', require: false Then run bundle install.

Configure Active Storage to Use S3: Update your config/storage.yml file to include an S3 configuration:

yaml Copy code amazon: service: S3 access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %> secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %> region: <%= ENV['AWS_REGION'] %> bucket: <%= ENV['AWS_BUCKET'] %> Update Environment Configuration: In your config/environments/production.rb, set Active Storage to use the S3 service:

ruby Copy code config.active_storage.service = :amazon Set Environment Variables on Heroku: Use the Heroku CLI to set your environment variables:

bash Copy code heroku config:set AWS_ACCESS_KEY_ID=your_access_key_id heroku config:set AWS_SECRET_ACCESS_KEY=your_secret_access_key heroku config:set AWS_REGION=your_s3_region heroku config:set AWS_BUCKET=your_s3_bucket_name

  1. Deploy and Test Deploy Your Changes to Heroku:

Push your code to Heroku and let it deploy. Test the Image Upload and Retrieval:

After deployment, try uploading a new logo or image and ensure that it persists across deployments. Migrate Existing Images (If Any):

If you have existing images stored locally, you’ll need to migrate them to S3. This can usually be done by re-uploading them through the admin interface or by writing a script to handle the migration.

  1. Clean Up and Maintenance Backup Configuration: Ensure you have a backup strategy in place for your S3 bucket. By setting up S3 for Active Storage, you ensure that your uploaded images persist across deployments, solving the issue you encountered on Heroku.
Seggellion commented 2 months ago

Just build the updater and let the app run without file hosting.