B2+CF+ShareX,实现无成本图床和便捷上传

  • A+
所属分类:其他

准备
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,并上传一个图片,记录下下图要用的域名
B2+CF+ShareX,实现无成本图床和便捷上传
2.点击App keys,添加一个新的key,bucket就选你刚创建的那个,记录下你的密钥,之后要在ShareX中用
B2+CF+ShareX,实现无成本图床和便捷上传
3.打开cf,cname一下上图要记的域名,小云朵点亮
B2+CF+ShareX,实现无成本图床和便捷上传
4.加一条页面缓存规则.
B2+CF+ShareX,实现无成本图床和便捷上传
5.创建一个workers,粘贴下列代码,记得b2domain和bucket的值改成自己的

  1. 'use strict';

  2. const b2Domain = 'img.domain.com'; // configure this as per instructions above

  3. const b2Bucket = 'bucket-name'; // configure this as per instructions above

  4. const b2UrlPath = /file/${b2Bucket}/;

  5. addEventListener('fetch', event => {

  6. return event.respondWith(fileReq(event));

  7. });

  8. // define the file extensions we wish to add basic access control headers to

  9. const corsFileTypes = ['png', 'jpg', 'gif', 'jpeg', 'webp'];

  10. // backblaze returns some additional headers that are useful for debugging, but unnecessary in production. We can remove these to save some size

  11. const removeHeaders = [

  12. 'x-bz-content-sha1',

  13. 'x-bz-file-id',

  14. 'x-bz-file-name',

  15. 'x-bz-info-src_last_modified_millis',

  16. 'X-Bz-Upload-Timestamp',

  17. 'Expires'

  18. ];

  19. const expiration = 31536000; // override browser cache for images - 1 year

  20. // define a function we can re-use to fix headers

  21. const fixHeaders = function(url, status, headers){

  22. let newHdrs = new Headers(headers);

  23. // add basic cors headers for images

  24. if(corsFileTypes.includes(url.pathname.split('.').pop())){

  25. newHdrs.set('Access-Control-Allow-Origin', '*');

  26. }

  27. // override browser cache for files when 200

  28. if(status === 200){

  29. newHdrs.set('Cache-Control', "public, max-age=" + expiration);

  30. }else{

  31. // only cache other things for 5 minutes

  32. newHdrs.set('Cache-Control', 'public, max-age=300');

  33. }

  34. // set ETag for efficient caching where possible

  35. const ETag = newHdrs.get('x-bz-content-sha1') || newHdrs.get('x-bz-info-src_last_modified_millis') || newHdrs.get('x-bz-file-id');

  36. if(ETag){

  37. newHdrs.set('ETag', ETag);

  38. }

  39. // remove unnecessary headers

  40. removeHeaders.forEach(header => {

  41. newHdrs.delete(header);

  42. });

  43. return newHdrs;

  44. };

  45. async function fileReq(event){

  46. const cache = caches.default; // Cloudflare edge caching

  47. const url = new URL(event.request.url);

  48. if(url.host === b2Domain && !url.pathname.startsWith(b2UrlPath)){

  49. url.pathname = b2UrlPath + url.pathname;

  50. }

  51. let response = await cache.match(url); // try to find match for this request in the edge cache

  52. if(response){

  53. // use cache found on Cloudflare edge. Set X-Worker-Cache header for helpful debug

  54. let newHdrs = fixHeaders(url, response.status, response.headers);

  55. newHdrs.set('X-Worker-Cache', "true");

  56. return new Response(response.body, {

  57. status: response.status,

  58. statusText: response.statusText,

  59. headers: newHdrs

  60. });

  61. }

  62. // no cache, fetch image, apply Cloudflare lossless compression

  63. response = await fetch(url, {cf: {polish: "lossless"}});

  64. let newHdrs = fixHeaders(url, response.status, response.headers);

  65. if(response.status === 200){

  66. response = new Response(response.body, {

  67. status: response.status,

  68. statusText: response.statusText,

  69. headers: newHdrs

  70. });

  71. }else{

  72. response = new Response('File not found!', { status: 404 })

  73. }

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

  75. return response;

  76. }

复制代码

6.workers里添加路由,使访问你的域名时,先走workers
B2+CF+ShareX,实现无成本图床和便捷上传
访问一下你的图片文件
比如说一开始是https://f000.backblazeb2.com/file/backblaze1489498/wallhaven-md2x8m.jpg
现在用https://dlcu.cf/wallhaven-md2x8m.jpg就可以访问了
7.配置ShareX.
这个感觉没啥好说的,主页面--目标--上传目标设置--backblaze b2,填上就行了
B2+CF+ShareX,实现无成本图床和便捷上传
结束
这个的好处就是方便了上传,自己在电脑前,截一下图直接上传,或者复制一下直接上传
还有就是可以用自己的域名,再说的话就是数据自己也能找回.

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin