图像从s3损坏

问题描述

这是我的上传代码(节点)

// generate a uuid string
            const uuid = uuidv4(); // type string

            // upload to wasabi

            fs.readFile(file.tempFilePath,"utf8",(err,data) => {
                if (err) {
                    throw (err);
                }
                // upload the object
                s3.upload(
                    {
                        Body: data,Key: uuid + "-" + file.name,Bucket: "files",Metadata: { 'ext': ".png" },// ContentLength: file.size
                    },data) => {
                        if (err) {
                            console.log(err);
                            throw (err);
                        }
                        console.log(data); 
                });
            });

对象实际上确实到达了存储桶。它具有超过0个字节。它大约是源图像的一半,但我只是假设有人在压缩它(芥末或其他东西)。 我正在使用 express-fileupload ,它的配置是:


app.use(fileUpload({

    createParentPath: true,limits: {
        fileSize: 2 * 1024 * 1024 * 1024 //2MB max file(s) size
    },useTempFiles: true,tempFileDir: "/tmp/",}));

最终,我直接从wasabi Web客户端直接下载了该文件,并尝试在图像查看器中将其打开,他们都说文件中有错误或者它们不支持文件类型(.png,因此受支持)。 图像只有〜150kb。 本质上,文件到达了那里但已损坏。

解决方法

utf8文本编码。但是.png不是文本。只需删除编码选项,它就可以工作。