aliyun_oss_plugin 0.0.6 copy "aliyun_oss_plugin: ^0.0.6" to clipboard
aliyun_oss_plugin: ^0.0.6 copied to clipboard

Flutter plugin for aliyun oss.

example/lib/main.dart

import 'package:aliyun_oss_plugin_example/file_util.dart';
import 'package:flutter/material.dart';
import 'package:aliyun_oss_plugin/aliyun_oss_plugin.dart' as AliyunOSSPlugin;
import 'package:aliyun_oss_platform_interface/aliyun_oss_platform_interface.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:uuid/uuid.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  dynamic initResult;
  double? progress;
  String? error;
  bool pause = false;
  String objectKey = "";
  @override
  void initState() {
    super.initState();
    initOSS();

    AliyunOSSPlugin.addUploadMethodCallListener(UploadMethodCallListener(
        init: (initData) {},
        progress: (progressData) {
          progress = progressData.totalBytesSent /
              progressData.totalBytesExpectedToSend;
          setState(() {});
        },
        success: (successData) {
          objectKey = "";
        },
        fail: (failData) {
          // error = failData.error;
          setState(() {});
        }));
  }

  initOSS() async {
    initResult = await AliyunOSSPlugin.initialize(
      'endpoint',
      '',
      // OSSStsTokenCredentialProvider(
      //     accessKeyId: stsAccessKeyId,
      //     secretKeyId: stsAccessKeySecret,
      //     securityToken: stsSecurityToken),
      OSSPlainTextCredentialProvider(
        accessKeyId: 'accessKeyId',
        secretKeyId: 'secretKeyId',
      ),
    );
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: Center(
            child: Text(error != null
                ? "上传错误: $error"
                : 'Running on: $initResult, progress: $progress'),
          ),
          floatingActionButton: Column(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              FloatingActionButton(
                onPressed: () async {
                  // runPlayground();
                  await Permission.storage.request();
                  // AliyunOSSPlugin.upload(bucketName);
                  pickFile(context, (filePath, name) {
                    String uuid = const Uuid().v4();
                    AliyunOSSPlugin.upload('', filePath, uuid, uuid);
                  });
                },
                child: const Icon(Icons.send),
              ),
              const SizedBox(
                height: 10,
              ),
              FloatingActionButton(
                  onPressed: () async {
                    final result = pause
                        ? await AliyunOSSPlugin.upload('', objectKey, '', '')
                        : await AliyunOSSPlugin.cancelUpload(objectKey);
                    if (result != null) {
                      setState(() {
                        pause = !pause;
                      });
                    }
                  },
                  child: pause ? Icon(Icons.play_arrow) : Icon(Icons.pause)),
            ],
          )),
    );
  }
}