c# - 从 Dropbox 上的公开共享文件夹下载图像

标签 c# dropbox dropbox-api dropbox-sdk

我收到了来自电子商务公司的共享文件夹链接。该链接是公开的,不会直接与我的 Dropbox 帐户共享。

如何获取图像的 url,将其传递给同一 sdk 的 DownloadAsync 方法或简单地传递给 HttpClient,然后...下载它?

理想情况下,它与我在浏览器中查看共享文件夹时单击图像时获得的链接相同。

https://www.dropbox.com/sh/{folder_hash}/{file_hash_maybe}/{filename}?dl=0

这是我尝试过的:

using Dropbox.Api;
using Dropbox.Api.Files;
...
var accessToken = "abracadabra";
var sharedFolderUrl = "https://www.dropbox.com/sh/{folder_hash}?dl=0";

using (var dbx = new DropboxClient(accessToken))
{
    var sharedLink = new SharedLink(sharedFolderUrl);
    var sharedFiles = await dbx.Files.ListFolderAsync(path: "", sharedLink: sharedLink);

    // sharedFiles - has over 13,000 entries, I use cursor to get them all.
    foreach (var file in sharedFiles.Entries)
    {
        if (file.IsFile)
        {
            // tried this, but:
            // 1. it's crazy to loop through all 
            // 2. link.Response.Url gives me the same url to a shared folder for all items 
            var link = await dbx.Sharing.GetSharedLinkFileAsync(url: sharedFolderUrl, path: "/" + file.Name);
        }
    }        

}

最佳答案

使用 GetSharedLinkFileAsync 方法是以编程方式从共享链接下载文件的正确方法。它实际上提供了元数据(在您提到的代码中的 link.Response 中),以及直接提供文件数据(而不是通过 URL)。

要访问文件数据,您可以使用 IDownloadResponse 下记录的任何 GetContentAs... 方法。由 GetSharedLinkFileAsync 返回。在您的代码中,它看起来类似于:link.GetContentAsStreamAsync()(或您想要的任何一个)。

或者,如果您想从共享链接下载整个文件夹,您可以使用 the URL parameters documented in this help center article 。 (不过,对于非常大的文件夹,这可能会失败。)

关于c# - 从 Dropbox 上的公开共享文件夹下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61621589/

相关文章:

c# - 需要 SearchBar 给出相同的结果,无论其字母是否大写

php - 在 Dropbox API 中获取带有元数据的下载链接

java - 解析 DBXException java 的最佳方法

php - 使用 PHP 将网站 + 数据库上传到 Dropbox

android - DbxRecord 服务器修改日期时间戳或修订

node.js - 从 NodeJS 流式上传到 Dropbox

c# - 稀疏八叉树的高效存储?

c# - 给定每个项目的概率,从列表中选择随机项目

c# - C#中的 session 超时问题

python-3.x - 安装保管箱失败并损坏 pip3