Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dyh] -feat: add MultipartUpload method #51

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -714,4 +714,79 @@ class Client with AuthMixin, HttpMixin implements ClientApi {

return await Future.wait(deletes);
}

/// initiateMultipartUpload from oss
@override
Future<Response<dynamic>> initiateMultipartUpload(
String fileKey, {
String? bucketName,
CancelToken? cancelToken,
}) async {
final String bucket = bucketName ?? this.bucketName;
final Auth auth = await getAuth();

final String url = "https://$bucket.$endpoint/$fileKey?uploads";
final HttpRequest request = HttpRequest.post(url);
auth.sign(request, bucket, "$fileKey?uploads");

return _dio.post(
request.url,
cancelToken: cancelToken,
options: Options(headers: request.headers),
);
}

/// uploadPart from oss
@override
Future<Response<dynamic>> uploadPart(
String fileKey,
List<int> partData,
int partNumber,
String uploadId, {
String? bucketName,
CancelToken? cancelToken,
}) async {
final String bucket = bucketName ?? this.bucketName;
final Auth auth = await getAuth();

final String url =
"https://$bucket.$endpoint/$fileKey?partNumber=$partNumber&uploadId=$uploadId";
final HttpRequest request = HttpRequest.put(url, headers: {
'content-type': 'application/octet-stream',
});
auth.sign(request, bucket, "$fileKey?partNumber=$partNumber&uploadId=$uploadId");

return _dio.put(
request.url,
data: partData,
cancelToken: cancelToken,
options: Options(headers: request.headers),
);
}

/// completeMultipartUpload from oss
@override
Future<Response<dynamic>> completeMultipartUpload(
String fileKey,
String uploadId,
String data, {
String? bucketName,
CancelToken? cancelToken,
}) async {
final String bucket = bucketName ?? this.bucketName;
final Auth auth = await getAuth();

final String url = "https://$bucket.$endpoint/$fileKey?uploadId=$uploadId";
final HttpRequest request = HttpRequest.post(url, headers: {
'content-type': 'application/xml',
});
auth.sign(request, bucket, "$fileKey?uploadId=$uploadId");

return _dio.post(
request.url,
data: data,
cancelToken: cancelToken,
options: Options(headers: request.headers),
);
}
}
23 changes: 23 additions & 0 deletions lib/src/client_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,27 @@ abstract class ClientApi {
String? bucketName,
CancelToken? cancelToken,
});

Future<Response<dynamic>> initiateMultipartUpload(
String fileKey, {
String? bucketName,
CancelToken? cancelToken,
});

Future<Response<dynamic>> uploadPart(
String fileKey,
List<int> partData,
int partNumber,
String uploadId, {
String? bucketName,
CancelToken? cancelToken,
});

Future<Response<dynamic>> completeMultipartUpload(
String fileKey,
String uploadId,
String data, {
String? bucketName,
CancelToken? cancelToken,
});
}