0

我尝试在颤振中使用 MultipartRequest 将文件上传到 s3,但是在到达“response.send()”时我得到了

I/flutter ( 8307): HandshakeException: Handshake error in client (OS Error:
I/flutter ( 8307):  CERTIFICATE_VERIFY_FAILED: self signed certificate in certificate chain(handshake.cc:354))

我在 Postman 中关闭了“SSL 证书验证”以使其正常工作,那么有什么方法可以在上传时将其关闭吗?

这是我使用的代码:

var request = http.MultipartRequest('POST', uri)
    ..fields['key'] = data.key
    ..fields['x-amz-algorithm'] = data.algorithm
    ..fields['x-amz-credential'] = data.credential
    ..fields['x-amz-date'] = data.date
    ..fields['x-amz-security-token'] = data.securityToken
    ..fields['policy'] = data.policy
    ..files.add(await http.MultipartFile.fromPath('File', imagePath, filename: imageName));
    print(request.toString());
    try {
        var  response = await request.send();
    await for (var value in response.stream.transform(utf8.decoder)) {
        print(value);
     }
    } catch (e) {
    print(e.toString());
    }
4

1 回答 1

1

如果有人面临同样的问题,我可以像这样使用“Dio”包解决上述问题

Dio _client = Dio();
_client.interceptors.add(LogInterceptor());
FormData formData = FormData.fromMap({
  'key': data.key,
  'x-amz-algorithm': data.algorithm,
  'x-amz-credential': data.credential,
  'x-amz-date': data.date,
  'x-amz-security-token': data.securityToken,
  'policy': data.policy,
  'x-amz-signature': data.signature,
  'File': await MultipartFile.fromFile(
    filePath,
    filename: fileName,
  )
});
(_client.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
    (HttpClient dioClient) {
  dioClient.badCertificateCallback =
      (X509Certificate cert, String host, int port) => true;
  return dio;
};
try {
  await _client.post(
    data.uploadUrl,
    data: formData,
  );
  _client.close();
} catch (e) {
  print(e.toString());
  _client.close();
}
于 2020-04-21T06:20:46.670 回答