wayjam / picgo-plugin-s3

PicGo S3 插件
MIT License
115 stars 18 forks source link

建议HTTPS单独设置开关而非根据endpoint来解析 #18

Open KyleRicardo opened 1 year ago

KyleRicardo commented 1 year ago

根据README中的示例配置:

    "aws-s3": {
      "accessKeyID": "xxx",
      "secretAccessKey": "xxxxx",
      "bucketName": "my-bucket",
      "uploadPath": "{year}/{md5}.{extName}",
      "endpoint": "s3.us-west-000.backblazeb2.com",
      "urlPrefix": "https://img.example.com/"
    }

会导致上传失败。错误日志如下:

2022-11-28 18:19:13 [PicGo INFO] [PicGo Server] shutdown 
2022-11-28 18:19:19 [PicGo INFO] [PicGo Server] is listening at 36677 
2022-11-28 18:19:26 [PicGo INFO] Before transform 
2022-11-28 18:19:26 [PicGo INFO] Transforming... Current transformer is [path] 
2022-11-28 18:19:26 [PicGo INFO] Before upload 
2022-11-28 18:19:26 [PicGo INFO] beforeUploadPlugins: renameFn running 
2022-11-28 18:19:26 [PicGo INFO] Uploading... Current uploader is [aws-s3] 
2022-11-28 18:19:47 [PicGo ERROR] 上传到 Amazon S3 发生错误,请检查配置是否正确 
2022-11-28 18:19:47 [PicGo ERROR] 
------Error Stack Begin------
TimeoutError: socket hang up
    at connResetException (node:internal/errors:691:14)
    at Socket.socketOnEnd (node:_http_client:471:23)
    at Socket.emit (node:events:406:35)
    at Socket.emit (node:domain:475:12)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
-------Error Stack End------- 
2022-11-28 18:19:47 [PicGo WARN] failed 
2022-11-28 18:19:47 [PicGo ERROR] 
------Error Stack Begin------
TimeoutError: socket hang up
    at connResetException (node:internal/errors:691:14)
    at Socket.socketOnEnd (node:_http_client:471:23)
    at Socket.emit (node:events:406:35)
    at Socket.emit (node:domain:475:12)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
-------Error Stack End------- 
2022-11-28 18:19:47 [PicGo ERROR] 
------Error Stack Begin------
TimeoutError: socket hang up
    at connResetException (node:internal/errors:691:14)
    at Socket.socketOnEnd (node:_http_client:471:23)
    at Socket.emit (node:events:406:35)
    at Socket.emit (node:domain:475:12)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
-------Error Stack End------- 

反复检查,关键配置参数填的都没问题。然后想是不是网络问题。打开Clash日志查看,发现上传时走的端口是80。 但是Backblaze B2是禁止http访问的,必须走https(同样的,在Cloudflare里面需要把SSL设置为【严格】)。 然后进入src/uploader.ts中查看,发现如下代码:

try {
    const u = url.parse(opts.endpoint)
    sslEnabled = u.protocol === "https:"
  } catch {
    // eslint-disable-next-line no-empty
  }
  const http = sslEnabled ? require(["https"](https://nodejs.org/api/https.html)) : require(["http"](https://nodejs.org/api/http.html))

发现走不走443端口是根据endpoint配置来的。parse出来是https才走443。 然后把端口前面加上https://才能正常上传。 这感觉不太符合直觉。因为S3的URL是这样的:

https://your-bucket-name.s3.s3.us-west-004.backblazeb2.com/xxxxxx

上传的url的hostname前面是加上了bucketName的。不是直接与https://相连。 建议像bucketEndpoint一样给个开关来控制,不要根据url去parse。

以上。

Pil0tXia commented 1 year ago

是的。需要在endpoint前增加https才能上传成功。即https://s3.us-west-000.backblazeb2.com