在supabase上上传文件:错误RequestInit:发送正文时需要双工选项

问题描述 投票:0回答:2

我正在尝试使用 node.js 在 supabase 上上传文件,但收到此错误: RequestInit:发送正文时需要双工选项。我不知道这意味着什么。我的代码做错了什么?

router.post("/", async (req, res) => {
    const datosForm = req.body;
 
    const supabase = createClient(
        process.env.SUPABASE_URL,
        process.env.SUPABASE_KEY
    );

    const form = formidable({
        multiples: false,
        //uploadDir: "public/images",
        keepExtensions: true,
    })
    //await form.parse(req)
    form.parse(req, async(err, fields, files) => {
        if (err) {
            console.log("error: " + err);
        } else {
            console.log("files: ", files.foto.newFilename);
            let imagePath;
            try {
                const mascotaDB = new Mascota(fields);
                if(files.foto.originalFilename != ""){
                    const ext = path.extname(files.foto.filepath);
                    const newFileName = "image_"+Date.now()+ext;
                    console.log("newFileName: ", newFileName);
                    try{
                        const{data, error}=await supabase.storage.from("articulos").upload(
                            newFileName,    
                            fs.createReadStream(files.foto.filepath),   
                            {
                                cacheControl: '3600',
                                upsert: false,     
                                contentType: files.foto.type,  
                            }
                        );                    
                        imagePath = newFileName;
                        console.log("path: ", imagePath);
                        console.log("mascota: ", imagePath);
                        mascotaDB.path=imagePath;
                        
                        await mascotaDB.save()
        
                        res.redirect("/articulos") 
                    }catch(err){
                        console.log("error: "+err);
                    }
                    
                }
               
            
            } catch (error) {
                console.log(error)
            }
        }
        
    })

    
});

我没有在我在 supabase 创建的存储桶中获取要上传的图像

node.js express file-upload supabase
2个回答
4
投票

具体错误参考这个

在请求中,init 参数需要属性

"duplex": "half"

可能是您的 NodeJS 版本导致了此问题,如下所示:https://github.com/nodejs/node/issues/46221


0
投票

我将读取的文件转换为base64字符串,然后使用decode方法生成数组缓冲区(如何上传base64字符串也包含在supabase的官方文档中:https://supabase.com /docs/reference/javascript/storage-from-upload?example=upload-file-using-arraybuffer-from-base64-file-data)

import { decode } from 'base64-arraybuffer';

const fileData = fs.readFileSync(zipFilePath);
const buffedInput = fileData.toString("base64");

// upload zip file to supabase
const res = await supabase.storage
              .from(bucketName)
              .upload(uploadPath, decode(buffedInput), { contentType: 'application/zip' }); 
    
console.log(res);
© www.soinside.com 2019 - 2024. All rights reserved.