繁体   English   中英

Google API 驱动器:使用 node.js 上传文件但是,如何使用工作区帐户的 1TB 存储空间而不是用于授权的 15GB 服务帐户存储空间?

[英]Google API Drive: Upload files using node.js but, How use the 1TB storage of a workspace account and not the 15GB of service account used to auth?

我在我的服务器上开发了一个过程,使用 Node.js 将多个文件上传到谷歌驱动器帐户。

  1. 首先,我在我的 Google 工作区帐户中购买了额外的存储空间 (1 TB) 以提供足够的空间。 让我们调用 Google 帐户user.account@domain.com

  2. 然后,我在user.account@domain.com下创建了一个服务帐户,并从 node.js 创建了身份验证所需的所有凭据。

  3. 我已经为模拟过程添加了具有域范围委托的服务 ID 帐户。

  4. 我已经定义了所有需要的范围:

https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.appdata
  1. 最后,我已经添加到 JavaScript 实现中,所有这些都应该需要。 下一个代码只是来自 JavaScript 实现的 auth Google API 的声明:
const { google } = require('googleapis');

// service account key file from Google Cloud console.
const KEYFILEPATH = 'upload-files-server-to-drive-7a5e1bf2dcbe.json';

// Request full drive access.
const SCOPES = ['https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive.appdata'];

// Create a service account initialize with the service account key file and scope needed
const auth = new google.auth.GoogleAuth({
    keyFile: KEYFILEPATH,
    scopes: SCOPES
});

auth.subject = 'user.account@domain.com';

const driveService = google.drive({version: 'v3', auth});

module.exports = {
    driveService
}
  1. 下一个 function 在模块内,这里是上传发生的地方:
const upload = async ( params ) => new Promise (async ( resolve, reject ) => {
    const driveService = params.driveService;

    const fileMetadata = {
        'name': `${params.id_numero}${params.name}`,
        'parents':  [  params.parentId  ]
    };
    
    try {
        if (fs.existsSync(params.folder + params.name)) {
            //file exists
        }else{
            reject({
                result: "error",
                message:"El archivo no existe. No se puede subir nada. " 
            })
            return
        }
    } catch(err) {
        console.log(err)
        reject({
            result: "error",
            message: "Hubo un error" . err 
        })
        return
    }

    // Definición del archivo que se desea subir
    const  media = {
        mimeType: params.mimeType,
        body: fs.createReadStream(params.folder + params.name)
    };
    await driveService.files.create({
        resource: fileMetadata,
        media: media,
        fields: 'id'
    })
    .then( response => {
        console.log(response)
        switch(response.status){
            case 200:
                let file = response.result;
                resolve({
                    result: "success",
                    message:'Created File Id: ' +  response.data.id,
                    url : "http://drive.google.com/uc?export=view&id="+response.data.id,
                    id : response.data.id
                })
                break;
            default:
                console.error('Error creating the file, ' + response.errors);
                reject({
                    result: "error",
                    message:"Error al crear el archivo en Drive, " + response.errors
                })
                break;
        }
    })
    .catch( e => {
        console.log(e)
        reject({
            result: "error",
            message:"ERROR. No se puede acceder a DRIVE " ,
            error : e.errors
        })
        return
    })
})

上面的代码绝对适用于将文件上传到驱动器。 然而,尽管user.account@domain.com有足够的空间(1TB 100% 免费),但所有上传的文件仍在服务帐户默认存储中上传,其中一个只有 15GB。

设置上传到所需存储我缺少什么?

谢谢你的帮助!

文档和参考: https://developers.google.com/admin-sdk/directory/v1/guides/delegation

谷歌 API 范围: https://developers.google.com/drive/api/v3/reference/files/create#auth

对于模拟,您可以尝试:

const auth = new google.auth.GoogleAuth({
    keyFile: './service.json',
    scopes: [
      'https://www.googleapis.com/auth/contacts',...
    ],
    clientOptions: {
      subject: 'email@email.com'
    },
  });

您需要根据您的代码和您正在做的事情来编辑它。

暂无
暂无

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

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