S3
Store data in Amazon S3 or another S3-compatible object store.
The driver uses fetch and request signing from aws4fetch, so it works in Node.js and edge runtimes.
#Usage
Driver name: s3
#Setup
Create a bucket with your provider and collect the following values:
- Access Key ID
- Secret Access Key
- Bucket name
- Endpoint
- Region
Make sure to install the required dependency:
npm i aws4fetchConfigure the required driver options:
import { createStorage } from "unstorage";
import s3Driver from "unstorage/drivers/s3";
const storage = createStorage({
driver: s3Driver({
accessKeyId: "", // Access Key ID
secretAccessKey: "", // Secret Access Key
endpoint: "",
bucket: "",
region: "",
}),
});Options:
accessKeyId(required): Access key ID.secretAccessKey(required): Secret access key.endpoint(required): S3-compatible service endpoint.bucket(required): Bucket name.region(required): Bucket region; useautofor Cloudflare R2.bulkDelete: Uses the bulk delete API to speed upclear()(default:true). Set it tofalsewhen the provider does not implement DeleteObjects.lib: An importedaws4fetchmodule or a function that returns it.
#Setting Headers
You can specify HTTP headers when storing items using the options parameter:
// Set Content-Type and Cache-Control
await storage.setItemRaw("image.png", imageBuffer, {
headers: {
"Content-Type": "image/png",
"Cache-Control": "max-age=31536000",
},
});
// Set custom S3 metadata
await storage.setItem("document.json", jsonString, {
headers: {
"Content-Type": "application/json",
"x-amz-meta-author": "john-doe",
},
});Supported headers include:
Content-TypeCache-ControlContent-DispositionContent-EncodingContent-LanguageExpires- Custom metadata via
x-amz-meta-*prefixed headers
Note:
getMeta()only returns custom metadata headers (those withx-amz-meta-*prefix). Standard headers likeContent-Typeare set on the S3 object but not returned bygetMeta().
#Tested providers
Any standards-compatible S3 provider should work. Contributions documenting additional tested providers are welcome.
#Amazon S3
Read more in Amazon S3.
Options:
- Set
endpointtohttps://s3.[region].amazonaws.com/
#Cloudflare R2
Read more in Cloudflare R2.
Options:
- Set
endpointtohttps://[uid].r2.cloudflarestorage.com/ - Set
regiontoauto