【问题标题】:create upload session to sharepoint site folder on Office 365 using microsoft graph使用 microsoft graph 在 Office 365 上创建上传会话到共享点站点文件夹
【发布时间】:2021-12-16 23:06:32
【问题描述】:

TLDR;

我有多个 Office 365 SharePoint 网站。我希望注册为 Azure 应用注册的基于 NodeJS 的守护程序写入这些 SharePoint 站点中的多个不同文件夹。我正在努力获取正确的请求 url 语法以创建可恢复的上传会话。微软提供的相当不明确和无效的示例使情况变得更糟。这里的一个重要细节是应用程序在没有用户委托的情况下执行此操作,这意味着没有用户使用应用程序来生成这些文件,而是它是并且应该是拥有文件上传的实体。许多示例展示了如何通过用户委托来做到这一点。一些正在上传的文件包括二进制或文本内容。

我已经阅读了相当多的 ms 文档并提到了许多问题。我什至在 YouTube 上观看了许多视频,并开始使用 microsoft-graph-client 挖掘源代码,以防有见解。我认为我无法查看图形服务器代码,或者这是否会有所帮助。以下只是一些由于密切相关而我仍然开放的部分。

这是我最近的尝试,我觉得应该非常接近:

const msal = require('@azure/msal-node');
const graph = require( '@microsoft/microsoft-graph-client' );
// all that client initialization and authentication work so I omited it for this example

const fileName = 'fileName.txt';
const filePath = 'path/to/file/' + fileName;
const stats = fs.statSync( filePath );
const totalSize = stats.size;
const readStream = fs.createReadStream( filePath );
const fileObject = new graph.StreamUpload( readStream, fileName, totalSize );

const site_id = '' // refers to the identifier for the SharePoint site, something like domain,guid,guid
const drive_id = '' // refers to the site specific drive that would sync to the Corp OneDrive folder
const item_id = '' // refers to the ID for a target folder
let folder1, folder2, folder3 // refer to possible folders. I assumed I could just specify the ID of the last one to upload into it

requestUrl = `https://graph.microsoft.com/v1.0/sites/${ site_id }/drives/${ drive_id }/items/${ item_id }/${ fileName }:/createuploadsession`
// OR
requestUrl = `https://graph.microsoft.com/v1.0/drives/${ drive_id }/items/${ item_id }/${ fileName }:/createuploadsession`
// OR
requestUrl = `https://graph.microsoft.com/v1.0/drives/${ drive_id }/root:/${ folder1 }/${ fileName }:/createuploadsession`
// OR
requestUrl = `https://graph.microsoft.com/v1.0/drives/${ drive_id }/root:/${ folder1 }:/${ fileName }:/createuploadsession`
// OR
requestUrl = `https://graph.microsoft.com/v1.0/drives/${ drive_id }/root:/${ folder1 }/${ folder2 }/${ fileName }:/createuploadsession`
// OR
requestUrl = `https://graph.microsoft.com/v1.0/drives/${ drive_id }/root:/${ folder1 }/${ folder2 }/${ folder3 }/${ fileName }:/createuploadsession`


const uploadSession = await new graph.LargeFileUploadTask.createUploadSession( client, requestUrl, payload );
// here usually results in (node:0) UnhandledPromiseRejectionWarning: Error: Invalid request
const uploadTask = await graph.LargeFileUploadTask( client, fileObject, uploadSession, options );
const uploadResult = await uploadTask.upload();
console.log( uploadResult?.responseBody || uploadResult )

应用堆栈:

  • NodeJS v14.x
  • @azure/msal-node@1.4.0
  • @microsoft/microsoft-graph-client@3.0.1

快速站点和文件夹概览:

项目一:

  1. 我们有许多项目来来去去,因此我们将这些项目的文件和参与这些项目的人员分开。我不会改变我们如何构建项目,因为这是一个长期存在的事情,背后有很多原因。对此主题的 /dev/null 的评论。
  2. 包含一个Documents 库,我们在其中使用 OneDrive for Business 同步文件
  3. 为此创建了一个 Teams 站点,我们在其中创建了创建文件夹的频道,我们可以通过 OneDrive 在 Project 1 - Documents 中访问这些文件夹
  4. 希望将文件上传到此处文件夹结构的任何级别。

项目 2:等

第 1 队:

  1. 与项目相同,但在这里我们进行团队特定的互动。
  2. 我们有很多团队,相当于公司内部的部门和子部门。
  3. 希望将文件上传到此处文件夹结构的任何级别。文件夹的 ID 很可能是长期的,但如果有人删除它们,我们可能应该根据路径找到文件夹的 ID。

第 2 队:等等

这是我们从 OneDrive 中看到的:

在团队中:

【问题讨论】:

  • 我设法通过使用第二个 requestUrl 并在 item_id 后添加一个冒号并将await graph.LargeFileUploadTask 更改为new graph.LargeFileUploadTask 来使其工作。后者是通过从 microsoft-graph-client 库中挖掘 LargeFileUploadtask.js 来理解代码来确定的

标签: node.js sharepoint microsoft-graph-api onedrive microsoft-graph-files


【解决方案1】:

我必须在第二个 requestUrl 格式的 folder_id 之后添加一个冒号。 await graph.LargeFileUploadTask 也必须变成 new graph.LargeFileUploadTask

const msal = require('@azure/msal-node');
const graph = require( '@microsoft/microsoft-graph-client' );
// all that client initialization and authentication work so I omited it for this example

const fileName = 'fileName.txt';
const filePath = 'path/to/file/' + fileName;
const stats = fs.statSync( filePath );
const totalSize = stats.size;
const readStream = fs.createReadStream( filePath );
const fileObject = new graph.StreamUpload( readStream, fileName, totalSize );

const site_id = '' // refers to the identifier for the SharePoint site, something like domain,guid,guid
const drive_id = '' // refers to the site specific drive that would sync to the Corp OneDrive folder
const folder_id = '' // refers to the ID for a target folder

requestUrl = `https://graph.microsoft.com/v1.0/drives/${ drive_id }/items/${ folder_id }:/${ fileName }:/createuploadsession`


const uploadSession = await new graph.LargeFileUploadTask.createUploadSession( client, requestUrl, payload );
// here usually results in (node:0) UnhandledPromiseRejectionWarning: Error: Invalid request
const uploadTask = new graph.LargeFileUploadTask( client, fileObject, uploadSession, options );
const uploadResult = await uploadTask.upload();
console.log( uploadResult?.responseBody || uploadResult )

【讨论】:

    猜你喜欢
    • 2022-06-29
    • 2016-01-06
    • 2023-04-01
    • 2018-05-12
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    相关资源
    最近更新 更多