惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CERT Recently Published Vulnerability Notes
U
Unit 42
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Securelist
L
Lohrmann on Cybersecurity
Blog — PlanetScale
Blog — PlanetScale
Recorded Future
Recorded Future
D
DataBreaches.Net
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
I
Intezer
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
I
InfoQ
宝玉的分享
宝玉的分享
Security Latest
Security Latest
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
博客园 - 司徒正美
H
Hacker News: Front Page
Y
Y Combinator Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
NISL@THU
NISL@THU
月光博客
月光博客
有赞技术团队
有赞技术团队
Cloudbric
Cloudbric
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
A
Arctic Wolf
博客园 - 【当耐特】
W
WeLiveSecurity
V
Visual Studio Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
V
V2EX
C
Cyber Attacks, Cyber Crime and Cyber Security
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Stack Overflow Blog
Stack Overflow Blog

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟 从Python转向Pharo https://blog.just4fun.site/post/iot/iot-open-source-projects/ Python异步编程笔记 https://blog.just4fun.site/post/iot/iot-open-source-hardware-community/ 万物积木化开发者社区 CodeLab ❤️ Blender Scratch3技术分析之云变量 API(第7篇) [译]对管道(Pipes)的偏爱 [译]提出正确的问题比得到正确答案更重要 蓝牙设备与Scratch3.0 创建你的第一个Scratch3.0 Extension Scratch3技术分析之项目内部数据(第6篇) Scratch3技术分析之社区 API(第5篇) Scratch3技术分析之User API(第4篇) Scratch3技术分析之项目主页API(第3篇) Scratch3技术分析之静态资源API(第2篇) Scratch3.0、micro:bit与Windows7 https://blog.just4fun.site/post/iot/zerynth-vs-micropython/ 核聚变、方所与半宅空间 可视化编程为何是个糟糕的主意 codelab.club周末聚会 关于codelab.club '下一件大事'是一个房间 Hungry Robot - Eat everything 编程作为一种思考方式 今日简史 史蒂夫·乔布斯传 罗素自选文集 https://blog.just4fun.site/post/edx/tianjin-scratch-ai/ https://blog.just4fun.site/post/edx/richie-cms-openedx/ 徒步武功山 WebUSB与micro:bit 积木化编程与3D场景 夜宿武功山顶 scratch3-adapter接入优必选Alpha系列机器人 https://blog.just4fun.site/post/edx/video-migration-note/ scratch3-adapter重构笔记 https://blog.just4fun.site/post/edx/edx-community-members/ 两种硬件编程风格的比较 使用micro:bit自制PPT翻页笔 柏拉图对话集 scratch3.0 + micro:bit 七月电影放映计划 非营利组织的管理 Screenly--用树莓派让任何屏幕变为可编程的数字标牌 以最佳实践开始你的Django项目 micro:bit与事件驱动 为Scratch3.0设计的插件系统(上篇) OCR应用一例 近两年读过的一些好书 blockly开发之使用python驱动浏览器中的turtle(2) 牛顿新传 文学理论入门 逻辑的引擎 人生的意义 blockly开发之生成并运行js代码(1) blockly开发之hello world(0) micro:bit使用笔记 神器之Termux https://blog.just4fun.site/post/iot/micropython-notes/ Cozmo what is this Scratch的前世今生 下段旅程 我行在远方 爆裂 途中杂记 https://blog.just4fun.site/post/edx/open-edx-startup/ cozmo系列之入门 - 有性格且可编程的机器人 PaperWeekly开发笔记 创业二三事
将网站发布到七牛云
种瓜 · 2017-02-06 · via 夜行人

更新说明: 我已经发布为python包,使用pip安装即可,文档参考:wwj718/publish


年前写了一个脚本,将create-react-app build出来的网站整体发布到七牛云上

如此一来前端完全托管在云上,与后端的通信仅通过RESTful api完成,后端压力大大降低

在技术层面,这个脚本本质上是将本地目录上传到七牛云上,同时在url中保留目录结构信息,通用性比较好,如果你想把本地项目发布出去,它可能帮的上忙,所以分享到博客上

安装依赖(python2.7):

pip install qiniu==7.1.1 path.py

脚本

你可以直接下载 wget https://gist.githubusercontent.com/wwj718/7d287692086a8d3e320eed9e94c1e631/raw/7556743cd2ef765d3b3dcc5262e185dfae726a0a/upload2qiniu.py

 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
#!/usr/bin/env python
# encoding: utf-8
import sys,os
from qiniu import Auth, put_file, etag, urlsafe_base64_encode,BucketManager,CdnManager
import qiniu.config
from path import path # 文档参考# https://pythontips.com/2014/01/23/python-101-writing-a-cleanup-script/


#需要填写你的 Access Key 和 Secret Key
#export QINIU_ACCESS_KEY=xxx 
access_key = os.environ["QINIU_ACCESS_KEY"]
#export QINIU_SECRET_KEY=xxx 
secret_key = os.environ["QINIU_SECRET_KEY"]

#要上传的空间
#export QINIU_BUCKET=xxx 
bucket_name = os.environ["QINIU_BUCKET"]

bucket_domain = os.environ["QINIU_BUCKET_DOMAIN"]
# 构建鉴权对象
q = Auth(access_key, secret_key)

cdn_manager = CdnManager(q)

#初始化BucketManager
bucket = BucketManager(q)
file2refresh=[]

def is_diff(key,localfile):
    '''
    比较本地文件与云端文件是否一致
    '''
    ret, info = bucket.stat(bucket_name, key)
    #print(ret,info)
    hash = ret
    if hash:
        diff = ret['hash'] != etag(localfile) # diff==False,说明文件相同,这个有点绕
        return diff
    else:
        diff = True
        # 不存在hash,说明还没上传过
        return True # 为True,说明文件对比结果为不一致

def refresh(urls):
    #https://github.com/qiniu/python-sdk/blob/master/examples/cdn_manager.py 列出所有上传文件,在七牛中刷新
    refresh_url_result = cdn_manager.refresh_urls(urls)
    #print_result(refresh_url_result)


# 上传整个目录到七牛,保存目录结构(在url中实现,本质上是通过命名)
def upload(key,localfile):
    '''
    key 文件在七牛上的名字,和它的url有关 ,参考:http://qiniu-developer.u.qiniudn.com/docs/v6/api/overview/faq.html
    localfile 本地文件路径
    '''
    # 比对本地文件和云端文件,如果不同则上传
    if is_diff(key,localfile):
        token = q.upload_token(bucket_name, key, 3600)
        ret, info = put_file(token, key, localfile)
        #print(ret,info) # 打印出更新文件以便于在七牛中刷新 : https://portal.qiniu.com/refresh
        url  = 'http://{}/{}'.format(bucket_domain, key) 
        print("refresh url:",url)
        file2refresh.append(url) #全局
        


#要上传的本地文件夹
root_dir = sys.argv[1] 

d = path(root_dir)
for i in d.walk():
    if i.isfile():
        #只上传文件
        #from IPython import embed;embed()
        key = str(i.abspath()).replace(str(d.abspath())+"/","") #实际是相对路径
        localfile = str(i)
        upload(key,localfile) # 返回需要刷新的文件然后一次性刷新
if file2refresh:
    refresh(file2refresh)

使用

首先你需要配置相关参数

1
2
3
4
export QINIU_ACCESS_KEY=xxx 
export QINIU_SECRET_KEY=xxx 
export QINIU_BUCKET=xxx #bucket name
export QINIU_BUCKET_DOMAIN=xxx

之后将目录网站整体上传(实际是上传整个文件夹):python upload2qiniu.py SITE_DIR

说明

对其中的两个函数做个说明:

is_diff:用于比对本地文件与云端对应文件,如果有修改则上传,否则不重复上传,这个函数目的是优化性能

refresh:这个函数用于刷新文件,这是七牛的缓存机制导致的,参考https://portal.qiniu.com/refresh,如果对七牛上同名(key)文件作更新,需要刷新缓存才能生效

文章作者 种瓜

上次更新 2017-02-06