Amazon Web Services (AWS) S3 is a popular way to upload and store files. You can use AWS S3 together with Vercel to upload, store, and retrieve objects.
Copy link to headingVercel Blob
Vercel Blob provides globally-distributed file storage for web applications, deeply integrated with Vercel's infrastructure. Blob is powered by AWS's S3 infrastructure, ensuring 99.999999999% of reliability. Your files are always secure and available without additional complexity.
Blob automatically leverages Vercel's global caching and data transfer, delivering your content quickly and cost-effectively. With built-in support for large files (up to 5TB), multi-part uploads, resumability, and concurrency, Blob handles even the most demanding media workloads, letting you focus on building great product experiences.
Pricing is transparent and usage-based, closely aligned with Amazon S3 but optimized further by Blob’s integration into Vercel’s delivery network. Get started with Vercel Blob.
Copy link to headingWith an Existing S3 Bucket
If you already have an existing AWS S3 bucket configured, you can retrieve (or generate) the access key and secret key for that IAM User. Ensure it has access to the proper policies: s3:DeleteObject, s3:GetObject, s3:ListBucket, s3:PutObject, s3:PutObjectAcl.
AWS credentials (e.g. AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY) and region configuration (e.g. AWS_REGION) can now be used directly as Environment Variables for Vercel deployments. Ensure these values are inside your Vercel project.
These variables are the default names expected by the AWS SDK, which means the user no longer has to configure credentials when using it. For example:
Copy link to headingCreating a New S3 Bucket
We’ve created an example Next.js application that allows you to upload photos to an S3 bucket.
- In Object Ownership, select "ACLs enabled" and "Bucket owner prefered"
- In Block Public Access settings for this bucket, uncheck "Block all public access".
Copy link to headingCreate a new IAM User
- Create a new IAM user
- Choose programmatic access
- Select "Attach policies directly"
- Add
s3:DeleteObject,s3:GetObject,s3:ListBucket,s3:PutObject,s3:PutObjectAcl.
Copy link to headingSave the access key and secret key for the IAM User
- Select the newly created user (IAM > Users > "your-user") and navigate to "Security Credentials".
- Under "Access Keys", create a key and save this information. We will use this in the next step.
Copy link to headingCreate an .env.local file similar to .env.example
- In your
env.localfile, use the information from your access key, along with the region and bucket name. - Do not adjust the naming of the keys, only input your values. This is to ensure S3 Client can read them as defaults.
Copy link to headingConfigure CORS to enable uploads from your browser
- Navigate to your bucket, and go to the "Permissions" tab.
- Scroll down to find "Cross-origin resource sharing (CORS)" and click "Edit" on the right side.
- Paste the following code below.
Copy link to headingTest
- Start our Next.js app at
localhost:3000 - Choose a
.pngor.jpgfile - You should see your file successfully uploaded to S3
Copy link to headingUploading files on the server
You can use Vercel Serverless Functions to upload files to AWS S3 on the server. After creating an instance of the AWS S3 client, you can use upload to create a new object with the given file name and body (based on a stream).
Copy link to headingUploading files in the browser
Alternatively, you can allow file uploads directly from the browser. For example, a user can select a file using an input, which then generates a pre-signed POST from a Route Handler to allow for secure uploads.
The API Route to generate the pre-signed POST is as follows:
If you need to support pausing and resuming uploads, as well as other advanced file upload strategies, explore libraries like Evaporate.