开源云文件存储服务器MyDrive

简介

什么是 myDrive ?

myDrive 是一个开源的、可自托管的云文件存储解决方案,类似于 Google Drive。它允许用户通过现代化的 Web 界面访问、上传和下载文件与文件夹,支持文件分享、媒体库、PWA,并使用 AES-256 对静态文件进行加密。后端使用 Node.jsMongoDB,为你提供一个安全、私有的个人云平台。

主要特点

  • 文件管理:支持文件和文件夹的上传与下载(文件夹会自动打包为 zip)。
  • 媒体支持:内置照片和视频查看器,并能自动生成缩略图。
  • 文件分享:可以为文件生成分享链接。
  • PWA 支持:支持渐进式网络应用,可将应用“安装”到桌面或手机主屏幕,提供接近原生的体验。
  • 安全加密:使用 AES-256 对存储的文件进行加密,并依赖 JWT 进行身份验证。
  • 跨平台:基于 Node.js,支持 Docker 一键部署,可在多种平台上运行。
  • 用户管理:支持电子邮件验证和新账户创建限制。

应用场景

  • 搭建家庭或个人媒体中心,用于存储和随时访问照片、视频。
  • 作为小型团队内部的文件共享和协作平台。
  • 替代商业云存储服务(如 Google Drive, Dropbox),将数据完全掌握在自己手中。
  • 为开发者提供一个可集成、可扩展的私有云存储后端服务。

myDrive 是一个功能强大且灵活的云存储解决方案,适合需要安全文件管理的个人和组织使用。

安装

在群晖上以 Docker 方式安装。

在注册表中搜索 kylehoell ,选择第一个 kylehoell/mydrive,版本选择 latest

本文写作时, latest 版本对应为 sha-bae5dbb

myDrive 由应用本身和 mongo 数据库两个服务组成,因此推荐使用 docker-compose 进行部署

准备工作

docker 文件夹中,创建一个新文件夹 mydrive,并在其中再创建三个子文件夹:data, temp, db

1
2
# 创建主文件夹 mydrive 和子目录
mkdir -p /volume1/docker/mydrive/{data,db,temp}
  • data: 用于存放 myDrive 上传的文件
  • temp: 用于存放临时文件,例如生成视频缩略图时
  • db: 用于存放 MongoDB 数据库文件

配置文件

进入 mydrive 目录,我们需要创建两个核心文件:docker-compose.ymlenv.txt

1
2
3
4
5
6
# 进入 mydrive 目录
cd /volume1/docker/mydrive

# 创建文件
touch docker-compose.yml
touch env.txt

env.txt

这是 myDrive 的环境变量文件,用于配置数据库连接、加密密钥等关键信息。

下面的示例基于官方的 .env.example ,源文件地址:https://github.com/subnub/myDrive/blob/master/.env.example

将以下内容粘贴到 env.txt 文件中,建议根据你的设置进行修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# If you are using Docker, set DOCKER=true
DOCKER=true

# MongoDB URL: Connection string for your MongoDB database
# Note: if using the compose file provided, the connection string should be as follows:
MONGODB_URL=mongodb://username:password@mongo:27017/mydrive?authSource=admin

# Database Type: Choose between "fs" and "s3", this specifies where the files will be stored.
# fs = Filesystem
# s3 = Amazon S3
DB_TYPE=fs

# If using fs,
# File Storage Directory: The directory where the files will be stored. Must be exact path.
# PATH MUST END IN A SLASH
# Example: /home/kyle/mydrive/ (must end in a slash)
FS_DIRECTORY=/data/

# If using s3,
# S3 Data: The S3 bucket and key where the files will be stored.
S3_ID=
S3_KEY=
S3_BUCKET=

# Encryiption Key (optional): The encryption key used to encrypt the files.
# DO NOT LOSE OR FORGET THIS KEY AS ALL DATA WILL BE LOST IF YOU LOSE IT.
# If you do not supply a key, the app will instead prompt you to type one into the terminal when you start the server.
KEY=0b22e0f7122fad4821ffdff5aa5f53526e794eeb9a42b4d4230eb87958fb978d

# Access tokens, refresh, and cookie
# These should be randomly generated in a secure manner.
# If you lose these tokens, all users will be logged out.
# You can also change these if you want to force all users to be logged out.
# Each token should be a different string.
# Example: sa4hQqJwGFLC1LJk59
PASSWORD_ACCESS=3659eeaa78d6382b122905707a05b55cab5c97aaa1e193462788fbfdfb7a6937
PASSWORD_REFRESH=0e73f09bf85a727ffcb05fe765324ab2082a798e682d8cd5075dd305fb9e2ba4
PASSWORD_COOKIE=330b09725e50038439f74d08008bd6f6d41639b65e1854faaca43375751c6055

# Video thumbnails (optional): If you want to enable video thumbnails, configure as so.
# Video thumbnail generation relies on ffmpeg, please ensure you have it installed.
# VIDEO_THUMBNAILS_ENABLED=true
VIDEO_THUMBNAILS_ENABLED=true

# Video thumbnails continued (optional):
# Sometimes generating a video thumbnail will fail with the default method.
# If so you can choose to instead temporarily store the video in a directory, and generate a thumbnail from that.
# WARNING: The file will be temporarily stored in this directory UNENCRYPTED.
# Temp directory example: /Users/kyle/mydrive/temp/ (must end in a slash)
# Temp video thumbnail limit: The maximum size of a video thumbnail in bytes.
# Example: 5000000000
TEMP_DIRECTORY=/temp/
TEMP_VIDEO_THUMBNAIL_LIMIT=5000000000

# Block account creation (optional): If you want to block account creation, configure as so, but after you create an account.
# BLOCK_CREATE_ACCOUNT=true

# Ports (optional): The ports to run the server on.
# HTTP_PORT: Default port is 3000
# HTTPS_PORT: Default port is 8080
HTTP_PORT=
HTTPS_PORT=

# URL (optional): The URL to run the server on.
# Most likely not needed, this changes the ip address/url express listens on.
URL=

# Email verifcation (optional): If you want to enable email verification configure as so.
# EMAIL_VERIFICATION=true
# Remote URL: This refers to the URL sent in the verification email: Example https://mydrive-storage.com
# Please navigate to the following link to verify your email address: {REMOTE_URL}/verify
# Should NOT end with a slash
EMAIL_VERIFICATION=
EMAIL_DOMAIN=
EMAIL_ADDRESS=
EMAIL_API_KEY=
EMAIL_HOST=
REMOTE_URL=

# Marks cookie generation as secure (Optional)
# This is recommended and should be enabled if you are running the app on HTTPS.
# SECURE_COOKIES=true
SECURE_COOKIES=


# SSL (Optional): If you want to enable SSL, configure as so.
# SSL=true
# Place your SSL certificate files in the root directory of the project
# With the names: certificate.crt, certificate.key, and certificate.ca-bundle;
SSL=

# HTTPS cert paths (optional): If you need to change the paths of the https certs
# You can do so with these env variables.
# By default myDrive looks for certificate.crt, certificate.ca-bundle and certificate.key on the root of the project
HTTPS_KEY_PATH=
HTTPS_CA_PATH=
HTTPS_CRT_PATH=

!!!重要提示:

  1. MONGODB_URL 文件中的 usernamepassword 必须与 docker-compose.yml 中的数据库用户和密码完全一致。
  2. KEY 是用于加密所有文件的密钥,一旦设置,绝对不能丢失或更改,否则所有已上传的文件将无法解密!
  3. PASSWORD_ACCESS, PASSWORD_REFRESH, PASSWORD_COOKIE 这三个值也必须修改,它们是保证账户安全的关键。

下表详细列出了每个环境变量的名称及其描述,便于理解 myDrive 应用的配置选项

环境变量 描述
DOCKER 设置为 true 表示应用在 Docker 中运行
MONGODB_URL MongoDB 数据库的连接字符串,格式为 mongodb://username:password@mongo:27017/mydrive?authSource=admin
DB_TYPE 数据库类型,选择存储文件的位置,选项为 fs(文件系统)或 s3Amazon S3
FS_DIRECTORY 如果使用文件系统存储,指定文件存储目录,必须以斜杠结尾,例如 /home/kyle/mydrive/
S3_ID 如果使用 S3 存储,指定 S3 的访问 ID
S3_KEY 如果使用 S3 存储,指定 S3 的密钥
S3_BUCKET 如果使用 S3 存储,指定 S3 存储桶的名称
KEY 用于加密文件的密钥(可选),如果丢失将无法恢复数据
PASSWORD_ACCESS 访问令牌,随机生成的字符串,用于用户认证
PASSWORD_REFRESH 刷新令牌,随机生成的字符串,用于刷新用户会话
PASSWORD_COOKIE Cookie 令牌,随机生成的字符串,用于用户会话管理
VIDEO_THUMBNAILS_ENABLED 是否启用视频缩略图生成,依赖于 ffmpeg 工具
TEMP_DIRECTORY 生成视频缩略图时的临时存储目录,必须以斜杠结尾
TEMP_VIDEO_THUMBNAIL_LIMIT 视频缩略图的最大大小(字节)
BLOCK_CREATE_ACCOUNT 如果要阻止账户创建,可以设置为 true
HTTP_PORT HTTP 服务端口,默认值为 3000
HTTPS_PORT HTTPS 服务端口,默认值为 8080
URL 运行服务器的 URL,通常不需要更改
EMAIL_VERIFICATION 如果要启用电子邮件验证,可以设置为 true
EMAIL_DOMAIN 电子邮件域名,用于发送验证邮件
EMAIL_ADDRESS 发送验证邮件的地址
EMAIL_API_KEY 电子邮件服务的 API 密钥
EMAIL_HOST 电子邮件服务的主机地址
REMOTE_URL 验证电子邮件中发送的 URL,示例:https://mydrive-storage.com
SECURE_COOKIES HTTPS 下生成安全 Cookie,建议启用
SSL 如果要启用 SSL,设置为 true
HTTPS_KEY_PATH HTTPS 密钥文件路径,默认情况下 myDrive 在项目根目录查找
HTTPS_CA_PATH HTTPS CA 证书路径
HTTPS_CRT_PATH HTTPS 证书路径

docker-compose.yml

将下面的内容粘贴到 docker-compose.yml 文件中。这里我们使用了本地端口 3466 来避免冲突。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
services:
app:
image: kylehoell/mydrive:latest
container_name: mydrive-app
volumes:
# Use the following volumes section if you want to use bind mounts:
# - /path/to/data:/data/
# - /path/to/temp:/temp/

# Use the following volumes section if you want to use named volumes:
- ./data:/data/
- ./temp:/temp/
ports:
- "3466:3000"
# Optional: Uncomment the following line if you want to use HTTPS
#- "${HTTPS_PORT:-8080}:8080"

# Use expose: if using a reverse proxy
# expose:
# - 3000
# - 8080
env_file:
- env.txt # Copy .env.example to .env and fill in the values

mongo:
image: mongo:8
container_name: mydrive-mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: username
MONGO_INITDB_ROOT_PASSWORD: password
# expose:
# - 27017
volumes:
- ./db:/data/db
healthcheck:
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
timeout: 10s
interval: 10s
retries: 10
start_period: 10s

注意: 请将 password 替换为您自己的强密码。这里如果修改了,别忘记 env.txt 中的数据库连接也要做对应的修改

启动服务

完成以上配置后,在 mydrive 目录下执行以下命令一键启动:

1
docker-compose up -d

运行

服务启动后,在浏览器中输入 http://群晖IP:3466 就能看到登录界面

创建你的第一个账户后,即可开始上传和管理你的文件

主界面

ADD NEW 可以上传文件或者文件夹

试了个文件夹

上传成功后

参考文档

subnub/myDrive: Node.js and mongoDB Google Drive Clone
地址:https://github.com/subnub/myDrive

MyDrive - Open Source Google Drive Clone (Node, Docker, Amazon S3, MongoDB) : r/selfhosted
地址:https://www.reddit.com/r/selfhosted/comments/1iyqh8d/mydrive_open_source_google_drive_clone_node/