44 lines
930 B
JavaScript
Executable File
44 lines
930 B
JavaScript
Executable File
'use strict';
|
|
|
|
const config = require('config');
|
|
const fs = require('fs');
|
|
const { nanoid } = require('nanoid');
|
|
const { Upload } = require('@aws-sdk/lib-storage');
|
|
const { S3Client } = require('@aws-sdk/client-s3');
|
|
|
|
async function init() {
|
|
const filepath = './public/img/sfw/animals/j0iiByCxGfA.jpeg';
|
|
|
|
const s3 = new S3Client({
|
|
region: 'eu-central-1',
|
|
endpoint: 's3.eu-central-1.wasabisys.com',
|
|
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 new Upload({
|
|
client: s3,
|
|
params: {
|
|
Bucket: config.s3.bucket,
|
|
Body: file,
|
|
Key: key,
|
|
ContentType: 'image/jpeg',
|
|
},
|
|
}).done();
|
|
|
|
// console.log(data);
|
|
console.log(status);
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
init();
|