putObject method

Future<String?> putObject(
  1. String objectKey,
  2. String filePath, {
  3. String? token,
  4. String? contentType = "image/jpeg",
})

Implementation

Future<String?> putObject(String objectKey, String filePath,
    {String? token,String? contentType = "image/jpeg"}) async {
  cosLog("putObject");
  var f = File(filePath);
  int flength = await f.length();
  var req = await getRequest("PUT", objectKey,
      headers: {
        "content-type": contentType,
        "content-length": flength.toString()
      },
      token: token);
  var fs = f.openRead();
  await req.addStream(fs);
  var response = await req.close();
  cosLog("request-id:" + (response.headers["x-cos-request-id"]?.first ?? ""));
  if (response.statusCode != 200) {
    String content = await response.transform(utf8.decoder).join("");
    cosLog("putObject error content: $content");
    return null;
  } else {
    return objectKey;
  }
}