refFromURL method

Reference refFromURL(
  1. String url
)

Returns a new Reference from a given URL.

The url can either be a HTTP or Google Storage URL pointing to an object. If the URL contains a storage bucket which is different to the current FirebaseStorage.bucket, a new FirebaseStorage instance for the Reference will be used instead.

Implementation

Reference refFromURL(String url) {
  assert(
    url.startsWith('gs://') || url.startsWith('http'),
    "'a url must start with 'gs://' or 'https://'",
  );

  String? bucket;
  String? path;

  if (url.startsWith('http')) {
    final parts = partsFromHttpUrl(url);

    assert(
      parts != null,
      "url could not be parsed, ensure it's a valid storage url",
    );

    bucket = parts!['bucket'];
    path = parts['path'];
  } else {
    bucket = bucketFromGoogleStorageUrl(url);
    path = pathFromGoogleStorageUrl(url);
  }

  return FirebaseStorage.instanceFor(
    app: app,
    bucket: 'gs://$bucket',
  ).ref(path);
}