使用flutter从Amazon S3下载文件

时间:2018-03-06 18:18:41

标签: amazon-s3 flutter

我没有太多关于颤动的经验,我想知道的是有没有办法下载亚马逊s3的视频并将文件保存在手机的内存中,谢谢

这是视频的网址" https://s3-us-west-1.amazonaws.com/videos.ecuestre.digital/165-3745-40957-1.mp4"

1 个答案:

答案 0 :(得分:4)

这就是我在flutter中下载二进制文件的方法: 在pubspec.yaml中,添加

path_provider: ^0.2.2

包括:

import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';

代码:

static var httpClient = new HttpClient();
Future<File> _downloadFile(String url, String filename) async {
  var request = await httpClient.getUrl(Uri.parse(url));
  var response = await request.close();
  var bytes = await consolidateHttpClientResponseBytes(response);
  String dir = (await getApplicationDocumentsDirectory()).path;
  File file = new File('$dir/$filename');
  await file.writeAsBytes(bytes);
  return file;
}

函数consolidateHttpClientResponseBytes()来自flutter docs: https://docs.flutter.io/flutter/foundation/consolidateHttpClientResponseBytes.html

注意:已编辑为more concise version from Simon Lightfoot