putObjectFile method

  1. @override
Future<Response> putObjectFile(
  1. String filepath, {
  2. PutRequestOption? option,
  3. CancelToken? cancelToken,
  4. String? fileKey,
})

upload object(file) to oss server filepath is the filepath of the File that will send to oss server bucketName is optional, we use the default bucketName as we defined in Client

Implementation

@override
Future<Response<dynamic>> putObjectFile(
  String filepath, {
  PutRequestOption? option,
  CancelToken? cancelToken,
  String? fileKey,
}) async {
  final String bucket = option?.bucketName ?? bucketName;
  final String filename = fileKey ?? filepath.split('/').last;
  final Auth auth = await getAuth();

  final MultipartFile multipartFile = await MultipartFile.fromFile(
    filepath,
    filename: filename,
  );

  final Callback? callback = option?.callback;

  final Map<String, dynamic> internalHeaders = {
    'content-type': contentType(filename),
    'content-length': multipartFile.length,
    'x-oss-forbid-overwrite': option.forbidOverride,
    'x-oss-object-acl': option.acl,
    'x-oss-storage-class': option.storage,
  };

  final Map<String, dynamic> externalHeaders = option?.headers ?? {};
  final Map<String, dynamic> headers = {
    ...internalHeaders,
    if (callback != null) ...callback.toHeaders(),
    ...externalHeaders
  };

  final String url = "https://$bucket.$endpoint/$filename";
  final HttpRequest request = HttpRequest.put(url, headers: headers);

  auth.sign(request, bucket, filename);

  return _dio.put(
    request.url,
    data: multipartFile.finalize(),
    options: Options(headers: request.headers),
    cancelToken: cancelToken,
    onSendProgress: option?.onSendProgress,
    onReceiveProgress: option?.onReceiveProgress,
  );
}