FeaturesStorage

Storage

ShipAny supports uploading files to AWS S3-compatible storage systems, such as AWS S3 / Cloudflare R2 / Tencent Cloud COS, etc.

Configure Cloud Storage

Choose your cloud storage platform, create a bucket, and set up access keys and access domain.

Then fill in the relevant parameters in your project’s configuration file:

.env
STORAGE_ENDPOINT = ""
STORAGE_REGION = ""
STORAGE_ACCESS_KEY = ""
STORAGE_SECRET_KEY = ""
STORAGE_BUCKET = ""
STORAGE_DOMAIN = ""

Upload AI Images to Cloud Storage

Upload AI-generated images (returned base64 data) to cloud storage.

app/api/demo/gen-image/route.ts
import { newStorage } from "@/lib/storage";
 
const storage = new Storage();
 
const filename = `image_${new Date().getTime()}.png`;
const key = `shipany/${filename}`;
const body = Buffer.from(image.base64, "base64");
 
try {
  const res = await storage.uploadFile({
    body,
    key,
    contentType: "image/png",
    disposition: "inline",
  });
 
  console.log("upload file success:", res);
} catch (err) {
  console.log("upload file failed:", err);
}

The body parameter accepted by uploadFile is of Buffer type. You can pass a file Buffer downloaded from a URL or a Buffer read from a local file.

Read Local Files and Upload to Cloud Storage

app/api/demo/gen-image/route.ts
import path from "path";
import { readFile } from "fs/promises";
import { newStorage } from "@/lib/storage";
 
const storage = newStorage();
 
const content = await readFile(path.join(process.cwd(), "sing.m4a"));
 
const res = await storage.uploadFile({
  body: content,
  key: `shipany/sing_${getUuid()}.mp3`,
  contentType: "audio/mpeg",
});

Note: path and readFile cannot be used in edge runtime, so when deploying on Cloudflare, you cannot read local files.

Download Remote Images and Upload to Cloud Storage

Given a remote image URL, download the image and upload it to cloud storage.

app/api/demo/gen-image/route.ts
import { newStorage } from "@/lib/storage";
 
const storage = newStorage();
 
const res = await storage.downloadAndUpload({
  url: "https://shipany.ai/logo.png",
  key: `shipany/logo.png`,
});

References