7

Dropbox rest api,在 metatada 函数中有一个名为“hash”的参数https://www.dropbox.com/developers/reference/api#metadata

我可以在本地计算此哈希而不调用任何远程 api 休息函数吗?

我需要知道这个值来减少上传带宽。

4

5 回答 5

5

元数据调用上的“哈希”参数实际上不是文件的哈希,而是元数据的哈希。其目的是避免在元数据请求期间通过提供元数据而无需重新下载请求中的元数据。它不打算用作文件哈希。

不幸的是,我看不到任何通过 Dropbox API 获取文件本身哈希的方法。我认为减少上传带宽的最佳选择是在本地跟踪文件的哈希值,并在确定是否上传文件时检测它们是否发生了变化。根据您的系统,您可能还希望跟踪元数据请求中返回的“rev”(修订)值,以便您可以判断 Dropbox 本身的版本是否已更改。

于 2012-10-22T18:09:07.200 回答
5

https://www.dropbox.com/developers/reference/content-hash解释了 Dropbox 如何计算他们的文件哈希。下面是一个 Python 实现:

import hashlib
import math
import os

DROPBOX_HASH_CHUNK_SIZE = 4*1024*1024

def compute_dropbox_hash(filename):
    file_size = os.stat(filename).st_size
    with open(filename, 'rb') as f:
        block_hashes = b''
        while True:
            chunk = f.read(DROPBOX_HASH_CHUNK_SIZE)
            if not chunk:
                break
            block_hashes += hashlib.sha256(chunk).digest()
        return hashlib.sha256(block_hashes).hexdigest()
于 2017-10-03T22:42:26.120 回答
1

这不会直接回答您的问题,而是更多地作为一种解决方法;dropbox sdk 提供了一个简单的 updown.py 示例,该示例使用文件大小和修改时间来检查文件的流通性。

取自updown.py的缩写示例:

dbx = dropbox.Dropbox(api_token)
...
# returns a dictionary of name: FileMetaData
listing = list_folder(dbx, folder, subfolder)
# name is the name of the file
md = listing[name]
# fullname is the path of the local file
mtime = os.path.getmtime(fullname)
mtime_dt = datetime.datetime(*time.gmtime(mtime)[:6])
size = os.path.getsize(fullname)
if (isinstance(md, dropbox.files.FileMetadata) and mtime_dt == md.client_modified and size == md.size):
    print(name, 'is already synced [stats match]')
于 2017-05-04T16:42:29.187 回答
0

就我而言,不,你不能。唯一的方法是使用这里解释的 Dropbox API 。

于 2017-07-03T05:45:51.773 回答
0

来自https://rclone.org的 rclone go 程序正是您想要的:

rclone hashsum dropbox localfile

rclone hashsum dropbox localdir

它不能超过一个路径参数,但我怀疑这是你可以使用的东西......

t0|todd@tlaptop/p8 ~/tmp|295$ echo "Hello, World!" > dropbox-hash-demo/hello.txt
t0|todd@tlaptop/p8 ~/tmp|296$ rclone copy dropbox-hash-demo/hello.txt dropbox-ttf:demo
t0|todd@tlaptop/p8 ~/tmp|297$ rclone hashsum dropbox dropbox-hash-demo 
aa4aeabf82d0f32ed81807b2ddbb48e6d3bf58c7598a835651895e5ecb282e77  hello.txt
t0|todd@tlaptop/p8 ~/tmp|298$ rclone hashsum dropbox dropbox-ttf:demo
aa4aeabf82d0f32ed81807b2ddbb48e6d3bf58c7598a835651895e5ecb282e77  hello.txt
于 2021-10-27T19:17:31.500 回答