fGetObject method

Future<void> fGetObject(
  1. String bucket,
  2. String object,
  3. String filePath
)

Downloads and saves the object as a file in the local filesystem.

Implementation

Future<void> fGetObject(
  String bucket,
  String object,
  String filePath,
) async {
  MinioInvalidBucketNameError.check(bucket);
  MinioInvalidObjectNameError.check(object);

  final stat = await statObject(bucket, object);
  final dir = dirname(filePath);
  await Directory(dir).create(recursive: true);

  final partFileName = '$filePath.${stat.etag}.part.minio';
  final partFile = File(partFileName);
  IOSink partFileStream;
  var offset = 0;

  Future<void> rename() async {
    await partFile.rename(filePath);
  }

  if (await partFile.exists()) {
    final localStat = await partFile.stat();
    if (stat.size == localStat.size) return await rename();
    offset = localStat.size;
    partFileStream = partFile.openWrite(mode: FileMode.append);
  } else {
    partFileStream = partFile.openWrite(mode: FileMode.write);
  }

  final dataStream = await getPartialObject(bucket, object, offset);
  await dataStream.pipe(partFileStream);

  final localStat = await partFile.stat();
  if (localStat.size != stat.size) {
    throw MinioError('Size mismatch between downloaded file and the object');
  }

  return rename();
}