如何在 DigitalOcean 空间中调整图像大小?

问题描述

我正在开发带有 express、multer 和 digitalocean 的服务器。

我正在将图像保存到数字海洋空间。

如何减小存储在数字海洋空间中的图像的大小?

我正在考虑使用一个尖锐的节点包。但没必要。

下面是我的代码

// 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" && ext !== ".jpg" && 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,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",response) {
  response.sendFile(__dirname + "/public/success.html");
});

app.get("/error",response) {
  response.sendFile(__dirname + "/public/error.html");
});

app.post("/upload",response,next) {
  upload(request,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.");
});

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)