python 将hexo部署到阿里云OSS https://www.ficapy.com/2016/03/06/install_hexo_to_aliyun_oss/index.html

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 将hexo部署到阿里云OSS https://www.ficapy.com/2016/03/06/install_hexo_to_aliyun_oss/index.html相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Ficapy

import os
import requests
import base64
import hmac
import datetime
from os.path import join, exists, relpath
from mimetypes import MimeTypes
from hashlib import sha1, md5
from pickle import dump, loads
from cStringIO import StringIO, InputType, OutputType
from lxml import html

KEY             = '---------'
SECRET          = '---------'
BUCKET          = '---------'
url             = '---------'
ABSOLUTE_PATH   = '/Users/Ficapy/CodeSpace/Blog/public'

s = requests.Session()

headers = {
    'Date': datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT'),
    'Content-Type': 'text/html',
    'HOST': url,
}


def signature(file_path):
    global headers
    path = join(ABSOLUTE_PATH, file_path)
    headers['Content-Type'] = MimeTypes().guess_type(path)[0] or 'text/html'
    sign = base64.b64encode(hmac.new(SECRET,
                                     'PUT' + "\n"
                                     + '' + "\n"
                                     + headers['Content-Type'] + "\n"
                                     + headers['Date'] + "\n"
                                     + '/' + BUCKET + '/' + file_path, sha1).digest()).strip()
    headers.update({'Authorization': 'OSS ' + KEY + ':' + sign})


def read_in_chunks(file_object, blocksize=1024):
    file_object = file_object if isinstance(file_object, (InputType, OutputType)) else open(
        join(ABSOLUTE_PATH, file_object))
    file_object.seek(0)
    while 1:
        data = file_object.read(blocksize)
        if not data:
            break
        yield data


def upload(file_path, file_object):
    signature(file_path)
    s.put('http://' + url + '/' + file_path, headers=headers, data=read_in_chunks(file_object)).raise_for_status()
    print('upload done: ' + file_path)


# 首先请求oss_fuck_md5文件,没有则全部上传  有则和生成的md5对比,本地文件不存在或变动则上传
# 所有文件上传成功后最后上传.oss_fuck_md5文件
def local_md5():
    file_md5_mp = {}
    for root, dirnames, files in os.walk(ABSOLUTE_PATH):
        for file in files:
            fullpath = join(root, file)
            rel = relpath(fullpath, ABSOLUTE_PATH)
            file_md5_mp[rel] = md5(open(fullpath).read()).hexdigest()
    return file_md5_mp


def remote_md5():
    a = s.get('http://' + url + '/oss_fuck_md5')
    if a.status_code != 200:
        return {}
    else:
        return loads(a.content)


def path_process(file_path):
    file_path = join(ABSOLUTE_PATH, file_path)
    tree = html.parse(file_path).getroot()
    hrefs = tree.xpath('//*[@href]')

    for i, href in enumerate(hrefs):
        rewrite = join(href.attrib['href'].split()[0], 'index.html')
        if exists(join(ABSOLUTE_PATH, rewrite[1:])):
            hrefs[i].attrib['href'] = rewrite
    f = StringIO()
    f.write(html.tostring(tree))
    return f


def main():
    remote = remote_md5()
    locals = local_md5()
    _locals = locals.copy()
    for local in _locals.keys():
        if _locals[local] != remote.get(local, None):
            continue
        else:
            locals.pop(local)

    for path in locals.keys():
        if path.endswith('html'):
            upload(path, path_process(path))
        else:
            upload(path, path)

    f = StringIO()
    dump(_locals, f)
    upload('oss_fuck_md5', f)


if __name__ == '__main__':
    main()

以上是关于python 将hexo部署到阿里云OSS https://www.ficapy.com/2016/03/06/install_hexo_to_aliyun_oss/index.html的主要内容,如果未能解决你的问题,请参考以下文章

Hexo部署到阿里云

使用阿里云OSS部署静态网站

Hexo部署到阿里云服务器ECS

Hexo部署到阿里云服务器ECS

Hexo部署到阿里云服务器ECS!

Hexo部署到云服务器