阿里云免费建网盘
前言
我白嫖到了微软的25TB储存,但是下载不方便,想用这点容量搭一个网盘。
考虑到我没有可以使用的云服务器,所需就选择了Oneindex-Serverless。
看了阿里云官网的Oneindex-serverless搭建文档,踩了好几个坑。官方文档
然鹅阿里云的文章也不是自己写的。阿里嫖了知乎的一篇文章传送门

枯了枯了,网上还有很多文档,但是都是一摸一样的。
了解Onedrive-Serverless
Oneindex前段时间被删库了,作者亲自删的,TA也没说为什么。包括Goindex也被删库了。
Oneindex-Serverless是另一个人做的,仓库地址在这里https://github.com/LiuChangFreeman/OneIndex-Serverless
Oneindex是基于PHP的,需要PHP空间,像我这种白嫖党是不可能买的,这辈子都不可能 
而Oneindex是基于Serverless云函数的,不用PHP空间,对我来说十分有吸引力。
搭建Onedrive-Serverless
开通函数计算和对象储存OSS
注册阿里云
先开通函数计算和对象储存OSS。

配置对象储存OSS
然后我们先到对象储存OSS进行设置。控制台传送门
新建储存桶,Bucket。

然后进行以下配置。注意,要记住区域
,之后有用的,然后Bucket名称
可以自定义。


然后可以看到关于这个Bucket的概览
。记住内网
的Endpoint
,就在下面的图片红框中。//hh,图片我网上搜了一张,我以前截屏的丢了。

配置access子账户
右上角选择AccessKey
管理,

先选择创建AccessKey子账户
,

然后再创建用户,如下图。

然后自定义下面的内容

之后添加权限。

添加AliyunOSSFullAccess
权限,添加过后右侧会自动添加权限,然后被添加的权限也会变成淡蓝色。点击下面的确定
.

之后我们去获取AccessKey。
点击Access账户,即下图蓝色的文字。

在下方创建AccessKey
。然后弹出这样一个框:

ID和SECRET必须保存好,之后它们不会再出现!!!
配置Serverless函数计算
打开函数计算的控制台控制台传送门
提醒:记得左上角选择和Bucket一样的地域!!!不然serverless不能访问Bucket!!!
新建函数!

选择flask-web这个模板,然后下一步。

自定义下面图片红框的内容,最好你自己能够记牢。

然后我们来配置初始化。
打开下面的配置
.

进行如下的配置,新建main.initializer
函数初始化入口,并把超时时间
选最大。

注意:右下角还有一个提交
!!!别忘了提交,不然不会保存这个设置!!!
然后我们编辑一下代码。按下图打开代码执行管理
。

然后把下面的代码复制进去。会很长,但是省的你们去下载了。如果要下载,我过会儿会放我的网盘上哈,我已经放在网盘上了,在这里https://drive.yelow.ga/#/?path=Public_Source/Blog/dd57,前面的地址很慢,我又弄了个快一点的/真的只是快了一点点/地址:https://share.yelow.ga/Blog/dd57/文件为Back.zip。记得过会儿Save一下。
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
| from __future__ import print_function import os import oss2 import requests import urllib import json import base64 import time from flask import Flask,request,redirect
password="123456" url_host= ""
access_key='' access_key_secret='' oss_end_point= '' oss_bucket_name=''
path_oss_store= "oneindex-serverless" filename_token= "token.json" items_per_page=50
app = Flask(__name__) app.secret_key = 'oneindex-serverless'
client_id = '0375f766-e528-4748-91e2-7d8fcb702889' client_secret = 'vXOJL93{#?xnotilNIU895:' redirect_uri_register = 'https://oneindex-serverless.github.io/redirect' redirect_uri_final = '{}/login/authorized'.format(url_host.strip("/"))
auth = oss2.Auth(access_key,access_key_secret) bucket = oss2.Bucket(auth, oss_end_point, oss_bucket_name) base_url='https://graph.microsoft.com/v1.0/' scopes= "offline_access files.readwrite.all" select="id,name,size,folder,image,video,lastModifiedDateTime"
token=None oss_available=False
def initializer(context): init()
def handler(environ, start_response): return app(environ, start_response)
@app.route('/') def home(): if token==None or "account" not in token: data = { "success":False, "oss_available":oss_available } else: data = { "success": True, "account":token["account"], "oss_available":oss_available, } return json.dumps(data)
@app.route('/verify') def verify(): code=request.args.get("code") code=base64.b64decode(code) if code==password: data={ "success":True } else: data={ "success":False } return json.dumps(data)
@app.route('/login') def login(): code=request.args.get("code") code=base64.b64decode(code) final=request.args.get("final") if code==password: url_login="https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={}&scope={}&response_type=code&redirect_uri={}&state={}".format(client_id, urllib.quote(scopes), redirect_uri_register, redirect_uri_final+"*"+final) return redirect(url_login)
@app.route('/login/authorized') def authorized(): global token try: code=request.args.get("code") final = request.args.get("final") url = "https://login.microsoftonline.com/common/oauth2/v2.0/token" headers = { "Content-Type": "application/x-www-form-urlencoded" } form = "client_id={}&redirect_uri={}&client_secret={}&code={}&grant_type=authorization_code".format(client_id,redirect_uri_register,client_secret,code) token = requests.post(url, headers=headers, data=form).json() token["time"] = time.time() path = "me/drive" url = base_url + path access_token = token["access_token"] headers = { "Authorization": "bearer {}".format(access_token), "Content-Type": "application/json" } me = requests.get(url, headers=headers).json() try: token["account"]=me["owner"]["user"]["email"] except: token["account"] = me["owner"]["user"]["displayName"] token["drive"] = me["id"] json_token = json.dumps(token, ensure_ascii=False, indent=4) path_token = os.path.join(path_oss_store, filename_token) bucket.put_object(path_token, json_token) bucket.put_object_acl(path_token,oss2.OBJECT_ACL_PRIVATE) return redirect(final) except Exception as e: result={"error":e.message,"token":token,"code":code,"final":final} return json.dumps(result)
@app.route('/list', methods = ["GET","POST"]) def list(): try: drive = token["drive"] access_token = token["access_token"] headers={ "Authorization":"bearer {}".format(access_token), "Content-Type":"application/json" } if request.method=="POST": data=request.get_data(as_text=True) data = json.loads(data) url=data["next"] else: path=request.values.get("path") if path: path = "drives/{}/root:/{}:/children".format(drive, path) else: path = "me/drive/root/children" url = base_url + path url = "{}?$top={}&$select={}".format(url, items_per_page, select) data = requests.get(url, headers=headers).json() response={} items=[] list=data["value"] for item in list: result={} if "folder" in item: result["type"]="folder" result["childCount"]=item["folder"]["childCount"] elif "image" in item: result["type"]="picture" elif "video" in item: result["type"] = "play-square" else: result["type"] = "file" result["id"] = item["id"] result["name"] = item["name"] result["size"] = item["size"] result["time"] = item["lastModifiedDateTime"] items.append(result) response["data"]=items if "@odata.nextLink" in data: response["next"]=data["@odata.nextLink"] else: response["next"] =None except Exception as e: response={"error":e.message,"data":data} return json.dumps(response)
@app.route('/download') def download(): id = request.args.get("id") if id: path='me/drive/items/{}'.format(id) url = base_url + path access_token = token["access_token"] headers = { "Authorization": "bearer {}".format(access_token), "Content-Type": "application/json" } data = requests.get(url, headers=headers).json() if not "folder" in data: url_download=data["@microsoft.graph.downloadUrl"] return redirect(url_download)
@app.before_request def before(*args,**kwargs): global token try: time_now = time.time() time_last = token["time"] if time_now - time_last >3500: refresh_token = token["refresh_token"] scope = token["scope"] url = "https://login.microsoftonline.com/common/oauth2/v2.0/token" headers = { "Content-Type": "application/x-www-form-urlencoded" } data = { "client_id": client_id, "client_secret": client_secret, "redirect_uri": redirect_uri_register, "refresh_token": refresh_token, "grant_type": "refresh_token", "scope": scope, } data = requests.post(url, data=data, headers=headers).json() data["time"] = time.time() data["account"] = token["account"] data["drive"] = token["drive"] token = data json_token = json.dumps(data, ensure_ascii=False, indent=4) path_token = os.path.join(path_oss_store, filename_token) bucket.put_object(path_token, json_token) except: pass
def init(): global token,oss_available try: service = oss2.Service(auth, oss_end_point.replace("http://", ""), connect_timeout=0.5) service.list_buckets() oss_available=True path_token ="{}/{}".format(path_oss_store,filename_token) if bucket.object_exists(path_token): token = bucket.get_object(path_token) token = json.loads(token.read()) except: pass
if __name__=="__main__": init() app.run()
|
第11行是你网盘管理员的密码,可以自定义,并且要记牢,以后一直都会用。
第12行到下面的图片位置找。

然后14行在单引号中填写刚刚获取的AccessKey,15行填写AccessKey的密码
在16行填写Endpoint,然后17行填写Bucket名称。
前端配置
我们在前面已经把所有的后端搭建好了,我们就来搭建前端。
偷懒小提示:可以直接Fork我的仓库,再在Gayhub(打错了不好意思)Github里面修改config.json
文件即可。仓库地址:Github传送门
先下载必要文件:https://drive.yelow.ga/#/?path=Public_Source/Blog/dd57前面的地址贼慢,换一个下载地址_传送门这里面的Front.zip。
解压该文件,得到文件夹下两个文件:

好,我们不管index.html,我们配置config.json。
打开config.json,给host赋值为HTTP触发器,如果你忘了,那么请你再看一下这张照片:

配置完应该长这样,保存一下

然后,我定义了自己的域名(drive.yelow.ga),我就加了一个CNAME,并用Git同步到Github的一个仓库里。
仓库记得打开Pages,当然,Gitee和Coding也是可以用的。
然后访问域名,会发现啥都没有。
连接Onedrive
在你刚打开的域名后面加上/admin,(例如我的就是https://drive.yelow.ga/#/admin,这个#是它自动会加的)
就到了管理界面。

对,就张这挫样。可能是用来迷惑的,右面的按钮设计成了搜索。
输入你在阿里云里的密码。不不,不是Access的,是你在main.py里面的密码。
进入了简洁的管理界面。
如果Bucket没设置好,这里对象储存
就会报错。由于我已经登陆了Onedrive,所以第三行就变绿了。

绑定Onedrive,就在左侧账号管理
。

但这个Oneindex-Serverless和Oneindex还是有很大差别的,Serverless版的只能下载,PHP的还有上传。
最后,欢迎访问我的网盘hh,里面说不定有好东西哦!!!