从共享 Dropbox 文件夹下载单个文件,无需该文件的共享链接

发布于 2025-01-10 08:03:57 字数 1386 浏览 0 评论 0 原文

正如标题所示,我可以访问一个共享文件夹,其中上传了一些文件。我只想下载一个名为“db.dta”的特定文件。所以,我有这个脚本:

def download_file(url, filename):
    url = url
    file_name = filename
    with open(file_name, "wb") as f:
        print("Downloading %s" % file_name)
        response = requests.get(url, stream=True)
        total_length = response.headers.get('content-length')

        if total_length is None: # no content length header
            f.write(response.content)
        else:
            dl = 0
            total_length = int(total_length)
            for data in response.iter_content(chunk_size=4096):
                dl += len(data)
                f.write(data)
                done = int(50 * dl / total_length)
                sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)) )    
                sys.stdout.flush()
    print(" ")
    print('Descarga existosa.')

如果我将 dl=0 修改为 1,它实际上会下载文件的共享链接,如下所示:

https://www.dropbox.com/s/ajklhfalsdfl/db_test.dta?dl=1

问题是,我在此共享文件夹中没有此特定文件的共享链接,所以如果我使用文件预览的 url,我收到拒绝访问的错误(即使我将 dl=0 更改为 1)。

https://www.dropbox.com/sh/a630ksuyrtw33yo/LKExc-MKDKIIWJMLKFJ?dl=1&preview=db.dta

给出的错误:

dropbox.exceptions.ApiError: ApiError('22eaf5ee05614d2d9726b948f59a9ec7', GetSharedLinkFileError('shared_link_access_denied', None))

有办法下载这个文件吗?

As the title says, I have access to a shared folder where some files are uploaded. I just want to donwload an specific file, called "db.dta". So, I have this script:

def download_file(url, filename):
    url = url
    file_name = filename
    with open(file_name, "wb") as f:
        print("Downloading %s" % file_name)
        response = requests.get(url, stream=True)
        total_length = response.headers.get('content-length')

        if total_length is None: # no content length header
            f.write(response.content)
        else:
            dl = 0
            total_length = int(total_length)
            for data in response.iter_content(chunk_size=4096):
                dl += len(data)
                f.write(data)
                done = int(50 * dl / total_length)
                sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)) )    
                sys.stdout.flush()
    print(" ")
    print('Descarga existosa.')

It actually download shares links of files if I modify the dl=0 to 1, like this:

https://www.dropbox.com/s/ajklhfalsdfl/db_test.dta?dl=1

The thing is, I dont have the share link of this particular file in this shared folder, so if I use the url of the file preview, I get an error of denied access (even if I change dl=0 to 1).

https://www.dropbox.com/sh/a630ksuyrtw33yo/LKExc-MKDKIIWJMLKFJ?dl=1&preview=db.dta

Error given:

dropbox.exceptions.ApiError: ApiError('22eaf5ee05614d2d9726b948f59a9ec7', GetSharedLinkFileError('shared_link_access_denied', None))

Is there a way to download this file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澜川若宁 2025-01-17 08:03:57

如果您有父文件夹的共享链接,而不是所需的特定文件,则可以使用 /2/sharing/get_shared_link_file 端点 仅下载特定文件。

Dropbox API v2 Python SDK 中,即 sharing_get_shared_link_file 方法(或sharing_get_shared_link_file_to_file)。根据您共享的错误输出,您似乎已经在使用它(尽管不在您发布的特定代码片段中)。

使用它看起来像这样:(

import dropbox

dbx = dropbox.Dropbox(ACCESS_TOKEN)

folder_shared_link = "https://www.dropbox.com/sh/a630ksuyrtw33yo/LKExc-MKDKIIWJMLKFJ"
file_relative_path = "/db.dat"

res = dbx.sharing_get_shared_link_file(url=folder_shared_link, path=file_relative_path)

print("Metadata: %s" % res[0])
print("File data: %s bytes" % len(res[1].content))

您在问题中提到了“db.dat”和“db.dta”。确保您使用实际上正确的那个。)

此外,请注意您是否使用通过“应用程序文件夹”访问类型:目前存在一个错误,可以造成这个将此方法与应用文件夹应用的访问令牌一起使用时,shared_link_access_denied 错误。


更新:当对具有应用程序文件夹访问类型的应用程序使用访问令牌时,/2/sharing/get_shared_link_metadata 和 /2/sharing/get_shared_link_file 端点(以及 SDK 中的相应方法)现在应该可以工作。

If you have the shared link to the parent folder and not the specific file you want, you can use the /2/sharing/get_shared_link_file endpoint to download just the specific file.

In the Dropbox API v2 Python SDK, that's the sharing_get_shared_link_file method (or sharing_get_shared_link_file_to_file). Based on the error output you shared, it looks like you are already using that (though not in the particular code snippet you posted).

Using that would look like this:

import dropbox

dbx = dropbox.Dropbox(ACCESS_TOKEN)

folder_shared_link = "https://www.dropbox.com/sh/a630ksuyrtw33yo/LKExc-MKDKIIWJMLKFJ"
file_relative_path = "/db.dat"

res = dbx.sharing_get_shared_link_file(url=folder_shared_link, path=file_relative_path)

print("Metadata: %s" % res[0])
print("File data: %s bytes" % len(res[1].content))

(You mentioned both "db.dat" and "db.dta" in your question. Make sure you use whichever is actually correct.)

Additionally, note if you using a Dropbox API app registered with the "app folder" access type: there's currently a bug that can cause this shared_link_access_denied error when using this method with an access token for an app folder app.


Update: the /2/sharing/get_shared_link_metadata and /2/sharing/get_shared_link_file endpoints (and corresponding methods in the SDKs) should now work when using an access token for an app with the app folder access type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文