English 中文(简体)
将图像档案上载到AWS-S3:类型 误差:无法读懂未经界定的特性(现成转移-编码)
原标题:Upload image files to AWS-S3: TypeError: Cannot read properties of undefined (reading transfer-encoding )

我正试图用MERN Stack将图像上载到AWS-S3,但我甚至可以回头去工作。 下面是:tutorial ,但我正在获得一种类型Error: 不能读懂未经界定的特性(现成转移-编码)。

我按照一些网站的建议,在我的表格中添加了<条码>encType=多部分/格式数据,但并未奏效。

I also did the relevant logs with debug here and formData.entries() produced the names of my files accurately:

const onImageChange = (event) => {
    const selectedFiles = event.target.files;
    setFiles(selectedFiles);
    log("event.target.files %o", event.target.files);
    log("selectedFiles %o", selectedFiles);
  };

  const handleSubmit = async (event) => {
    event.preventDefault();

    if (files.length === 0) {
      return alert("please select an image");
    }

    const formData = new FormData();
    log("formData %o", formData);

    for (const file of files) {
      formData.append("s3Images", file);
    }

    log("formData after append %o", formData);

    for (const entry of formData.entries()) {
      const [key, value] = entry;
      console.log(`${key}:`, value);
    }

    const result = await fetch(BASE_URL + "/upload", {
      method: "POST",
      body: formData,
    });
    log("result %o", result);

    const data = await result.json();
    log("data %o", data);

难道我的神职人员有错吗?

const uploadWithMulter = () =>
  multer({
    storage: multerS3({
      s3: s3,
      bucket: BUCKET_NAME,
      metadata: function (req, file, cb) {
        cb(null, { fieldname: file.fieldname });
      },
      key: function (req, file, cb) {
        cb(null, file.originalname);
      },
    }),
  }).array("s3Images", 2);

const uploadToAws = (req, res) => {
  const upload = uploadWithMulter();

  upload((req, res, error) => {
    if (error) {
      res.json({ error, msg: "Error occurred while uploading images." });
      return;
    }
    res.json({ msg: "Files uploaded successfully.", files: req.files });
  });
};

router.post("/upload", uploadToAws);
问题回答

I think the error is way you re handling the upload middleware with Multer. In your uploadWithMulter function, you are calling multer() each time it is called. Try this

const uploadWithMulter = multer({
  storage: multerS3({
    s3: s3,
    bucket: BUCKET_NAME,
    metadata: function (req, file, cb) {
      cb(null, { fieldname: file.fieldname });
    },
    key: function (req, file, cb) {
      cb(null, file.originalname);
    },
  }),
}).array("s3Images", 2);

noww you create the multer middleware only once and then using it in the uploadToAws function. The uploadWithMulter function now returns the configured multer middleware.

(确保你具备必要的受扶养人)

我的帽子的位置造成了这一问题。 它应当是上载(req, res,(error) =>{if(error){......,上载(req, res, mis) =>{if(error){......

这项工作:

const uploadToAws = (req, res) => {
  const upload = uploadWithMulter();
  // log("req %o", req);
  upload(req, res, (error) => {
    if (error) {
      res.json({ error, msg: "Error occurred while uploading images." });
      return;
    }
    res.json({ msg: "Files uploaded successfully.", files: req.files });
  });
};




相关问题
how to debug curl call to amazon s3 when it get stuck

I m using the PHP S3 class and this backup script to backup ~500Mb file from Linux server to S3. The call to s3 gets stuck (never returns) and top shows httpd process which consumes 100% CPU and 1% ...

Synchronizing S3 Folders/Buckets [closed]

I have an S3 Bucket that holds static content for all my clients in production. I also have a staging environment which I use for testing before I deploy to production. I also want the staging ...

Pure Javascript app + Amazon S3?

I m looking to confirm or refute the following: For what I have read so far it is not possible to write a web application with only javascript -- no server side logic -- served from Amazon S3 that ...

Using a CDN like Amazon S3 to control access to media

I want to use Amazon S3/CloudFront to store flash files. These files must be private as they will be accessed by members. This will be done by storing each file with a link to Amazon using a mysql ...

What s a good way to collect logs from Amazon EC2 instances?

My app is hosted on an Amazon EC2 cluster. Each instance writes events to log files. I need to collect (and data mine) over these logs at the end of each day. What s a recommended way to collect these ...

热门标签