62 lines
1.8 KiB
JavaScript
62 lines
1.8 KiB
JavaScript
import { S3Client, ListObjectsV2Command, GetObjectCommand } from '@aws-sdk/client-s3';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const s3 = new S3Client({
|
|
endpoint: 'https://storageedu.microtecinformaticacurso.com.br:443',
|
|
region: 'us-east-1',
|
|
credentials: {
|
|
accessKeyId: 'minioadmin',
|
|
secretAccessKey: 'MiniO2026!Seguro'
|
|
},
|
|
forcePathStyle: true,
|
|
tls: false // Disable SSL verification for self-signed certs
|
|
});
|
|
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
|
|
|
async function run() {
|
|
const destDir = path.join(__dirname, 'photos');
|
|
if (!fs.existsSync(destDir)) {
|
|
fs.mkdirSync(destDir, { recursive: true });
|
|
}
|
|
|
|
try {
|
|
console.log('Listando objetos no bucket fotos-alunos...');
|
|
const listCommand = new ListObjectsV2Command({
|
|
Bucket: 'fotos-alunos'
|
|
});
|
|
const listResponse = await s3.send(listCommand);
|
|
const objects = listResponse.Contents || [];
|
|
console.log(`Encontrados ${objects.length} objetos.`);
|
|
|
|
for (const obj of objects) {
|
|
if (!obj.Key) continue;
|
|
console.log(`Baixando ${obj.Key}...`);
|
|
const getCommand = new GetObjectCommand({
|
|
Bucket: 'fotos-alunos',
|
|
Key: obj.Key
|
|
});
|
|
const getResponse = await s3.send(getCommand);
|
|
const stream = getResponse.Body;
|
|
if (stream) {
|
|
const destPath = path.join(destDir, obj.Key);
|
|
const fileStream = fs.createWriteStream(destPath);
|
|
// Convert web stream / readable stream to local file
|
|
const buffer = await stream.transformToByteArray();
|
|
fs.writeFileSync(destPath, buffer);
|
|
console.log(`Salvo em ${destPath}`);
|
|
}
|
|
}
|
|
console.log('Download concluído!');
|
|
} catch (err) {
|
|
console.error('Erro:', err);
|
|
}
|
|
}
|
|
|
|
run();
|