uploadData static method

Future uploadData(
  1. String folderName,
  2. String fileName,
  3. Uint8List data
)

Implementation

static Future uploadData(
  String folderName,
  String fileName,
  Uint8List data,
) async {
  final length = data.length;

  final uri = Uri.parse(_s3Endpoint);
  final req = http.MultipartRequest("POST", uri);
  final multipartFile = http.MultipartFile(
      'file', http.ByteStream.fromBytes(data), length,
      filename: fileName);

  final policy = Policy.fromS3PresignedPost(
      '$folderName/$fileName', _bucketname, _accessKeyId, 15, length,
      region: _region);
  final key =
      SigV4.calculateSigningKey(_secretKeyId, policy.datetime, _region, 's3');
  final signature = SigV4.calculateSignature(key, policy.encode());

  req.files.add(multipartFile);
  req.fields['key'] = policy.key;
  req.fields['acl'] = 'public-read';
  req.fields['X-Amz-Credential'] = policy.credential;
  req.fields['X-Amz-Algorithm'] = 'AWS4-HMAC-SHA256';
  req.fields['X-Amz-Date'] = policy.datetime;
  req.fields['Policy'] = policy.encode();
  req.fields['X-Amz-Signature'] = signature;

  try {
    final res = await req.send();
    await for (var value in res.stream.transform(utf8.decoder)) {
      if (kDebugMode) {
        print(value);
      }

      return value;
    }
  } catch (e) {
    if (kDebugMode) {
      print(e.toString());
    }

    return e;
  }
}