简体   繁体   English

谷歌驱动器 api 上传和显示 JWT 认证

[英]Google drive api upload & display with JWT authentication

I want to upload files into my google drive account with Node.JS but while uploading the files I want to create a folder for these files and give a specific access to some emails to view them into my google drive account, the basic scenario is i have a form which i upload for example 3 files (video,pdf,image), then i made a multiselect input field that put 3 emails inside then i click upload, i want only these 3 emails to display this folder and everyone with other email cannot view the files therefor my login system is JsonWebToken ( JWT ) so is it possible to do this, I just want to upload files to google driver and then my frontend checks who has logged in with JWT token access then display these files in the frontend.我想使用 Node.JS 将文件上传到我的 google drive 帐户,但是在上传文件时我想为这些文件创建一个文件夹并授予对某些电子邮件的特定访问权限以将它们查看到我的 google drive 帐户,基本情况是我有一个我上传的表格,例如 3 个文件(视频、pdf、图像),然后我制作了一个多选输入字段,将 3 封电子邮件放入其中,然后我单击上传,我只希望这 3 封电子邮件显示此文件夹以及其他 email 的每个人无法查看文件因此我的登录系统是 JsonWebToken ( JWT ) 所以是否可以这样做,我只想将文件上传到谷歌驱动程序然后我的前端检查谁使用 JWT 令牌访问登录然后在前端显示这些文件.

I made this code but i cannot find a way to add a file and then add a permission to this file who can view these files:我制作了这段代码,但我找不到添加文件然后向该文件添加可以查看这些文件的权限的方法:

const { google } = require('googleapis');
const googleScope = ['https://www.googleapis.com/auth/drive']
const googleKey = process.env.GOOGLE_KEY;
const fs = require('fs')
const express = require('express');
const router = express.Router();

const Multer = require("multer");


const authenticateGoogle = () => {
  const auth = new google.auth.GoogleAuth({
    keyFile: __dirname + "/book-375320-56d9bd2b6239.json",
    scopes: googleScope,
  });
  return auth;
};

const multer = Multer({
  storage: Multer.diskStorage({
    destination: function (req, file, callback) {
      callback(null, `${__dirname}/fff`);
    },
    filename: function (req, file, callback) {
      callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
    },
  }),
  limits: {
    fileSize: 5 * 1024 * 1024,
  },
});

const uploadToGoogleDrive = async (file , auth ) => {
    const fileMetadata = {
      name: file.originalname,
      parents: ["1PLzjikkBXnSpBNi7OZxh3-XhEAIji_94"]
    };
  
    const media = {
      mimeType: file.mimetype,
      body: fs.createReadStream(file.path),
    };
  
    const driveService = google.drive({ version: "v3", auth });
  
    const response = await driveService.files.create({
      requestBody: fileMetadata,
      media: media,
      fields: "id",
    });
    return response;
};


const deleteFile = (filePath) => {
    fs.unlink(filePath, () => {
      console.log("file deleted");
    });
};

router.post("/uploadGD", multer.single("file") , async (req , res , next ) => {
    try {
      if (!req.file) {
        res.status(400).send("No file uploaded.");
        return;
      }
      const auth = authenticateGoogle();
      const response = await uploadToGoogleDrive(req.file, auth);
      deleteFile(req.file.path);
      res.status(200).json({ response });
    } catch (err) {
      console.log(err);
}});

module.exports = router;

I expect to upload files with permissions that who can view my files and then get JWT token and if the JWT token has the same email as the permission files he can view it inside the frontend.我希望上传具有可以查看我的文件的权限的文件,然后获得 JWT 令牌,如果 JWT 令牌与权限文件具有相同的 email,他可以在前端查看它。

Your code appears to be using service account authorization.您的代码似乎正在使用服务帐户授权。 Which also implies that you have granted your service account access to the folder in question.这也意味着您已授予您的服务帐户访问相关文件夹的权限。

This means it will be up to your code to decide who has access.这意味着您的代码将决定谁有权访问。

I would suggest that in your login system you simply add a new claim to the user which your code can use to decide if this person has access.我建议在您的登录系统中,您只需向用户添加一个新的声明,您的代码可以使用该声明来决定此人是否具有访问权限。

How you do this is hard to know without understanding more about the Login to system used by your users.如果不更多地了解用户使用的登录系统,就很难知道您是如何做到这一点的。

either way this is not something a service account can do so its up to you too implement within your own system.无论哪种方式,这都不是服务帐户可以做到的,因此也取决于您在自己的系统中实施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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