alex-phillips / clouddrive
Amazon CloudDrive 的 PHP SDK 和 CLI
Requires
- php: >=5.5
- alex-phillips/utilities: ~0.1
- cilex/cilex: ^1.1
- guzzlehttp/guzzle: ^6.0
- j4mie/idiorm: ^1.5
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-14 17:42:54 UTC
README
注意:此项目正在积极开发中。
CloudDrive-PHP 是一个用于与 Amazon 的 Cloud Drive 交互的 SDK 和 CLI 应用程序。
该项目最初是一个用于通过命令行管理 Cloud Drive 存储的应用程序,后来考虑到其他人可能希望利用 API 调用并使用它来开发自己的软件,因此我确保构建的库可以扩展,并将 CLI 应用程序作为一个包含的工具,该工具也可以用作实现的示例。
安装
通过 Composer(用于项目内使用)
$ composer require alex-phillips/clouddrive
全局安装以从任何位置运行 CLI(只要全局 composer bin
目录在您的 $PATH
中)。
$ composer global require alex-phillips/clouddrive
CLI
设置
CLI 的首次运行需要使用您的 Amazon Cloud Drive 凭证通过应用程序验证您的 Amazon Cloud Drive 账户。使用 config
命令设置这些凭证以及与您的 Amazon 账户关联的电子邮件。
$ clouddrive config email me@example.com
$ clouddrive config client-id CLIENT_ID
$ clouddrive config client-secret CLIENT_SECRET
设置凭证后,只需运行 init
命令。这将为您提供一个要访问的验证 URL。将重定向到的 URL 粘贴到终端并按回车键。
$ clouddrive init
Initial authorization required.
Navigate to the following URL and paste in the redirect URL here.
https://www.amazon.com/ap/oa?client_id=CLIENT_ID&scope=clouddrive%3Aread_all%20clouddrive%3Awrite&response_type=code&redirect_uri=https://
...
Successfully authenticated with Amazon CloudDrive.
认证后,运行 sync
命令以同步您的本地缓存与 Cloud Drive 的当前状态。
用法
CLI 应用程序依赖于您的本地缓存与 Cloud Drive 保持同步,因此请定期运行 sync
命令以检索自上次同步以来的任何更改。您可以在任何时候使用 -h
标志查看所有可用命令以及每个命令的用法。
Cloud Drive version 0.1.0
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
cat Output a file to the standard output stream
clear-cache Clear the local cache
clearcache Clear the local cache
config Read, write, and remove config options
download Download remote file or folder to specified local path
du Display disk usage for the given node
find Find nodes by name or MD5 checksum
help Displays help for a command
init Initialize the command line application for use with an Amazon account
link Generate a temporary, pre-authenticated download link
list Lists commands
ls List all remote nodes inside of a specified directory
metadata Retrieve the metadata (JSON) of a node by its path
mkdir Create a new remote directory given a path
mv Move a node to a new remote folder
pending List the nodes that have a status of 'PENDING'
quota Show Cloud Drive quota
rename Rename remote node
renew Renew authorization
resolve Return a node's remote path by its ID
restore Restore a remote node from the trash
rm Move a remote Node to the trash
sync Sync the local cache with Amazon CloudDrive
trash List the nodes that are in trash
tree Print directory tree of the given node
upload Upload local file or folder to remote directory
usage Show Cloud Drive usage
SDK
SDK 响应
所有方法调用都返回一个类似 REST API 的结构作为响应,但返回 Node
对象的方法除外。
示例响应
[ 'result' => true, 'data' => [ 'message' => 'The response was successful' ] ]
每个 API 类型的响应至少包含 2 个键:success
和 data
。 Success
是一个布尔值,表示请求是否成功完成,而数据包含与请求相关的各种信息。
** 注意:** nodeExists
的响应将返回 success = true
如果节点存在,如果节点不存在则返回 false
。
返回 Node
对象的各种方法调用,如 findNodeByPath
和 findNodeById
,将返回一个 Node
对象或 null
,如果找不到节点。
入门
首先,使用您想要与之通信的账户的电子邮件地址和必要的 Cloud Drive API 凭证创建一个新的 CloudDrive
对象,以及用于存储本地数据的 Cache
对象。(目前只有 SQLite 缓存存储)。
$clouddrive = new CloudDrive\CloudDrive($email, $clientId, $clientSecret, new \CloudDrive\Cache\SQLite($email)); $response = $clouddrive->getAccount()->authorize();
首次尝试授权账户时,response
将“失败”,并且响应中的 data
键将包含一个 auth_url
。这是在初始授权期间授予您的应用程序访问 Cloud Drive 所必需的。只需导航到该 URL,您将被重定向到一个“localhost” URL,其中包含访问所需的代码。
接下来,再次使用传递的跳转URL调用授权
$clouddrive = new CloudDrive\CloudDrive($email, $clientId, $clientSecret, new \CloudDrive\Cache\SQLite($email)); $response = $clouddrive->getAccount()->authorize($redirectUrl);
现在响应将成功,访问令牌将被存储在缓存中。从现在开始,当账户需要更新授权时,它将自动使用其“刷新令牌”在authorize
方法内进行。
本地缓存
目前支持MySQL(以及MariaDB)和SQLite3用于本地缓存存储。只需使用必要的参数实例化这些即可。如果您使用MySQL,请确保数据库已创建。缓存存储的初始化将自动创建所需的表。
$cacheStore = new \CloudDrive\Cache\SQLite('my-cache', './.cache');
节点
一旦您已验证Account
对象并创建本地缓存,初始化Node
对象以利用这些。
Node::init($account, $cache);
现在所有静态的Node
方法都可用于检索、查找和操作Node
对象。
$results = Node::loadByName('myfile.txt');
各种Node
方法将返回一个数组,如果可以返回多个节点,或者如果只返回1个节点(例如,通过ID查找),则返回一个Node
对象。如果没有找到节点,则方法将分别返回空数组或null
值。
贡献
有关详细信息,请参阅CONTRIBUTING。
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。