flutter_aly_oss 0.0.4 copy "flutter_aly_oss: ^0.0.4" to clipboard
flutter_aly_oss: ^0.0.4 copied to clipboard

aliyun OSS plugin.

example/lib/main.dart

import 'dart:io';
import 'dart:math';

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:image_picker/image_picker.dart';
import 'package:flutter/services.dart';
import 'package:flutter_aly_oss/flutter_aly_oss.dart';
import 'package:uuid/uuid.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // 访问的endpoint地址
  final String OSS_ENDPOINT = "http://oss-cn-shanghai.aliyuncs.com";

  //callback 测试地址
  final String OSS_CALLBACK_URL = "http://oss-demo.aliyuncs.com:23450";

  // STS 鉴权服务器地址,使用前请参照文档 https://help.aliyun.com/document_detail/31920.html 介绍配置STS 鉴权服务器地址。
  // 或者根据工程sts_local_server目录中本地鉴权服务脚本代码启动本地STS 鉴权服务器。详情参见sts_local_server 中的脚本内容。
  final String STS_SERVER_URL =
      "*************"; //STS 地址
  final String BUCKET_NAME = "*************";
  final String OSS_ACCESS_KEY_ID = "*************";
  final String OSS_ACCESS_KEY_SECRET = "*************";
  String text = '';
  final _uuid = Uuid();

  @override
  void initState() {
    super.initState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  init() async {
    final FlutterAlyOss _alyOss = FlutterAlyOss();
    var result = await _alyOss
        .init(InitRequest(_uuid.v4(), STS_SERVER_URL, BUCKET_NAME));
    // StreamSubscription streamSubscription1 = _alyOss.onProgress.listen((event) {
    //   print('_alyOss.onProgress');
    // }, onDone: () {
    //   print('_alyOss.onProgress_done');
    // });
    // StreamSubscription streamSubscription2 = _alyOss.onUpload.listen((event) {
    //   print('_alyOss.onUpload');
    // }, onDone: () {
    //   print('_alyOss.onUpload_done');
    // });
  }

  Future pickerAndUpload(
      {ImageSource type: ImageSource.gallery,
      String key: 'attachment/'}) async {
    File _image;
    if (type == ImageSource.gallery) {
      final pickedFile =
          await ImagePicker().getImage(source: ImageSource.gallery);
      _image = File(pickedFile.path);
    } else if (type == ImageSource.camera) {
      final pickedFile =
          await ImagePicker().getImage(source: ImageSource.camera);
      _image = File(pickedFile.path);
    } else {}
    print('pickerAndUpload');
    return await upload(_image, key);
  }

  Future upload(File image, [String key = 'attachment/']) async {
    if (image != null) {
      String str =
          '${DateTime.now().millisecondsSinceEpoch}${getRandomAlphanumeric()}.jpg';
      print('str:${image.absolute.path}');
      print('str:$str');
      setState(() {
        text = 'upload';
      });
      var res = await FlutterAlyOss().upload(UploadRequest(_uuid.v4(),
          'future-lenoir-bucket', '${key}${str}', image.absolute.path));
      setState(() {
        text = res.toString();
      });
      return Future.value({'image': ''});
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              TextButton(
                  onPressed: () {
                    init();
                  },
                  child: Text('setup(初始化)', style: TextStyle(fontSize: 18.0))),
              TextButton(
                  onPressed: () {
                    pickerAndUpload();
                  },
                  child: Text('上传图片)', style: TextStyle(fontSize: 18.0))),
              Text(text)
            ],
          ),
        ),
      ),
    );
  }

  static String getRandomAlphanumeric({strLength = 6}) {
    String alphabet =
        '0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
    String left = '';
    for (var i = 0; i < strLength; i++) {
//    right = right + (min + (Random().nextInt(max - min))).toString();
      left = left + alphabet[Random().nextInt(alphabet.length)];
    }
    return left;
  }
}
0
likes
30
pub points
0%
popularity

Publisher

unverified uploader

aliyun OSS plugin.

Homepage

License

MIT (LICENSE)

Dependencies

flutter, uuid

More

Packages that depend on flutter_aly_oss