本文介绍如何在使用 Microsoft Graph API 时处理长时间运行的操作。 某些 API 响应需要不确定的时间才能完成。 Microsoft Graph 可能会使用长时间运行的操作模式,而不是在返回响应之前等待操作完成。 此模式为应用提供了一种轮询长时间运行的操作的状态更新的方法,而无需等待操作完成的任何请求。
常规模式涉及以下步骤:
- 应用通过 API 请求长时间运行的操作。 API 接受操作并返回
202 Accepted
响应以及 Location
API URL 的标头,以检索操作状态报告。
- 你的应用请求操作状态报告 URL,并接收 asyncJobStatus 响应,其中包含长时间运行的操作的进度。
- 长时间运行的操作完成。
- 你的应用再次请求操作状态报告 URL,并收到一个 异步JobStatus 响应,显示操作完成情况。
先决条件
执行长时间运行的操作所需的相同 权限 也需要查询长时间运行的操作的状态。
初始操作请求
以下示例使用 driveitem: copy 方法。
在此方案中,应用发出复制包含大量数据的文件夹的请求。
此请求可能需要几秒钟才能完成,因为数据量很大。
POST https://graph.microsoft.com/beta/me/drive/items/{folder-item-id}/copy
Content-Type: application/json
{
"parentReference": {
"path": "/drive/root:/Documents"
},
"name": "Copy of LargeFolder1"
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Beta.Drives.Item.Items.Item.Copy;
using Microsoft.Graph.Beta.Models;
var requestBody = new CopyPostRequestBody
{
ParentReference = new ItemReference
{
Path = "/drive/root:/Documents",
},
Name = "Copy of LargeFolder1",
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Drives["{drive-id}"].Items["{driveItem-id}"].Copy.PostAsync(requestBody);
请阅读 SDK 文档,了解如何将 SDK 添加到项目并创建 authProvider 实例的详细信息。
mgc-beta drives items copy post --drive-id {drive-id} --drive-item-id {driveItem-id} --body '{\
"parentReference": {\
"path": "/drive/root:/Documents"\
},\
"name": "Copy of LargeFolder1"\
}\
'
请阅读 SDK 文档,了解如何将 SDK 添加到项目并创建 authProvider 实例的详细信息。
// Code snippets are only available for the latest major version. Current major version is $v0.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
graphdrives "github.com/microsoftgraph/msgraph-beta-sdk-go/drives"
graphmodels "github.com/microsoftgraph/msgraph-beta-sdk-go/models"
//other-imports
)
requestBody := graphdrives.NewCopyPostRequestBody()
parentReference := graphmodels.NewItemReference()
path := "/drive/root:/Documents"
parentReference.SetPath(&path)
requestBody.SetParentReference(parentReference)
name := "Copy of LargeFolder1"
requestBody.SetName(&name)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
copy, err := graphClient.Drives().ByDriveId("drive-id").Items().ByDriveItemId("driveItem-id").Copy().Post(context.Background(), requestBody, nil)
请阅读 SDK 文档,了解如何将 SDK 添加到项目并创建 authProvider 实例的详细信息。
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.beta.drives.item.items.item.copy.CopyPostRequestBody copyPostRequestBody = new com.microsoft.graph.beta.drives.item.items.item.copy.CopyPostRequestBody();
ItemReference parentReference = new ItemReference();
parentReference.setPath("/drive/root:/Documents");
copyPostRequestBody.setParentReference(parentReference);
copyPostRequestBody.setName("Copy of LargeFolder1");
var result = graphClient.drives().byDriveId("{drive-id}").items().byDriveItemId("{driveItem-id}").copy().post(copyPostRequestBody);
请阅读 SDK 文档,了解如何将 SDK 添加到项目并创建 authProvider 实例的详细信息。
const options = {
authProvider,
};
const client = Client.init(options);
const driveItem = {
parentReference: {
path: '/drive/root:/Documents'
},
name: 'Copy of LargeFolder1'
};
await client.api('/me/drive/items/{folder-item-id}/copy')
.version('beta')
.post(driveItem);
请阅读 SDK 文档,了解如何将 SDK 添加到项目并创建 authProvider 实例的详细信息。
<?php
use Microsoft\Graph\Beta\GraphServiceClient;
use Microsoft\Graph\Beta\Generated\Drives\Item\Items\Item\Copy\CopyPostRequestBody;
use Microsoft\Graph\Beta\Generated\Models\ItemReference;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new CopyPostRequestBody();
$parentReference = new ItemReference();
$parentReference->setPath('/drive/root:/Documents');
$requestBody->setParentReference($parentReference);
$requestBody->setName('Copy of LargeFolder1');
$result = $graphServiceClient->drives()->byDriveId('drive-id')->items()->byDriveItemId('driveItem-id')->copy()->post($requestBody)->wait();
请阅读 SDK 文档,了解如何将 SDK 添加到项目并创建 authProvider 实例的详细信息。
Import-Module Microsoft.Graph.Beta.Files
$params = @{
parentReference = @{
path = "/drive/root:/Documents"
}
name = "Copy of LargeFolder1"
}
Copy-MgBetaDriveItem -DriveId $driveId -DriveItemId $driveItemId -BodyParameter $params
请阅读 SDK 文档,了解如何将 SDK 添加到项目并创建 authProvider 实例的详细信息。
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph_beta import GraphServiceClient
from msgraph_beta.generated.drives.item.items.item.copy.copy_post_request_body import CopyPostRequestBody
from msgraph_beta.generated.models.item_reference import ItemReference
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = CopyPostRequestBody(
parent_reference = ItemReference(
path = "/drive/root:/Documents",
),
name = "Copy of LargeFolder1",
)
result = await graph_client.drives.by_drive_id('drive-id').items.by_drive_item_id('driveItem-id').copy.post(request_body)
请阅读 SDK 文档,了解如何将 SDK 添加到项目并创建 authProvider 实例的详细信息。
API 响应操作已被接受,并提供 URL 来检索长时间运行的操作的状态。
HTTP/1.1 202 Accepted
Location: https://api.onedrive.com/monitor/4A3407B5-88FC-4504-8B21-0AABD3412717
注意: 返回的位置 URL 可能不在 Microsoft Graph API 终结点上。
在许多情况下,此步骤是请求的结束,因为复制操作完成时无需应用执行任何其他工作。
但是,如果你的应用需要显示复制操作的状态或确保它完成且没有错误,它可以使用监视 URL 来执行此操作。
通过监视器 URL 检索状态报告
为了检查复制操作的状态,应用会请求获取上一响应中返回的 URL。
注意: 此请求不需要身份验证,因为 URL 生存期较短,对原始调用方是唯一的。
GET https://api.onedrive.com/monitor/4A3407B5-88FC-4504-8B21-0AABD3412717
服务会做出响应,并显示长时间运行的操作仍在进行中。
HTTP/1.1 202 Accepted
Content-type: application/json
{
"operation": "ItemCopy",
"percentageComplete": 27.8,
"status": "inProgress"
}
此信息可用于向用户提供复制操作的最新进度。
应用可以继续轮询监视器 URL,以请求获取状态更新并跟踪操作进度。
通过监视器 URL 检索已完成状态报告
几秒钟后,复制操作完成。
这一次,当应用向监视器 URL 发出请求时,响应将重定向到操作的完成结果。
GET https://api.onedrive.com/monitor/4A3407B5-88FC-4504-8B21-0AABD3412717
操作完成后,来自监视服务的响应将返回结果的资源 ID。
HTTP/1.1 202 Accepted
Content-type: application/json
{
"percentageComplete": 100.0,
"resourceId": "01MOWKYVJML57KN2ANMBA3JZJS2MBGC7KM",
"status": "completed"
}
检索已完成的操作的结果
作业完成后,监视器 URL 返回结果的资源 ID。 在本例中,它是原始项的新副本。
以下示例演示如何使用资源 ID 来寻址此新项。
GET https://graph.microsoft.com/beta/me/drive/items/{item-id}
// Code snippets are only available for the latest version. Current version is 5.x
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Drives["{drive-id}"].Items["{driveItem-id}"].GetAsync();
请阅读 SDK 文档,了解如何将 SDK 添加到项目并创建 authProvider 实例的详细信息。
// Code snippets are only available for the latest major version. Current major version is $v0.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
//other-imports
)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
items, err := graphClient.Drives().ByDriveId("drive-id").Items().ByDriveItemId("driveItem-id").Get(context.Background(), nil)
请阅读 SDK 文档,了解如何将 SDK 添加到项目并创建 authProvider 实例的详细信息。
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
DriveItem result = graphClient.drives().byDriveId("{drive-id}").items().byDriveItemId("{driveItem-id}").get();
请阅读 SDK 文档,了解如何将 SDK 添加到项目并创建 authProvider 实例的详细信息。
const options = {
authProvider,
};
const client = Client.init(options);
let driveItem = await client.api('/me/drive/items/{item-id}')
.version('beta')
.get();
请阅读 SDK 文档,了解如何将 SDK 添加到项目并创建 authProvider 实例的详细信息。
<?php
use Microsoft\Graph\Beta\GraphServiceClient;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$result = $graphServiceClient->drives()->byDriveId('drive-id')->items()->byDriveItemId('driveItem-id')->get()->wait();
请阅读 SDK 文档,了解如何将 SDK 添加到项目并创建 authProvider 实例的详细信息。
Import-Module Microsoft.Graph.Beta.Files
Get-MgBetaDriveItem -DriveId $driveId -DriveItemId $driveItemId
请阅读 SDK 文档,了解如何将 SDK 添加到项目并创建 authProvider 实例的详细信息。
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph_beta import GraphServiceClient
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
result = await graph_client.drives.by_drive_id('drive-id').items.by_drive_item_id('driveItem-id').get()
请阅读 SDK 文档,了解如何将 SDK 添加到项目并创建 authProvider 实例的详细信息。
HTTP/1.1 200 OK
Content-type: application/json
{
"id": "",
"name": "Copy of LargeFolder1",
"folder": { },
"size": 12019
}
支持的资源
以下方法支持长时间运行的操作。
Resource |
API |
driveItem |
复制 |