使用flutter从Amazon S3下载文件

问题描述 投票:2回答:1

我没有太多的扑动经验,我想知道的是,如果有任何方式下载亚马逊s3的视频并将文件保存在手机的内存中,谢谢

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

amazon-s3 flutter
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.