简体   繁体   中英

file upload backup api to backup file on aws s3 in nodejs

I want a make an API that will take a file or folder path from the user and upload it to AWS s3 I made progress but

when the user gives a file path it's searching the file path in the server, not in the user's pc

I know I made a mistake but I don't know how to connect API from the users pc and get access to system files

here is code for the post route

router.post("/create/:id", auth, async (req, res) => {
  try {
    let form = new multiparty.Form();
    form.parse(req, async (err, fields, files) => {
      console.log(fields);
      console.log(files);
      //check if user has access to project
      const user_id = req.userId;
      const project_id = req.params.id;
      const user_access = await check_user_access_project(user_id, project_id);
      const user = await User.findById(user_id);
      const project = await Project.findById(project_id);
      if (user_access === 1) {
        //create version
        const version = new Version({
          project_id: project_id,
          user_id: user_id,
          versionName: fields.versionName[0],
          version_description: fields.versionDescription[0],
          version_file: [],
        });
        const version_data = await version.save();
        console.log(version_data);

        let version_id = version_data._id;

        //sync folders to s3
        const version_folder_path = fields.files_path[0];
        let key = `${user.firstName}_${user_id}/${project.projectName}/${fields.versionName[0]}`;
        const version_folder_list = await sync_folders(
          version_folder_path,
          key
        );

        console.log("version folder list", version_folder_list);

        //update version with version folders
        await Version.findByIdAndUpdate(
          version_id,
          {
            $set: {
              version_file: version_folder_list,
            },
          },
          { new: true }
        );
        //wait for version update
        await version.save();

        //send response
        res.json({
          success: true,
          version: version_data,
        });
      } else {
        res.status(401).json({
          success: false,
          message: "User does not have access to project",
        });
      }
    });
  } catch (error) {
    res.status(400).json({ message: error.message });
  }
});

here is the folder sync code

const sync_folders = async (folder_path, key) => {
  function getFiles(dir, files_) {
    files_ = files_ || [];
    var files = fs.readdirSync(dir);
    for (var i in files) {
      var name = dir + "/" + files[i];
      if (fs.statSync(name).isDirectory()) {
        getFiles(name, files_);
      } else {
        files_.push(name);
      }
    }
    return files_;
  }

  const files = getFiles(folder_path);
  console.log(files);
  const fileData = [];
  for (let i = 0; i < files.length; i++) {
    const file = files[i];
    console.log(file);

    const fileName = file.split("/").pop();
    const fileType = file.split(".").pop();
    const fileSize = fs.statSync(file).size;
    const filePath = file;

    const fileBuffer = fs.readFileSync(filePath);

    //folder is last part of folder path (e.g. /folder1/folder2/folder3)
    const folder = folder_path.split("/").pop();

    console.log("folder: " + folder);
    //split filepath
    const filePath_ = filePath.split(folder).pop();

    let filekey = key + "/" + folder + filePath_;

    console.log("filekey: " + filekey);

    const params = {
      Bucket: bucket,
      Key: filekey,
      Body: fileBuffer,
      ContentType: fileType,
      ContentLength: fileSize,
    };
    const data = await s3.upload(params).promise();
    console.log(data);
    fileData.push(data);
  }

  console.log("file data", fileData);
  console.log("files uploaded");
  return fileData;
};

if some buddy can help me pls I need your help

You need to post the item in a form rather than just putting the directory path of user in and then upload the result to your s3 bucket.

This might be a good start if you're new to it:

https://www.w3schools.com/nodejs/nodejs_uploadfiles.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM