This library works best when you set the following environment variables:
S3_UPLOAD_KEY=AAAAAAAAAAAAAAAAAAAA
S3_UPLOAD_SECRET=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
S3_UPLOAD_BUCKET=name-of-s3-bucket
S3_UPLOAD_REGION=us-east-1
Once these ENVs are set no other configuration is needed. Next-s3-upload will automatically read these ENVs when uploading files.
If you cannot define the environment variables from the previous section, you can create your own and pass them into the API route:
// pages/api/s3-upload.js
import { APIRoute } from "next-s3-upload";
export default APIRoute.configure({
accessKeyId: process.env.CUSTOM_ENV_ACCESS_KEY_ID,
secretAccessKey: process.env.CUSTOM_ENV_SECRET_ACCESS_KEY,
bucket: "bucket-name",
region: "us-east-1"
});
Remember to always store your accessKeyId
and secretAccessKey
in environment variables. It's a security risk to check-in the plain text version of these keys when using version control. Environment variables work best because they keep the actual values out of commit history.
You can also set the bucket endpoint as well as the path style using the API route. This is only needed if you are using a non-AWS host.
// pages/api/s3-upload.js
import { APIRoute } from "next-s3-upload";
export default APIRoute.configure({
// ..
endpoint: "https://my.endpoint",
forcePathStyle: true
});
Now your upload will work with any S3 compatible web host.