I am working on an app that uploads an image to Amazon S3 bucket, and instead of just showing a Spinner, I'd love to be able to get Progress on the status of that upload.
I am uploading a file using MultipartRequest from Package:http. I am successfully uploading the file but I want to get the Progress of the file that is being uploaded. How can I achieve that? My current code looks something like this.
static Future<bool> uploadFile({@required userId, @required albumId, @required data, @required fileName}) async{
const _accessKeyId = 'AKIAVV**********';
const _secretKeyId = 'iyBmdn6v***************************';
const _region = 'us-east-1';
const _s3Endpoint = 'https://myalbums-content.s3.amazonaws.com';
final file = data;
var stream = new http.ByteStream(DelegatingStream.typed(file.openRead()));
final length = await file.length();
final uri = Uri.parse(_s3Endpoint);
final req = http.MultipartRequest("POST", uri);
var multipartFile = http.MultipartFile('file', stream, length,
contentType: new MediaType('image','JPG'));
final policy = Policy.fromS3PresignedPost(userId.toString() +'/'+ albumId.toString() +'/'+ fileName,
'myalbums-content', _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();
if(res.statusCode == 204){
return true;
} else {
return false;
}
} catch (e) {
print('Network issue: '+e.toString());
return false;
}
}
Thank you in advance.