traxxx/src/utils/s3.js

41 lines
836 B
JavaScript

'use strict';
const config = require('config');
const AWS = require('aws-sdk');
const fs = require('fs');
const nanoid = require('nanoid');
async function init() {
const filepath = './public/img/sfw/animals/j0iiByCxGfA.jpeg';
const endpoint = new AWS.Endpoint('s3.wasabisys.com');
const s3 = new AWS.S3({
// region: 'eu-central-1',
endpoint,
credentials: {
accessKeyId: config.s3.accessKey,
secretAccessKey: config.s3.secretKey,
},
});
try {
const data = await s3.listBuckets().promise();
const file = fs.createReadStream(filepath);
const key = `img/${nanoid()}.jpg`;
const status = await s3.upload({
Bucket: config.s3.bucket,
Body: file,
Key: key,
ContentType: 'image/jpeg',
}).promise();
console.log(data);
console.log(status);
} catch (error) {
console.log(error);
}
}
init();