缩略图对象有 url,但没有内容

Thumbnail object has url, but no content

使用 Microsoft Graph C# SDK v1.13。我能够为我的 DriveItem 检索 IDriveItemThumbnailsCollectionPage。通过该页面,我可以获得第一个 ThumbnailSetThumbnailSet 有小型、中型和大型 Thumbnail 对象。

每个 Thumbnail 对象似乎都有一个有效的缩略图 URL。但是,.Content (Stream) 属性中的 none 是有效的...它们都是 NULL.

例如:

IDriveItemThumbnailsCollectionPage page = await client
    .Me
    .Drive
    .Items[this.Id]
    .Thumbnails
    .Request()
    .GetAsync();

if ((page != null) && (page.CurrentPage != null) && (page.CurrentPage.Count > 0)) {
    ThumbnailSet thumbs = page.CurrentPage.FirstOrDefault();
    if (thumbs != null) {
        if ((minHeight != null) && (minHeight.HasValue) && (minWidth != null) && (minWidth.HasValue)) {
            if ((minHeight > 48) || (minWidth > 48)) {
                Thumbnail thumb = thumbs.Medium;
                thumbStream = thumb?.Content;
            }
        }

        if (thumbStream == null)
            thumbStream = thumbs?.Small?.Content;
    }
}

如何使用 SDK 获取这些缩略图的内容流之一?

使用 VS 调试器查看返回的变量,"thumbs" 填充了大小和 URL,但尝试检索内容始终为 NULL。

由于 Thumbnail.Url property 用于存储 URL 以获取缩略图 内容,下载缩略图内容的最简单选项可能是这样的:

var page = await graphClient.Me.Drive.Items[itemId].Thumbnails.Request().GetAsync();
var thumbnailSet = page.FirstOrDefault();
var thumbnail = thumbnailSet?.Medium;
using (var client = new System.Net.WebClient())
{
    var content = client.DownloadData(thumbnail.Url);  //download it as a byte array

    System.IO.File.WriteAllBytes(targetFileName, content); //save into file
}

选项 2

另一种选择是利用以下端点 (from docs):

GET /me/drive/items/{item-id}/thumbnails/{thumb-id}/{size}/content

这是 msgraph-sdk-dotnet 的示例:

//1.get thumbnail metadata (similar to previous example)
var requestUrl = $"{graphClient.BaseUrl}/me/drive/items/{itemId}/thumbnails/{thumbnailId}/{thumbnailSize}";
var message = new HttpRequestMessage(HttpMethod.Get, requestUrl);
await graphClient.AuthenticationProvider.AuthenticateRequestAsync(message);
var response = await graphClient.HttpProvider.SendAsync(message);
var content = await response.Content.ReadAsStringAsync();
var thumbnail = JsonConvert.DeserializeObject<Thumbnail>(content);

//2.get thumbnail content
requestUrl = $"{graphClient.BaseUrl}/me/drive/items/{itemId}/thumbnails/{thumbnailId}/{thumbnailSize}/content";
message = new HttpRequestMessage(HttpMethod.Get, requestUrl);
await graphClient.AuthenticationProvider.AuthenticateRequestAsync(message);
response = await graphClient.HttpProvider.SendAsync(message);
var bytesContent = await response.Content.ReadAsByteArrayAsync();