#node.js #image #multer #image-resizing #multer-s3
Вопрос:
Я разрабатываю сервер с помощью express, multer и digitalocean.
И я сохраняю изображение в цифровом пространстве.
Как я могу уменьшить размер изображений, хранящихся в цифровых океанских пространствах?
И я рассматриваю возможность использования пакета sharp node. Но в этом нет необходимости.
Ниже приведен мой код
// Load dependencies
const aws = require("aws-sdk");
const express = require("express");
const multer = require("multer");
const multerS3 = require("multer-s3");
const sharp = require("sharp");
const path = require("path");
const app = express();
const spacesEndpoint = new aws.Endpoint("sgp1.digitaloceanspaces.com");
const s3 = new aws.S3({
endpoint: spacesEndpoint,
});
const upload = multer({
fileFilter: function (req, file, cb) {
const ext = path.extname(file.originalname);
if (ext !== ".png" amp;amp; ext !== ".jpg" amp;amp; ext !== ".jpeg") {
return cb(new Error("Only images are allowed"));
}
cb(null, true);
},
storage: multerS3({
s3: s3,
bucket: "<MY-BUCKET>",
acl: "public-read",
key: function (request, file, cb) {
console.log(file);
const newFileName = Date.now() "-" file.originalname;
const fullPath = "image/" newFileName;
cb(null, fullPath);
},
}),
}).array("upload", 1);
// Views in public directory
app.use(express.static("public"));
// Main, error and success views
app.get("/", function (request, response) {
response.sendFile(__dirname "/public/index.html");
});
app.get("/success", function (request, response) {
response.sendFile(__dirname "/public/success.html");
});
app.get("/error", function (request, response) {
response.sendFile(__dirname "/public/error.html");
});
app.post("/upload", function (request, response, next) {
upload(request, response, function (error) {
if (error) {
console.log(error);
return response.redirect("/error");
}
response.redirect("/success");
});
});
app.listen(3001, function () {
console.log("Server listening on port 3001.");
});
Спасибо