一、 配置 Backblaze

Backblaze 官网:www.backblaze.com
Backblaze 是 2007 年创立的美国云存储和数据备份公司,数据可靠性和 SLA 不用过于担心。
Backblaze B2 是一个云存储解决方案,类似于 Amazon AWS S3, 但是价格稍微便宜一些. Backblaze 的云存储每个注册用户拥有 10G 免费空间以及每天 1G 的下载流量,上传流量不限。超过免费额度后的价格以及与其他几家主流商家的对比见下图。
img

1. 创建 Backblaze 账号并创建存储桶

创作一个桶 ,

img

桶独特名字越复杂越好,避免常用字符串,防止被猜出来恶意刷流量,桶设置为公众便于进行外部直接访问。

img

点击桶设定

img

增加缓存时间字段 {"cache-control":"max-age=43200"},增加 cloudflare 缓存命中率,完成后点击更新桶

img

随意上传一个文件(目的是为了查看存储桶参数,文件类型大小都无所谓),

img

点击右边的感叹号查看文件详细信息,

img

记下友好 URL 的域名和 S3 URL 中存储桶的位置,如下图所示,

img


二、 配置 cloudflare

由于 Bandwidth Alliance(带宽联盟),Backblaze 到 Cloudflare 之间的出口是 完全免费的,所以 Backblaze B2 可以配合 cloudflare CDN 来使用达到无限免费下载流量。
img

1. 接入自定义域名并开启 CDN

将自己的域名接入 cloudflare 后创建 cname 记录,将你自己想要的域名指向刚才友好 URL 的域名,并开启云朵(CDN),

img

假设存储桶中文件路径为 https://f000.backblazeb2.com/file/bucketname/year/month/day/yourfile.jpg ,则将 f000.backblazeb2.com 替换为你接入 CDN 的域名,若能正常访问到文件则配置成功。

2. 隐藏存储桶名称和无用信息

虽然现在已经可以访问图床文件,但是图床 URL 中有一些敏感信息会暴露存储桶的名称,导致源文件有被攻击刷流的风险,所以需要配置路由以隐藏敏感信息。
注:方法不唯一,这里通过 cloudflare workers 修改路由。

1)创建 workers 并填入代码

cloudflare worker 的创建不再赘述,网上有很多教程。
workers 代码:

javascript
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
'use strict';
const b2Domain = '你的域名'; // configure this as per instructions above
const b2Bucket = '存储桶名称'; // configure this as per instructions above
const b2UrlPath = <code>/file/${b2Bucket}/</code>;
addEventListener('fetch', event => {
return event.respondWith(fileReq(event));
});

// define the file extensions we wish to add basic access control headers to
const corsFileTypes = ['png', 'jpg', 'gif', 'jpeg', 'webp'];

// backblaze returns some additional headers that are useful for debugging, but unnecessary in production. We can remove these to save some size
const removeHeaders = [
'x-bz-content-sha1',
'x-bz-file-id',
'x-bz-file-name',
'x-bz-info-src_last_modified_millis',
'X-Bz-Upload-Timestamp',
'Expires'
];
const expiration = 31536000; // override browser cache for images - 1 year

// define a function we can re-use to fix headers
const fixHeaders = function(url, status, headers){
let newHdrs = new Headers(headers);
// add basic cors headers for images
if(corsFileTypes.includes(url.pathname.split('.').pop())){
newHdrs.set('Access-Control-Allow-Origin', '*');
}
// override browser cache for files when 200
if(status === 200){
newHdrs.set('Cache-Control', "public, max-age=" + expiration);
}else{
// only cache other things for 5 minutes
newHdrs.set('Cache-Control', 'public, max-age=300');
}
// set ETag for efficient caching where possible
const ETag = newHdrs.get('x-bz-content-sha1') || newHdrs.get('x-bz-info-src_last_modified_millis') || newHdrs.get('x-bz-file-id');
if(ETag){
newHdrs.set('ETag', ETag);
}
// remove unnecessary headers
removeHeaders.forEach(header => {
newHdrs.delete(header);
});
return newHdrs;
};
async function fileReq(event){
const cache = caches.default; // Cloudflare edge caching
const url = new URL(event.request.url);
if(url.host === b2Domain && !url.pathname.startsWith(b2UrlPath)){
url.pathname = b2UrlPath + url.pathname;
}
let response = await cache.match(url); // try to find match for this request in the edge cache
if(response){
// use cache found on Cloudflare edge. Set X-Worker-Cache header for helpful debug
let newHdrs = fixHeaders(url, response.status, response.headers);
newHdrs.set('X-Worker-Cache', "true");
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newHdrs
});
}
// no cache, fetch image, apply Cloudflare lossless compression
response = await fetch(url, {cf: {polish: "lossless"}});
let newHdrs = fixHeaders(url, response.status, response.headers);

if(response.status === 200){

response = new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newHdrs
});
}else{
response = new Response('File not found!', { status: 404 })
}

event.waitUntil(cache.put(url, response.clone()));
return response;
}

修改 " 你的域名 "和" 存储桶名称 " 后粘贴至 workers 并保存。

2)配置路由

回到自己的域名页,点击 Workers Routes

图片[1]-利用cloudflare+backblaze自建图床源站
img

按照下图填写:

img

此时访问 https://你的域名/year/month/day/yourfile.jpg ,若能访问到文件则配置成功。

3. 配置缓存时间

按照下图操作:

图片[2]-利用cloudflare+backblaze自建图床源站
img

注:可自行调整配置。

至此,图床源站配置完成,只需配合一个图床程序即可,推荐兰空图床