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 1 commit
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);

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);

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);

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,
});
}
20 changes: 16 additions & 4 deletions lib/src/model/auth.dart
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这边我不确定有没有影响到其他,逻辑可能还有问题,自测是通了。InitiateMultipartUpload这个url特殊,它是...?uploads,你的这个签名会跟oss那边的对不上

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这边我不确定有没有影响到其他,逻辑可能还有问题,自测是通了。InitiateMultipartUpload这个url特殊,它是...?uploads,你的这个签名会跟oss那边的对不上

签名对不上的问题如何解决?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

通了是指能上传成功了还是?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

能上传成功

Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class Auth {
final String contentType = req.headers['content-type'] ?? '';
final String date = req.headers['x-oss-date'] ?? '';
final String headerString = _getHeaderString(req);
final String resourceString = _getResourceString(bucket, key, req.param);
final String resourceString =
_getResourceString(bucket, key, req.param, req.oriUrl);
final String stringToSign = [
req.method,
contentMd5,
Expand Down Expand Up @@ -104,17 +105,28 @@ class Auth {
String _getResourceString(
String bucket,
String fileKey,
Map<String, dynamic> param,
) {
Map<String, dynamic> param, [
String? url,
]) {
String path = "/";
if (bucket.isNotEmpty) path += "$bucket/";
if (fileKey.isNotEmpty) path += fileKey;

bool hasSuffix = false;

if (url != null) {
if (url.lastIndexOf("?") != -1) {
hasSuffix = true;
path += "?${url.split("?").last}";
}
}

final String signedParamString = param.keys
.where((key) => SignParameters.signedParams.contains(key))
.map((item) => "$item=${param[item]}")
.join("&");
if (signedParamString.isNotEmpty) {
path += "?$signedParamString";
path += "${hasSuffix ? '' : '?'}$signedParamString";
}

return path;
Expand Down
4 changes: 4 additions & 0 deletions lib/src/model/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class HttpRequest {
return queryString.isEmpty ? _url : "$_url?$queryString";
}

String get oriUrl {
return _url;
}

factory HttpRequest.get(
String url, {
Map<String, dynamic>? parameters,
Expand Down