uploadFile method

Future<String> uploadFile(
  1. File file,
  2. String bucketName,
  3. String poolID,
  4. _AWSRegion region, {
  5. String s3FolderPath = "",
  6. String? fileName,
  7. _AWSRegion? subRegion,
  8. S3AccessControl accessControl = S3AccessControl.publicRead,
  9. bool useTimeStamp = false,
  10. TimestampLocation timeStampLocation = TimestampLocation.prefix,
  11. bool debugLog = false,
})

Upload function takes File, S3 bucket name, pool ID and region as required param Default access control is PUBLIC_READ Debugging is disable by default this will prevent any logs and messages To enable use debugLog:true

Implementation

Future<String> uploadFile(
  File file,
  String bucketName,
  String poolID,
  _AWSRegion region, {
  String s3FolderPath: "",
  String? fileName,
  _AWSRegion? subRegion,
  S3AccessControl accessControl: S3AccessControl.publicRead,
  bool useTimeStamp: false,
  TimestampLocation timeStampLocation: TimestampLocation.prefix,
  bool debugLog: false,
}) async {
  Map<String, dynamic> args = <String, dynamic>{};
  var result;
  String contentType;

  if (!await file.exists()) throw SimpleS3Errors.FileDoesNotExistsError;

  if (!(fileName != null && fileName.length > 0)) {
    String originalFileName = file.path.split('/').last.replaceAll(" ", "");

    if (useTimeStamp) {
      int timestamp = DateTime.now().millisecondsSinceEpoch;

      if (timeStampLocation == TimestampLocation.prefix) {
        fileName = '$timestamp\_$originalFileName';
      } else {
        fileName = '${originalFileName.split(".").first}\_$timestamp\.${originalFileName.split(".").last}';
      }
    } else
      fileName = originalFileName;
  }

  contentType = mime(fileName)!;

  if (debugLog) {
    debugPrint('S3 Upload Started <-----------------');
    debugPrint(" ");
    debugPrint("File Name: $fileName");
    debugPrint(" ");
    debugPrint("Content Type: $contentType");
    debugPrint(" ");
  }

  args.putIfAbsent("filePath", () => file.path);
  args.putIfAbsent("poolID", () => poolID);
  args.putIfAbsent("region", () => region.region);
  args.putIfAbsent("bucketName", () => bucketName);
  args.putIfAbsent("fileName", () => fileName);
  args.putIfAbsent("s3FolderPath", () => s3FolderPath);
  args.putIfAbsent("debugLog", () => debugLog);
  args.putIfAbsent("contentType", () => contentType);
  args.putIfAbsent("subRegion", () => subRegion != null ? subRegion.region : "");
  args.putIfAbsent("accessControl", () => accessControl.index);

  bool methodResult = await _methodChannel.invokeMethod('upload', args);

  if (methodResult) {
    String _region = subRegion != null ? subRegion.region : region.region;
    String _path = s3FolderPath != "" ? bucketName + "/" + s3FolderPath + "/" + fileName : bucketName + "/" + fileName;

    result = "https://s3-$_region.amazonaws.com/$_path";

    if (debugLog) {
      debugPrint("Status: Uploaded");
      debugPrint(" ");
      debugPrint("URL: $result");
      debugPrint(" ");
      debugPrint("Access Type: $accessControl");
      debugPrint(" ");
      debugPrint('S3 Upload Completed-------------->');
      debugPrint(" ");
    }
  } else {
    if (debugLog) {
      debugPrint("Status: Error");
      debugPrint(" ");
      debugPrint('S3 Upload Error------------------>');
    }
    throw SimpleS3Errors.UploadError;
  }

  return result;
}