Fiaz Ali
Fiaz Ali

Reputation: 143

Downloading Files from Firebase Storage is very slow

Scenario : I am downloading image/video files from firebase storage and displaying them in a list.

Issue : The response is pretty slow. It is taking way too much time to download images/videos.

CODE :

Directory tempDir = await getTemporaryDirectory();
final File tempFile = File('${tempDir.path}/$fileName');
if(tempFile.existsSync()){
  await tempFile.delete();
}

await tempFile.create();
var ref = FirebaseStorage.instance.ref(path); 
// final DownloadTask task = ref.writeToFile(tempFile);
final DownloadTask task = ref.writeToFile(tempFile);

await task.then((value) => {
  file = tempFile
  //Also delete the temp file
  // await tempFile.delete();
});

RESULT : The files gets download successfully but response is way too much long. Any suggestions on how to make downloads faster. I also tested the response on real device with a release apk but still way too much slow downloading from Firebase Storage. I am using internet connection of 20MBPS so it's pretty decent speed to download data. Also, the uploading of files to firebase storage is pretty decent and i haven't found any issues there.

NOTE : I am currently using Firebase Free plan for development. Is that affecting the download response ? Really need some experts advice here.

IMAGES ON FIREBASE Images on Firebase

VIDEOS ON FIREBASE enter image description here

Upvotes: 1

Views: 3149

Answers (2)

Fiaz Ali
Fiaz Ali

Reputation: 143

I managed to reduce the download time to access files from Firebase Storage. Here are some of the steps i did :

1 - All Firebase projects are basically Google Cloud projects. Didn't know about that as i am new to Firebase and server-side environment. Understanding Firebase Projects

2- I have found an answer regarding fast access from firebase storage bucket which is basically a google cloud bucket. Firebase Storage very slow compared to Firebase Hosting

The solution posted by Zywy. Aside form making the bucket public, he mentioned that you need to install Google Cloud SDk to make files(which are going to be uploaded to the bucket) public. I think that is the case for native android development and not for Flutter. I didn't installed the Cloud SDK. I just make the bucket public and all the files that are then uploaded to the bucket are then also public. Correct me if i am wrong here.

So basically what i did was to make my bucket puclic by default. Here is a link on how to do that : Making Data Public

After making the bucket public, the image files which were taking 150-200 ms to download get reduced to 10-30 ms(millliseconds).

Anybody who is new to Firebase like me and stuck in the same situation should follow these steps.

Also, i am still learning about Firebase so if anyone has some expert advice on making this answer better or if i am wrong about anything about this issue then let me and whole community know about this.

Also, i am still searching about the security issue on making the bucket public. Is it harmful or not? So, if any expert out there on Cloud Platform security then let us know.

Thanks to Robert Sandberg for the early response on this issue.

Upvotes: 2

Robert Sandberg
Robert Sandberg

Reputation: 8607

First an answer your specific question: No, Firebase Spark plan won't affect download time.

It's hard to write a good answer for this very broad question. But based on the answers you provided in the comment... here goes:

If you are sitting in a different region than the US (which you had your bucket in), that will of course impact the download time. So a suggestion is to make sure that your storage is in the region where you have most users, or implement load balancing.

Have you timestamped the actual download time? Narrowing down whether or not it is that part that actually does the downloading that takes time, or some other part of the code? So just before you start the actual download, and then right after it is finished, not including other parts. That way you can isolate where a lot of time is. Check it with something like this:

final before = DateTime.now();
<... specific code that does the downloading>
final difference = DateTime.now().difference(before);

Then another thing to take into consideration is how you implement the fetching of each image. If you know that you will download 10 images. Instead of doing it synchronously one after another, consider doing that fetching asynchronously and waiting for the slowest. Perhaps something like this:

final allImages = await Future.wait([
  downloadImage(1),
  downloadImage(2),
  downloadImage(3),
  ...
]);

Upvotes: 1

Related Questions