Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Dieuz's avatar
Level 1

Laravel Mix, Upload files to S3

Hello,

I would like to upload my assets file to my S3 Bucket after running "npm run production".

How can I do that? I tried installing npm package like this one: https://github.com/webpack-contrib/s3-plugin-webpack

but I have no clue how to make it work with laravel mix.

Any help appreciated

0 likes
3 replies
dniccum's avatar

Hi, I am using Fortrabbit for my hosting and they have a S3-like type of object storage where you can store assets; in this case my mix-compiled CSS and JS. I was able to utilize the mix.webpackConfig parameter to build out my extension.

Install S3Plugin

npm install webpack-s3-plugin --save

Add this to your webpack.mix.js

mix.webpackConfig({
        plugins: [
            new S3Plugin({
                s3Options: {
                    accessKeyId: '${KEY}',
                    secretAccessKey: '${SECRET}',
                    endpoint: '${SERVER}'
                },
                s3UploadOptions: {
                    Bucket: '${BUCKET}'
                },
                directory: 'public'
            })
        ]
    });

and replace ${KEY}, ${SECRET}, ${SERVER}, and ${BUCKET} with the appropriate strings..

Environment detection

Because I didn't want my assets to be uploaded when developing locally, I wrapped the above JS snippet with the following if statement:

if (process.env.npm_config_env === 'staging' || process.env.npm_config_env === 'production') {

}

Then you can run the following command to trigger the upload

npm run dev --env=staging

Hope this helps.

2 likes

Please or to participate in this conversation.