Spencer
Spencer

Reputation: 115

How can i optimize simultaneous uploads of images and videos to Firebase Storage and Firestore?

I am trying to optimize the process of uploading images and videos to Firebase Storage and Firestore simultaneously. Currently, I am using a loop to conditionally check each asset with separate methods for images and videos. I also utilize a WriteBatch, but i am facing several issues:

I'd like to accomplish these tasks quickly:

  1. Image/video compression with VideoCompress and FlutterImageCompress
  2. Upload to FirebaseStorage
  3. Retrieve Download URLs
  4. Create Asset objects to save as documents to Firestore
List<AssetEntity> assetsList = [];
WriteBatch batch = FirebaseFirestore.instance.batch();
int totalFiles = assets.length;
int uploadedFiles = 0;

for (final asset in assets) {
 late Uint8List? compressedThumbnailData;
 late Uint8List? compressedData;
 late String contentType;
 late String filename;
 late String downloadUrl;
 late String thumbnailUrl;

 if (asset.type == AssetType.video) {} else {}
 // conditionally check if asset is video or image, compress it, then assign it
 // a contentType, docId, filename, and the compressedData

 downloadUrl = await mediaServices.uploadImage(
  image: compressedData,
  type: contentType,
  path: bucketPath,
  filename: filename,
 );

 GeoPoint? location = await getAssetGeoPoint(asset);

 final asset = Asset(
  ... assign all of the models parameters 
 );

 assetsList.add(asset);

 final assetDocRef = _firestore.collection("").doc(docId);

 batch.set(assetDocRef, asset.toMap());

 uploadedFiles++;
 
}

await batch.commit();

Upvotes: 0

Views: 51

Answers (1)

HerPat
HerPat

Reputation: 403

I agree with @Doug Stevenson. The upload speed is determined by various factors, including the protocol used, available bandwidth, network latency, and the size and quality of the images or videos being uploaded.

For Cloud Storage for Firebase location, you must consider choosing the location of your bucket close to you to reduce latency and increase availability. (This post might give you insights)

If you are uploading using Firebase Client SDKs, the upload speed is slower compared to using gsutil. This is because, unlike the direct communication between gsutil and Cloud Storage, the SDKs must evaluate Security Rules, which introduces latency due to the time spent on these evaluations.

You can also try reducing the size and quality of the images or videos, as well as increasing your internet speed. (see this post)

For ways to track upload progress and other useful methods, check out these blogs :

  1. 5 tips for Firebase Storage

  2. Uploading Images, Videos and files to an API in Flutter: Best Practices and Methods

  3. Firebase Cloud Storage

I hope the above helps.

Upvotes: 0

Related Questions