tencent cloud

Video on Demand

ドキュメントVideo on Demand

Flutter Upload SDK

Download
フォーカスモード
フォントサイズ
最終更新日: 2025-08-05 09:38:22
This example is based on VOD Professional Flutter SDK v1.0.2.

​Environment Preparation​

Flutter SDK Version: ≥ 3.3.0.
When uploading media files on iOS and Android, if media files uploaded by your app are sourced from the gallery or device storage, you must configure and request necessary permissions (e.g., gallery access, storage access).
​The demo uses third-party plugins for access and configures required permissions. Adjust according to your business scenario.​​
<uses-permission android:name="android.permission.INTERNET"/>

Dependencies​

Copy the voduploadadv project locally and use a relative path for dependency.
​Example​ (assuming voduploadadv is in a sibling directory):
voduploadadv:
path: ../voduploadadv/
After adding, run these commands to refresh dependencies:
flutter clean
flutter pub get

Authentication Configuration​

Upload requires client identity authentication. Configure an authentication callback to provide secretId, secretKey, token, and expiredTime when needed by the SDK.
Note:
This configuration is mandatory. Skipping it will cause upload failures.​​
TxVodUploadPlugin.instance.initWithScopeLimitCredential((stsCredentialScopes) async {
// Recommended: Fetch credentials from your server
Map<String, dynamic> jsonMap = jsonDecode(testRsCre);
Map<String, dynamic> resJsonMap = jsonMap["Response"];
Map<String, dynamic> credentialsJsonMap = resJsonMap["Credentials"];
int expiredTimeStamp = DateTime.parse(credentialsJsonMap['Expiration']).millisecondsSinceEpoch;
return FTXSessionQCloudCredentials(
secretId: credentialsJsonMap["AccessKeyId"],
secretKey: credentialsJsonMap["SecretAccessKey"],
token: credentialsJsonMap["SessionToken"],
expiredTime: expiredTimeStamp);
});
The stsCredentialScopes parameter includes:
region: Default empty string.
bucket: Your bucket ID (auto in auto-mode).
prefix: Directory prefix (default empty).
action: Possible values:
// Simple upload
"name/cos:PutObject",
// Form upload
"name/cos:PostObject",
// Multipart upload: Initiate
"name/cos:InitiateMultipartUpload",
// Multipart upload: List ongoing
"name/cos:ListMultipartUploads",
// Multipart upload: List uploaded parts
"name/cos:ListParts",
// Multipart upload: Upload part
"name/cos:UploadPart",
// Multipart upload: Complete
"name/cos:CompleteMultipartUpload",
// Multipart upload: Abort
"name/cos:AbortMultipartUpload"
Use these fields to distinguish credentials for different upload operations.

Create Uploader Object​

When creating a TXUploader, there are two modes: subApp and bucket, corresponding to the automatic mode and the specified bucket mode respectively.
The two modes correspond to the construction methods TXUploader.bucketMode and TXUploader.autoMode respectively. In addition, you also need to pass in your sub-application ID. The sample code is as follows:
_uploader = TXUploader.bucketMode(0000000000, "xxxxxxxxxxxxx");

Set Upload Callbacks​

After creation, if you need to monitor the upload status, you need to set up callbacks. All callbacks are optional for implementation, and the business can implement them according to its own needs. The sample code for setting up callbacks is as follows:
_uploader?.setUploadCallback = FTXUploadCallback(
successCallBack: (header, result) async {
// Upload success! Access file URL via result.accessUrl
},
failCallBack: (clientException, serviceException) async {
// Handle upload failure
},
progressCallBack: (complete, target) {
// Progress: 'complete' = bytes uploaded, 'target' = total bytes
},
stateCallBack:(state) {
// State change notification
},
startUploadCallback: (bucket, cosKey, uploadId) async {
// Upload started! Store 'uploadId' for resuming later
}
);

Start Uploading​

​Optional:​​ Configure upload settings before starting:
UploadConfig uploadConfig = UploadConfig();
uploadConfig.sliceSizeForUpload = 1024 * 1024 * 2; // Default: 2MB slice size (adjustable)
uploadConfig.isHttps = true; // Use HTTPS (default: true)
uploadConfig.enableVerification = true; // Enable slice verification (default: true)

Configure Upload Parameters​

UploadParams properties:
Parameter
Description
localFilePath
Local file path (required).
fileKey
Filename on server (required).
uploadId
Resume upload using previous ID (optional).
uploadConfig
Upload settings (optional).
​Start upload:​​
int? code = await _uploader?.upload(params);
if (code == TXUploadCode.TX_UPLOAD_OK) {
showResult("Upload started");
} else {
showResult("Upload failed with code $code");
}
Return Codes:​​
TXUploadCode.TX_UPLOAD_OK: Success.
TXUploadCode.TX_UPLOAD_BUSY: Uploader busy.
TX_FILE_NOT_FOUND: Invalid file path.

ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック