一键同步cos中的内容

时间:2022-07-24
本文章向大家介绍一键同步cos中的内容,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

上文说到博客从hexo转向hugo,又迎来了新的问题.那就是如何快速的部署我们的博客到云上..

因为hugo不像hexo那样有各种各样的插件,所以部署的话基本都靠手动 目前有几个可行的方案

  • Github pages 服务 稳定性没的说,但是必须要仓库公开,有点难受的感觉
  • coding pages 服务 速度比较占优势,但是稳定性有些欠妥(听说企业版好用些,但是没试过..)
  • netlify + github 服务 稳定性也很好,也支持一些拓展功能(比如一些重定向之类的),是一个好的选择.
  • COS(腾讯家的存储器) 速度很好,但是不支持自定义域名的https,想要支持的话,要开CDN.国内CDN要求备案接入

考虑之下,我选择CloudFlare+COS,主要是CF支持一些好用的拓展性功能我很喜欢(防火墙之类),而放在COS,稳定性较好.老用户有免费额度,够个人用了..但是手动上传也够难受的,所以就简单写了一个py

先要安装COS模块 具体可以看 这里

脚本如下:

#!/usr/bin/env python3
# coding:utf-8
# @Author: yumu
# @Date:   2019-12-14
# @Email:   yumusb@foxmail.com
# @Last Modified by:   yumu
# @Last Modified time: 2019-12-14

import os 
import hashlib
import qcloud_cos

def getfiles(path):
	filelist=[]
	for root,dirs,files in os.walk(path):
		for name in files:
			filelist.append(os.path.join(root,name))
	return filelist

def getlocalfiles(path):
	myfiles = getfiles(path)
	localfile={}
	for file in myfiles:
		fileETag = hashlib.md5(open(file,'rb').read()).hexdigest()
		localfile.update({file[len(path):]:fileETag})
	return localfile

def getremotefiles(client,bucket):
	response = client.list_objects(Bucket=bucket,Prefix='')['Contents']
	remotefile={}
	for x in response:
		remotefile.update({x['Key']:x['ETag'][1:-1]})
	return remotefile


secret_id = '' #https://console.cloud.tencent.com/cam/capi
secret_key = ''
region = '' #your region
scheme = 'https'
bucket = '' #bucket name
config = qcloud_cos.CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Scheme=scheme)
client = qcloud_cos.CosS3Client(config)


path = "./public/" #dir need to upload 

remotefile=getremotefiles(client,bucket)

localfile=getlocalfiles(path)

#print(localfile)
rmd5s=sorted(remotefile.values())
lmd5s=sorted(localfile.values())
if(rmd5s==lmd5s):
	exit("nothing todo ")

else:
	for x in localfile:
		if(remotefile.get(x) == None or localfile[x]!=remotefile[x]):
			with open(path+x,'rb')as f:
				response = client.put_object(Bucket=bucket,Body=f,Key=x,EnableMD5=False)
				if(len(response['ETag']) == 34):
					print("[+] %s upload success."%(path+x))
				else:
					print("[+] %s upload fail."%(path+x))
	for y in remotefile:
		if(localfile.get(y) == None):
			response = client.delete_object(Bucket=bucket,Key=y)
			print("[+] %s del success."%(y))

大致思路: 获取./public/ 目录下的所有文件=>获取上一步所有文件的md5=>获取COS远程文件的MD5=>比对,变化的上传=>如果远程文件在本地不存在则删除 这样就做到了增量更新.

如果使用中有Bug 欢迎留言~