-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigrate-uploads-to-minio.ts
More file actions
97 lines (76 loc) · 2.19 KB
/
migrate-uploads-to-minio.ts
File metadata and controls
97 lines (76 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import * as fs from 'fs';
import * as path from 'path';
import { Client } from 'minio';
// Ajustar com nome dos buckets
const BUCKETS = {
avatars: 'avatars',
banners: 'banners',
documents: 'documents',
general: 'general',
};
/*
Detecta automaticamente QUAL bucket deve receber o arquivo
baseado no prefixo do nome do arquivo.
Ex:
avatar_123.png → bucket "avatars"
*/
function detectBucket(filename: string): string {
const lower = filename.toLowerCase();
if (lower.startsWith('avatar_')) return BUCKETS.avatars;
if (lower.startsWith('banner_')) return BUCKETS.banners;
if (lower.startsWith('doc_')) return BUCKETS.documents;
return BUCKETS.general;
}
//Configurar o client MinIO
const minio = new Client({
endPoint: 'ieee_minio', // nome do container
port: 9000,
useSSL: false,
accessKey: 'minioadmin',
secretKey: 'minioadmin',
});
//Função principal
async function migrate() {
const uploadsDir = path.join(process.cwd(), 'uploads');
if (!fs.existsSync(uploadsDir)) {
console.log('❌ Pasta uploads/ não existe. Nada para migrar.');
return;
}
const files = fs.readdirSync(uploadsDir);
if (files.length === 0) {
console.log('✔️ Pasta uploads/ está vazia.');
return;
}
console.log(`🔍 Encontrados ${files.length} arquivos para migrar...\n`);
const report: any[] = [];
for (const file of files) {
const bucket = detectBucket(file);
const filePath = path.join(uploadsDir, file);
try {
// Garantir bucket existente
const exists = await minio.bucketExists(bucket).catch(() => false);
if (!exists) await minio.makeBucket(bucket);
// Enviar arquivo
await minio.fPutObject(bucket, file, filePath);
// Remover local após sucesso
fs.unlinkSync(filePath);
report.push({
file,
bucket,
status: 'migrated',
});
console.log(`✔️ ${file} → bucket "${bucket}"`);
} catch (err) {
console.error(`❌ Erro ao migrar ${file}:`, err);
report.push({
file,
bucket,
status: 'error',
error: err.message,
});
}
}
console.log('\nMIGRAÇÃO CONCLUÍDA\n');
console.table(report);
}
migrate();