When new code is pushed to strapi and deployed, the images are lost

Am I doing something wrong, when depoloying new changes or it can not be done without this? I am using Railway to deploy strapi

1 Like

The issue you’re experiencing with lost images after deploying new changes to Strapi is a common problem, especially when using platforms like Railway. This occurs because Strapi, by default, stores uploaded files locally in the server’s filesystem. When you deploy new changes, the server instance is typically recreated, causing the loss of these locally stored files.

To solve this problem, you need to use a persistent storage solution. Here are some steps and options to consider:

  1. Use a cloud storage provider:
    Strapi supports various cloud storage providers through plugins. Popular options include:

    • Amazon S3
    • Google Cloud Storage
    • Cloudinary
  2. Configure Strapi to use the chosen provider:
    Install the appropriate plugin and configure it in your Strapi project. For example, for Amazon S3:

    npm install @strapi/provider-upload-aws-s3
    

    Then, configure the provider in ./config/plugins.js:

    module.exports = ({ env }) => ({
      upload: {
        config: {
          provider: 'aws-s3',
          providerOptions: {
            accessKeyId: env('AWS_ACCESS_KEY_ID'),
            secretAccessKey: env('AWS_ACCESS_SECRET'),
            region: env('AWS_REGION'),
            params: {
              Bucket: env('AWS_BUCKET'),
            },
          },
        },
      },
    });
    
  3. Set environment variables:
    In Railway, set the necessary environment variables for your chosen storage provider.

  4. Update your deployment process:
    Ensure that your deployment process includes installing the new plugin and applying any necessary configurations.

  5. Migrate existing files:
    If you have existing files, you’ll need to migrate them to the new storage solution manually.

  6. Update your application’s file access:
    Ensure your frontend application is updated to fetch files from the new storage location.

Additional considerations:

  • Database persistence: Ensure your database is also persistent. Railway offers options for this.
  • Strapi version: Make sure you’re using a recent version of Strapi that supports your chosen storage provider.
  • Cost: Be aware of potential costs associated with cloud storage solutions.

By implementing a cloud storage solution, your uploaded files will persist across deployments, solving the issue of lost images.

For Railway specifically, you might also consider using their volume storage feature if available, but cloud storage providers often offer more flexibility and scalability.

Remember to test your setup thoroughly in a staging environment before applying changes to production.

Thank you for reply, I will definitely try it

Could you elaborate a bit on using Railway volumes?

I am using the mount path of /public/uploads. But still, the data gets uploaded to the server’s filesystem.