Skip to content

Instantly share code, notes, and snippets.

@1mehdifaraji
Created September 3, 2025 11:04
Show Gist options
  • Select an option

  • Save 1mehdifaraji/39ee498323dd5281ba4c1aea6ac69b1b to your computer and use it in GitHub Desktop.

Select an option

Save 1mehdifaraji/39ee498323dd5281ba4c1aea6ac69b1b to your computer and use it in GitHub Desktop.
// Delete
import {
S3Client,
ListObjectsV2Command,
DeleteObjectsCommand,
} from "@aws-sdk/client-s3";
import dotenv from "dotenv";
dotenv.config();
const s3Client = new S3Client({
region: "auto",
endpoint: process.env.ARVAN_ENDPOINT,
credentials: {
accessKeyId: process.env.ARVAN_KEY,
secretAccessKey: process.env.ARVAN_SECRET,
},
});
const emptyBucket = async (bucketName) => {
try {
let continuationToken;
do {
const listResponse = await s3Client.send(
new ListObjectsV2Command({
Bucket: bucketName,
ContinuationToken: continuationToken,
})
);
const objects = listResponse.Contents || [];
if (objects.length === 0) break;
const deleteParams = {
Bucket: bucketName,
Delete: { Objects: objects.map((obj) => ({ Key: obj.Key })) },
};
await s3Client.send(new DeleteObjectsCommand(deleteParams));
console.log(`βœ… Deleted ${objects.length} objects`);
continuationToken = listResponse.IsTruncated
? listResponse.NextContinuationToken
: undefined;
} while (continuationToken);
console.log(`πŸŽ‰ Bucket "${bucketName}" is now empty!`);
} catch (err) {
console.error(`❌ Failed to empty bucket:`, err);
}
};
const main = async () => {
if (!process.env.ARVAN_BUCKET) {
console.error("❌ ARVAN_BUCKET not set in .env!");
process.exit(1);
}
await emptyBucket(process.env.ARVAN_BUCKET);
};
main();
// Upload
import fs from "fs";
import path from "path";
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import dotenv from "dotenv";
dotenv.config();
const DIST_FOLDER = path.resolve(process.cwd(), "dist");
const s3Client = new S3Client({
region: "auto",
endpoint: process.env.ARVAN_ENDPOINT, // https://s3.ir-thr-at1.arvanstorage.ir
credentials: {
accessKeyId: process.env.ARVAN_KEY,
secretAccessKey: process.env.ARVAN_SECRET,
},
});
const getContentType = (filePath) => {
const ext = path.extname(filePath).toLowerCase();
switch (ext) {
case ".html":
return "text/html";
case ".js":
return "application/javascript";
case ".css":
return "text/css";
case ".json":
return "application/json";
case ".png":
return "image/png";
case ".jpg":
case ".jpeg":
return "image/jpeg";
case ".svg":
return "image/svg+xml";
case ".ico":
return "image/x-icon";
case ".woff":
return "font/woff";
case ".woff2":
return "font/woff2";
default:
return "application/octet-stream";
}
};
const uploadFile = async (filePath, key) => {
try {
const fileStream = fs.createReadStream(filePath);
const contentType = getContentType(filePath);
await s3Client.send(
new PutObjectCommand({
Bucket: process.env.ARVAN_BUCKET,
Key: key,
Body: fileStream,
ContentType: contentType,
ACL: "public-read",
})
);
console.log(`βœ… Uploaded: ${key}`);
} catch (err) {
console.error(`❌ Failed to upload ${key}:`, err);
}
};
const uploadDir = async (dirPath, prefix = "") => {
const entries = fs.readdirSync(dirPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dirPath, entry.name);
const key = prefix ? `${prefix}/${entry.name}` : entry.name;
if (entry.isDirectory()) await uploadDir(fullPath, key);
else {
await uploadFile(fullPath, key);
}
}
};
const main = async () => {
if (!fs.existsSync(DIST_FOLDER)) {
console.error("❌ dist folder not found! Run `npm run build` first.");
process.exit(1);
}
console.log("πŸ“¦ Uploading dist folder to Arvan Cloud...");
await uploadDir(DIST_FOLDER);
console.log("πŸŽ‰ Upload complete!");
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment