B2 Cloudflare

Posted on  by 



B2 Cloudflare

I want to setup Cloudflare CDN to speed up my Backblaze B2 bucket. It’s a private one so I’m using this tutorial: I see however that tutorial doesn’t quite match the current Cloudflare interface. I am hoping to to take this step by step, i’m not a programmer. The tutorial isn’t very clear or a step by step. But it has a script at the bottom of the page I assume is for the worker. B2 cloud with Cloudflare. Posted by 2 days ago. B2 cloud with Cloudflare. Hi:) Hope this is the right place to ask this question. Was reading to perform. Backblaze B2’s compatibility for the S3 API is an exciting update that has made their storage platform highly compatible with existing code bases and legacy systems. And, as a special offer to Cloudflare blog readers, Backblaze will pay the migration costs for transferring your data from S3 to Backblaze B2 (click here for more detail).

B2 Cloudflare Student

Backblaze
cf-worker-b2.js
'use strict';
constb2Domain=';//你要在Cloudflare上綁定的Backblaze網域
constb2Bucket=';//Backblaze B2的存儲桶名稱
constb2UrlPath=`/file/${b2Bucket}/`;
addEventListener('fetch',event=>{
returnevent.respondWith(fileReq(event));
});
// define the file extensions we wish to add basic access control headers to
constcorsFileTypes=['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
constremoveHeaders=[
'x-bz-content-sha1',
'x-bz-file-id',
'x-bz-file-name',
'x-bz-info-src_last_modified_millis',
'X-Bz-Upload-Timestamp',
'Expires'
];
constexpiration=31536000;// override browser cache for images - 1 year
// define a function we can re-use to fix headers
constfixHeaders=function(url,status,headers){
letnewHdrs=newHeaders(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(status200){
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
constETag=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);
});
returnnewHdrs;
};
asyncfunctionfileReq(event){
constcache=caches.default;// Cloudflare edge caching
consturl=newURL(event.request.url);
if(url.hostb2Domain&&!url.pathname.startsWith(b2UrlPath)){
url.pathname=b2UrlPath+url.pathname;
}
letresponse=awaitcache.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
letnewHdrs=fixHeaders(url,response.status,response.headers);
newHdrs.set('X-Worker-Cache','true');
returnnewResponse(response.body,{
status: response.status,
statusText: response.statusText,
headers: newHdrs
});
}
// no cache, fetch image, apply Cloudflare lossless compression
response=awaitfetch(url,{cf: {polish: 'lossless'}});
letnewHdrs=fixHeaders(url,response.status,response.headers);
if(response.status200){
response=newResponse(response.body,{
status: response.status,
statusText: response.statusText,
headers: newHdrs
});
}else{
response=newResponse('找不到檔案!',{status: 404})
}
event.waitUntil(cache.put(url,response.clone()));
returnresponse;
}
CloudflareB2 Cloudflare

B2 Cloudflare 2

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment




Coments are closed