ge.tt文件分享服务CLI及API的问题

关于 ge.tt

最近我一直在寻找支持直链形式的文件分享服务:

  • 支持通过 cURL 或者公开 API 上传文件生成分享地址;
  • 用户可以通过 wget、cURL 等程序直接下载文件;
  • 上传者后续可以更新文件并保持分享地址不变;
  • 国内的普通用户可以正常访问下载分享的文件。

如果使用 Dropbox、Google Drive 应该也可以实现前面几个需求,但这两个网盘的最大问题在于国内难以描述的墙,而且上传下载还是稍微有点复杂。

以前我使用过 DriveHQ 服务,免费用户支持通过 FTP 方式上传文件(付费用户还支持通过 WebDAV 上传),上传到 Web 分享目录的文件可以直接通过 HTTP 下载,这样也算能达到我的要求了,只是自动处理 FTP 上传覆盖要稍微麻烦点。

目前我使用的是 ge.tt 文件分享服务,相对 DriveHQ 来说其优势在于 ge.tt 开放了 API,而且也有第三方写的 Python / Perl / Java 等各种形式的文件管理工具。虽然 ge.tt 免费帐户只有 2GB 的存储空间,但仍然已经有 500 多万的用户分享了近 5000 万份文件,拿来分享一些小文件或者程序也是很方便的了。

ge.tt 提供了 REST 和 Live API,重点可以关注其 REST API(不过 ge.tt 官网的 API 说明文档暂时无法访问):

http://ge.tt/developers/overview

ge.tt 推荐在客户端上使用 gett-cli 工具来管理文件和分享,同样可以参考其官网说明:

http://ge.tt/tools

gett-cli 的问题

gett-cli 工具基于 Python 3 实现,其 Bitbucket 项目主页为:

https://bitbucket.org/mickael9/gett-cli/overview

查看项目说明显示该工具看起来也很简单,安装之后通过 gett 命令就可以管理了。gett 命令默认提供了上传文件、列表分享、删除分享、删除分享中的文件(一个分享地址支持包含多个文件)、搜索分享或文件的功能。

只是 gett-cli 在一开始登录的时候就碰到了问题,输入用户和密码之后直接登录失败:

(trusty)zzm@localhost:~/Downloads$ gett
Please enter your Ge.tt email: xxx@gmail.com
Please enter your Ge.tt password: 
Traceback (most recent call last):
  File "/usr/local/bin/gett", line 11, in <module>
    load_entry_point('gett-cli==0.2.3', 'console_scripts', 'gett')()
  File "/usr/local/lib/python3.4/dist-packages/gett_cli-0.2.3-py3.4.egg/gett/uploader.py", line 96, in entry_point
  File "/usr/local/lib/python3.4/dist-packages/gett_cli-0.2.3-py3.4.egg/gett/uploader.py", line 198, in main
  File "/usr/local/lib/python3.4/dist-packages/gett_cli-0.2.3-py3.4.egg/gett/gett.py", line 122, in login_auth
  File "/usr/local/lib/python3.4/dist-packages/gett_cli-0.2.3-py3.4.egg/gett/gett.py", line 103, in _load
KeyError: 'accesstoken'

我在参考 ge.tt 的 REST API 说明之后使用 cURL 模拟登录请求,ge.tt 服务器也会返回 Wrong credentials 错误:

(trusty)zzm@localhost:~/Downloads$ curl -i -X POST --data '{"email":"xxx@gmail.com","password":"test-pass"}' http://
open.ge.tt/1/users/login
HTTP/1.1 403 Forbidden
Content-Type: application/json; charset=utf-8
Date: Wed, 05 Apr 2017 18:11:11 GMT
ETag: W/"71-xxx+xxx"
set-cookie: sails.sid=s%3Axxx-xxx%2FkOnG%2Bxxx%2Bxxx; Path=/; HttpOnly
Vary: X-HTTP-Method-Override, Accept-Encoding
X-Powered-By: Sails <sailsjs.org>
Content-Length: 113
Connection: keep-alive

{"message":"Wrong credentials","body":{"err":"Missing credentials"},"login":0,"reason":{"error":"access denied"}}

经过调试我才发现 ge.tt 的 REST API 请求中必须增加 Content-Type: application/json;charset=UTF-8 头才能正常返回数据。

修改 gett-cli 工具 Python 代码之后,现在 gett 命令可以正常返回用户使用的空间了:

(trusty)zzm@localhost:~/Downloads$ gett
Please enter your Ge.tt email: xxx@gmail.com
Please enter your Ge.tt password: 
Do you wish to store the session token? (y/n): y
Storage used: 232.81 KB out of 2.00 GB (0.0%)

登录成功之后 gett-cli 工具默认会将 Refresh Token 保存在用户主目录的 .gett-token 文件中:

root@ee9055d6b11f:~# cat .gett-token
r.0.user-25YKSbxxxxxxx-..xxxxxxx

这样后面再使用 gett-cli 工具就不用重复输入用户名和密码登录了。

修改 gett-cli

另外我还发现 gett-cli 工具存在由于缺少文件大小参数导致上传的文件不正确等问题,为此我专门修改了 gett-cli 代码以解决登录和上传的问题。由于生成的 gett-cli.patch 补丁文件内容有点长这里就不贴出来了,有需要的话可以从下面的 Pastebin 地址下载:

http://pastebin.com/raw/0siCtqkW

有动手能力的朋友们可以自行检出 Bitbucket 上的 gett-cli 版本库进行修改编译安装。

如果嫌编译麻烦也可以直接修改替换已经安装好的 gett-cli 工具 egg 文件,以我使用的 Ubuntu 系统上的 Python 3.4 为例,大概步骤如下(中间直接使用 patch 命令打补丁):

(trusty)zzm@localhost:~/Downloads$ cp /usr/local/lib/python3.4/dist-packages/gett_cli-0.2.3-py3.4.egg .
(trusty)zzm@localhost:~/Downloads$ sudo unzip gett_cli-0.2.3-py3.4.egg && rm -f gett_cli-0.2.3-py3.4.egg
Archive:  gett_cli-0.2.3-py3.4.egg
  inflating: EGG-INFO/zip-safe
  inflating: EGG-INFO/top_level.txt
  inflating: EGG-INFO/entry_points.txt
  inflating: EGG-INFO/dependency_links.txt
  inflating: EGG-INFO/SOURCES.txt
  inflating: EGG-INFO/PKG-INFO
  inflating: gett/uploader.py
  inflating: gett/gett.py
  inflating: gett/__init__.py
  inflating: gett/__pycache__/uploader.cpython-34.pyc
  inflating: gett/__pycache__/gett.cpython-34.pyc
  inflating: gett/__pycache__/__init__.cpython-34.pyc
(trusty)zzm@localhost:~/Downloads$ sudo patch -p2 < gett-cli.patch
(trusty)zzm@localhost:~/Downloads$ sudo py3compile gett/gett.py gett/uploader.py
(trusty)zzm@localhost:~/Downloads$ sudo zip -r gett_cli-0.2.3-py3.4.egg EGG-INFO gett
  adding: EGG-INFO/ (stored 0%)
  adding: EGG-INFO/PKG-INFO (deflated 26%)
  adding: EGG-INFO/entry_points.txt (deflated 2%)
  adding: EGG-INFO/top_level.txt (stored 0%)
  adding: EGG-INFO/SOURCES.txt (deflated 48%)
  adding: EGG-INFO/dependency_links.txt (stored 0%)
  adding: EGG-INFO/zip-safe (stored 0%)
  adding: gett/ (stored 0%)
  adding: gett/uploader.py (deflated 68%)
  adding: gett/__pycache__/ (stored 0%)
  adding: gett/__pycache__/__init__.cpython-34.pyc (deflated 21%)
  adding: gett/__pycache__/gett.cpython-34.pyc (deflated 57%)
  adding: gett/__pycache__/uploader.cpython-34.pyc (deflated 48%)
  adding: gett/__init__.py (stored 0%)
  adding: gett/gett.py (deflated 71%)
(trusty)zzm@localhost:~/Downloads$ sudo mv gett_cli-0.2.3-py3.4.egg /usr/local/lib/python3.4/dist-packages/gett_cli-0.2.3-py3.4.egg

当然如果你不想在系统中安装 gett-cli 也可以直接下载使用我修改好的 gett 工具,解压缩之后运行其中的 uploader.py 程序即可(需要 Python 3 环境):

https://zohead.com/downloads/gett-cli-0.2.3.tar.gz

使用 gett-cli 工具

登录之后使用 gett-cli 工具上传文件非常简单,默认上传文件时都是创建新的分享,上传多个文件则直接附加多个文件参数:

(trusty)zzm@localhost:~/Downloads$ gett gett-cli-0.2.3.tar.gz
Creating file(s)...
--------------------------------------------------------------------------------
Share: Untitled (1 file(s)) [http://ge.tt/8D95lij2]
--------------------------------------------------------------------------------
 - gett-cli-0.2.3.tar.gz           6.54 KB  http://ge.tt/8D95lij2/v/0  remote

gett-cli-0.2.3.tar.gz  (  1/1) [########################################] 100 %

Storage used: 194.00  B out of 2.00 GB (0.0%)

上面结果中的 http://ge.tt/8D95lij2 就是新创建的文件分享地址,http://ge.tt/8D95lij2/v/0 则是该分享中某个文件的地址(0 是分享下的文件 ID,如果上传多个文件就会有 v/1v/2 之类的文件地址)。

如果需要删除创建的分享可以运行:

(trusty)zzm@localhost:~/Downloads$ gett --delete http://ge.tt/8D95lij2
Deleted share: Untitled [http://ge.tt/8D95lij2]

Storage used: 6.74 KB out of 2.00 GB (0.0%)

用户如果需要下载分享中的文件那就更方便了,不需要 gett-cli 工具,直接一条 cURL 命令下载即可(替换命令中的分享地址和文件 ID):

(trusty)zzm@localhost:~/Downloads$ curl -k -o gett-cli-0.2.3.tar.gz -L -e "http://ge.tt/8D95lij2" "http://api.ge.tt/1/files/8D95lij2/0/blob?download"

后记

ge.tt 文件分享服务还存在一些小问题,比如某个分享中已经上传的文件目前还无法直接以相同的文件 ID 进行覆盖替换(例如上面例子中的 http://ge.tt/8D95lij2/v/0),只能删除文件再上传新文件到该分享,新上传的文件的 ID 只能递增,不能保持原来的文件 ID(例如删除之后新上传的文件 ID 可能是 http://ge.tt/8D95lij2/v/3)。

结合这段时间我的使用感觉来看,ge.tt 用于分享小文件还算比较方便的,很适合一些需要通过工具或命令自动上传下载文件的场合。最后如果文章中有任何问题,还请提出指正,祝大家玩的开心。

ge.tt文件分享服务CLI及API的问题》上的评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*