ubuntu 利用阿里网盘API实现文件的上传和下载

文章目录

背景

最近在用ubuntu系统做实验,而ubuntu 系统的文件上传和下载操作很麻烦;

于是便打算使用阿里网盘的API 进行文件下载与上传;

脚本

初始化

阿里云盘API工具 aligo

点击查看Aligo github

方便在linux服务器上使用,只需要填入一个刷新的token就可以了。

安装aligo

shell 复制代码
pip install aligo

aligo教程

请读者自行浏览

实战

chrome抓包 获得refresh_token

python 复制代码
import aligo
from aligo import Aligo
refresh_token = "抓包拿到的refresh_token"
ali = Aligo(refresh_token=refresh_token)

查看你的用户信息,就知道是否登录成功了

python 复制代码
print(ali.get_user())

parse.py

python 复制代码
import argparse

import aligo
from aligo import Aligo

ali = Aligo()


def down_file_or_folder(remote_path, local_folder, is_file=False):
    file = (
        ali.get_file_by_path(remote_path)
        if is_file
        else ali.get_folder_by_path(remote_path)
    )

    if is_file:
        ali.download_file(file_id=file.file_id, local_folder=local_folder)
    else:
        ali.download_folder(folder_file_id=file.file_id, local_folder=local_folder)


def upload_file_or_folder(local_file_folder, remote_folder, is_file=False):
    remote_folder_id = ali.get_folder_by_path(remote_folder).file_id

    if is_file:
        ali.upload_file(file_path=local_file_folder, parent_file_id=remote_folder_id)
    else:
        ali.upload_folder(
            folder_path=local_file_folder, parent_file_id=remote_folder_id
        )


def main():
    parser = argparse.ArgumentParser(
        description="Download file or folder from Aliyun Drive. 默认下载 & 文件夹"
    )
    parser.add_argument("-up", "--is_up", action="store_true", help="默认是下载模式")
    parser.add_argument(
        "-f", "--is_file", action="store_true", help="默认是上传和下载文件夹"
    )

    parser.add_argument(
        "-r",
        "--remote",
        action="store",
        required=True,
        metavar="REMOTE_FOLDER_PATH",
        help="specify the remote file or folder path to download or upload.",
    )
    parser.add_argument(
        "-l",
        "--local",
        action="store",
        required=True,
        metavar="LOCAL_FOLDER_PATH",
        help="specify the local file or folder path to download or upload.",
    )
    args = parser.parse_args()

    print(args.__dict__)

    is_upload, is_file, remote, local = (
        args.is_up,
        args.is_file,
        args.remote,
        args.local,
    )

    # 上传
    if is_upload:
        print("上传...")
        upload_file_or_folder(
            local_file_folder=local, remote_folder=remote, is_file=is_file
        )
    else:
        print("下载...")
        down_file_or_folder(remote_path=remote, local_folder=local, is_file=is_file)


main()
bash 复制代码
Download file or folder from Aliyun Drive. 默认下载 & 文件夹

options:
  -h, --help            show this help message and exit
  -up, --is_up          不填是下载,填是上传
  -f, --is_file         不填默认是上传和下载文件夹,填了上传和下载文件
  -r REMOTE_FOLDER_PATH, --remote REMOTE_FOLDER_PATH
                        specify the remote file or folder path to download or
                        upload.
  -l LOCAL_FOLDER_PATH, --local LOCAL_FOLDER_PATH
                        specify the local file or folder path to download or
                        upload.

演示

在upload_test文件夹下,有up.txt文件;

在阿里云盘,创建 tmp 文件夹,再在其下创建 parse文件夹

上传文件

bash 复制代码
python parse.py -f -up -r tmp/parse -l upload_test/up.txt

上述脚本实现将 本地 upload_test/up.txt 上传到阿里网盘的 tmp/parse 文件夹下;

如下图所示,文件上传成功:

上传文件夹

bash 复制代码
python parse.py -up -r tmp/parse -l upload_test

上述脚本实现,将 本地 upload_test 文件夹上传到阿里网盘的 tmp/parse 文件夹下;

如下图所示,文件夹上传成功:

下载文件

bash 复制代码
python parse.py -f -r tmp/parse/up.txt -l down_test

上述脚本实现,将 阿里网盘的 tmp/parse/up.txt文件下载到本地 down_test 文件下;

如下图所示,up.txt 文件下载成功:

下载文件夹

bash 复制代码
python parse.py -r tmp/parse/upload_test -l down_test
相关推荐
YYRAN_ZZU10 分钟前
Petalinux新建自动脚本启动
linux
charlie11451419126 分钟前
嵌入式Linux驱动开发pinctrl篇(1)——从寄存器到子系统:驱动演进之路
linux·运维·驱动开发
Agent手记32 分钟前
异常考勤智能预警与处理与流程优化方案 | 基于企业级Agent的超自动化实战教程
运维·人工智能·ai·自动化
于小猿Sup44 分钟前
VMware在Ubuntu22.04驱动Livox Mid360s
linux·c++·嵌入式硬件·自动驾驶
cen__y1 小时前
Linux12(Git01)
linux·运维·服务器·c语言·开发语言·git
不仙5202 小时前
VMware Workstation 26.0.0 在 Ubuntu 24.04 (内核 6.17.0) 上的安装与内核模块编译问题
linux·ubuntu·elasticsearch
AI视觉网奇3 小时前
linux 检索库 判断库是否支持
java·linux·服务器
dapeng-大鹏3 小时前
KVM+LVM 零停机在线扩容 Ubuntu 根分区:从磁盘添加到逻辑卷扩展完整
linux·运维·ubuntu·磁盘空间扩展
乐维_lwops3 小时前
案例解读|运维监控助力某大型卷烟厂构建高效运维监控体系
运维·运维案例
JiaWen技术圈3 小时前
网站用户注册行为验证码方案
运维·安全