Ali Cloud currently has no Flutter or DART SDK available for integration. Here according to ali Cloud OSS documentation and referenceJava form uploadTo write a method for Flutter to upload images to Ali Cloud OSS.
Ali Cloud OSS is used herePostObject (Form upload)Uploads the picture to the server
The HTTP library uses DIO
Dio form upload example
var formData = FormData.fromMap({
'name': 'wendux'.'age': 25.'file': await MultipartFile.fromFile('./text.txt', filename: 'upload.txt')});var response = await dio.post('/info', data: formData);
Copy the code
To preparePostObject (Form upload)Required parameters
///The main account AccessKey of Aliyun has access rights to all apis, which is highly risky. It is strongly recommended that you create and use a RAM account for API access or daily operation and maintenance. Please log in to the RAM console to create a RAM account.
const String OSSAccessKeyId = '<yourAccessKeyId>';
///Used to generate Signature
const String accessKeySecret = '<yourAccessKeySecret>';
const String policy = "{\" expiration \ ": \" the 2120-01-01 T12:00:00. 000 z \ ", \ "the conditions \" : [[\ "content - length - range \", 0, 104857600]]}";
Copy the code
There is a description appendix: Post Policy on aliyun document
Hmac-sha1 base64 is used to encrypt hMAC-sha1 base64
Specific upload picture code:
///Here, replace yourBucketName with yourBucketName
///Oss-cn-hangzhou: Endpoint This section uses Hangzhou as an example. Set this parameter to other regions based on actual conditions
final String url = 'https://yourBucketName.oss-cn-hangzhou.aliyuncs.com';
///Upload pictures to Ali Cloud OSS
Future uploadImage(File imgFile) async{
///Encode Policy Base64
String encodePolicy = base64Encode(utf8.encode(policy));
/// To generate the signature
String signature = getSignature(encodePolicy);
/// Use the package:path/path.dart library to get the image name
String fileName = basename(imgFile.path);
/// Ask Aliyun to create a folder for Flutter
fileName = 'flutter/$fileName';
var formData = FormData.fromMap({
'key': fileName,
'success_action_status':200.///If the field is set to 200 or 204, OSS returns an empty document with the corresponding status code.
'OSSAccessKeyId': OSSAccessKeyId,
'policy': encodePolicy,
'Signature':signature,
'Content-Type':'image/jpeg'.'file': await MultipartFile.fromFile(imgFile.path),
});
///Upload files through FormData
var response = await dio.post(url, data: formData,onSendProgress: (int sent,int total){
print('$sent $total');///Prints the progress of uploading data
});
print(response.headers);
print("${response.statusCode} ${response.statusMessage}");
if(response.statusCode == 200) {///Image uploaded successfully
///Url of the image uploaded successfully
String imageServerPath = '$url/$fileName'; }}Copy the code
Signature generation method:
///Obtain ali cloud OSS encryption parameter Signature
String getSignature(String encodePolicy){
var key = utf8.encode(accessKeySecret);
var bytes = utf8.encode(encodePolicy);
var hmacSha1 = new Hmac(sha1, key);
Digest sha1Result = hmacSha1.convert(bytes);
print("sha1Result:$sha1Result");
String signature = base64Encode(sha1Result.bytes);
print("signature:$signature");
return signature;
}
Copy the code
Final dart libraries and third-party libraries used:
///Dio is a powerful Dart Http request library
import 'package:dio/dio.dart';
///This is used for Base64 encoding, UTF-8 encoding
import 'dart:convert';
///This is used to get the name of the file file
import 'package:path/path.dart';
///Hmac-sha1 encryption (this is a third-party library needs to rely on)
import 'package:crypto/crypto.dart';
Copy the code