Download file from Amazon S3 with flutter

4,056

This is how I downloaded a binary file in flutter : In your pubspec.yaml, add

path_provider: ^0.2.2

includes :

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

code :

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;
}

The function consolidateHttpClientResponseBytes() comes from the flutter docs : https://docs.flutter.io/flutter/foundation/consolidateHttpClientResponseBytes.html

Note: Edited to more concise version from Simon Lightfoot

Share:
4,056
Mauricio Ramos
Author by

Mauricio Ramos

Updated on December 04, 2022

Comments

  • Mauricio Ramos
    Mauricio Ramos over 1 year

    I do not have much experience with flutter, what I would like to know is if there is any way to download a video of amazon s3 and save the file in the memory of the cell phone, thanks

    This is the url of the video "https://s3-us-west-1.amazonaws.com/videos.ecuestre.digital/165-3745-40957-1.mp4"

    • alltooconfusingthereforesleep
      alltooconfusingthereforesleep about 6 years
      Did you get an answer? If you did, please write your answer down. If not, you could ask this question a little more open. The answer should be the same for any video file from url.
  • Ayush Malviya
    Ayush Malviya almost 4 years
    Hey, I have to make my bucket public to download via url right?