Use Sonic object storage with Node.js
Since Sonic is a S3-compatible object storage, you can use s3 libraries, like the S3Client provided by the AWS SDK. To begin, create a storage zone in your account and select the region and storage tier that you prefer. Pushr will create a bucket and will configure CDN routing automatically to cache and accelerate your bucket's content globally.
Create a storage zone
Each storage zone is a bucket + a CDN configuration applied automatically. From the CDN section in your dashboard, create a new storage zone:
In the popup window that appears, name your zone and select a storage region:
Now with your new storage zone created, click on it to manage it. Navigate to the Storage & Hostnames tab to reveal your storage endpoint, access key, secret key and the bucket name that was generated for you automatically:
Configure Sonic
Install the AWS SDK package:
npm install @aws-sdk/client-s3
Configure the Sonic S3 endpoint and credentials in /utils/s3.js
:
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({
endpoint: "https://s3.de01.sonic.r-cdn.com",
forcePathStyle: false,
region: "us-east-1",
credentials: {
accessKeyId: process.env.S3_KEY,
secretAccessKey: process.env.S3_SECRET
}
});
export { s3Client };
Note: the region
variable is set for compatiblity with the AWS SDK, especially bucket creation, but is currently not being used by Sonic. Instead, to create a new bucket, you need to create a new storage zone in your account, and a bucket will be created and attached to it automatically.
Bucket policy
Sonic does not support editing of bucket policies. There is one default mixed access policy that applies to each bucket. It has the following characteristics:
• Unauthenticated requests can download files from the bucket url/CDN hostname via direct links.
• Only authenticated requests can write, list and tag objects towards the S3 endpoint. This behaviour is equivalent to disabling AWS's Block Public Access setting.