×¼±¸
- 1.Ò»¸öcloudflareÕ˺Šhttps://dash.cloudflare.com/
- 2.Ò»¸öB2Õ˺Šhttps://www.backblaze.com/b2/cloud-storage.html
- 3.windowsÈí¼þShareX
²½Öè
1.×¢²áB2Õ˺Å,µã»÷½øÈëB2 Cloud Storage,µã»÷Buckets´´½¨Ò»¸öBUcket,ÉèΪpublic,²¢ÉÏ´«Ò»¸öͼƬ,¼Ç¼ÏÂÏÂͼҪÓõÄÓòÃû
2.µã»÷App keys,Ìí¼ÓÒ»¸öеÄkey,bucket¾ÍÑ¡Äã¸Õ´´½¨µÄÄǸö,¼Ç¼ÏÂÄãµÄÃÜÔ¿,Ö®ºóÒªÔÚShareXÖÐÓÃ
3.´ò¿ªcf,cnameÒ»ÏÂÉÏͼҪ¼ÇµÄÓòÃû,СÔƶäµãÁÁ
4.¼ÓÒ»ÌõÒ³Ã滺´æ¹æÔò.
5.´´½¨Ò»¸öworkers,Õ³ÌùÏÂÁдúÂë,¼ÇµÃb2domainºÍbucketµÄÖµ¸Ä³É×Ô¼ºµÄ
'use strict';
const b2Domain = 'img.domain.com'; // configure this as per instructions above
const b2Bucket = 'bucket-name'; // configure this as per instructions above
const b2UrlPath = `/file/${b2Bucket}/`;
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;
}
6.workersÀïÌí¼Ó·ÓÉ,ʹ·ÃÎÊÄãµÄÓòÃûʱ,ÏÈ×ßworkers
·ÃÎÊÒ»ÏÂÄãµÄͼƬÎļþ
±ÈÈç˵һ¿ªÊ¼ÊÇhttps://f000.backblazeb2.com/file/backblaze1489498/wallhaven-md2x8m.jpg
ÏÖÔÚÓÃhttps://dlcu.cf/wallhaven-md2x8m.jpg¾Í¿ÉÒÔ·ÃÎÊÁË
7.ÅäÖÃShareX.
Õâ¸ö¸Ð¾õûɶºÃ˵µÄ,Ö÷Ò³Ãæ¨CÄ¿±ê¨CÉÏ´«Ä¿±êÉèÖèCbackblaze b2,ÌîÉϾÍÐÐÁË
½áÊø
Õâ¸öµÄºÃ´¦¾ÍÊÇ·½±ãÁËÉÏ´«,×Ô¼ºÔÚµçÄÔÇ°,½ØÒ»ÏÂͼֱ½ÓÉÏ´«,»òÕ߸´ÖÆÒ»ÏÂÖ±½ÓÉÏ´«
»¹ÓоÍÊÇ¿ÉÒÔÓÃ×Ô¼ºµÄÓòÃû,ÔÙ˵µÄ»°¾ÍÊÇÊý¾Ý×Ô¼ºÒ²ÄÜÕÒ»Ø.
ÔÎÄ£ºhttps://www.wangfuchao.com/1290/