uploadFile static method

Future<String?> uploadFile({
  1. required String accessKey,
  2. required String secretKey,
  3. required String bucket,
  4. required File file,
  5. String destDir = '',
  6. String region = 'us-east-2',
  7. ACL acl = ACL.public_read,
  8. String? filename,
  9. void onProgress(
    1. int bytes,
    2. int totalBytes
    )?,
})

Upload a file, returning the file's public URL on success.

Implementation

static Future<String?> uploadFile({
  /// AWS access key
  required String accessKey,

  /// AWS secret key
  required String secretKey,

  /// The name of the S3 storage bucket to upload  to
  required String bucket,

  /// The file to upload
  required File file,

  /// The path to upload the file to (e.g. "uploads/public"). Defaults to the root "directory"
  String destDir = '',

  /// The AWS region. Must be formatted correctly, e.g. us-west-1
  String region = 'us-east-2',

  ///Access control list enables you to manage access to bucket and objects
  ///For more information visit [https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html]
  ACL acl = ACL.public_read,

  /// The filename to upload as. If null, defaults to the given file's current filename.
  String? filename,

  /// this method will show file upload progress and file total size
  final void Function(int bytes, int totalBytes)? onProgress,
}) async {
  final endpoint = 'https://$bucket.s3-$region.amazonaws.com';
  final uploadDest = '$destDir/${filename ?? path.basename(file.path)}';

  final stream = http.ByteStream(Stream.castFrom(file.openRead()));
  final length = await file.length();

  final uri = Uri.parse(endpoint);
  final req = MultipartRequest("POST", uri, onProgress: onProgress);
  final multipartFile = http.MultipartFile('file', stream, length,
      filename: path.basename(file.path));

  final policy = Policy.fromS3PresignedPost(
      uploadDest, bucket, accessKey, 15, length, acl,
      region: region);
  final key =
      SigV4.calculateSigningKey(secretKey, policy.datetime, region, 's3');
  final signature = SigV4.calculateSignature(key, policy.encode());

  req.files.add(multipartFile);
  req.fields['key'] = policy.key;
  req.fields['acl'] = aclToString(acl);
  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 '$endpoint/$uploadDest';
  } catch (e) {
    print('Failed to upload to AWS, with exception:');
    print(e);
    return null;
  }
}