AngularFire2 - Firebase Storage getDownloadURL() - How to return Firestore's URL


ETX

I've been going through the angularfire2 documentation to retrieve downloadURl from storage. I hope something simple is missing here.

The documentation states:

@Component({
  selector: 'app-root',
  template: `<img [src]="profileUrl | async" />`
})
 export class AppComponent {
   profileUrl: Observable<string | null>;
   constructor(private storage: AngularFireStorage) {
   const ref = this.storage.ref('users/davideast.jpg');
   this.profileUrl = ref.getDownloadURL();
 }
}

However, once I have uploaded the image, I want to return the download url as a string to upload to firestore. I need download URL for external service.

my function

uploadImage(base64data) {

  const filePath = (`myURL/photo.jpg`);
  const storageRef = firebase.storage().ref();

  var metadata = {
    contentType: 'image',
    cacheControl: "public, max-age=31536000",
  };

  const ref = this.storage.ref(filePath);
  const task = ref.putString(base64data, 'data_url', metadata).then(() => {

    var downloadURL = ref.getDownloadURL();

  })

}

This uploads the image perfectly. However, I want to write the download URL to firestore. When the console logs my "downloadURL" variable, I get the following:

PromiseObservable {_isScalar: no, promise: y, scheduler: undefined}

The download is inside an observable promise. How can I get only the download URL string as a variable? Once there, I can sort out the Firestore update.

Haresh

This answer has nothing to do with Firebase 5.0 version, they removed downloadURL() from upload task. Please refer to the doc .

.downloadURL()When the upload is complete , the observable will emit the download URL string. Then you need to subscribe to get the value.

uploadImage(base64data) {

  const filePath = (`myURL/photo.jpg`);
  //const storageRef = firebase.storage().ref();

  var metadata = {
    contentType: 'image',
    cacheControl: "public, max-age=31536000",
  };

  const ref = this.storage.ref(filePath);
  const task = ref.putString(base64data, 'data_url', metadata);
  const downloadURL = task.downloadURL();

  downloadURL.subscribe(url=>{
     if(url){
         console.log(url);
         //wirte the url to firestore
     }
  })

}

Hope this helps. Check this blog for more details

Related


How to get URL getDownloadURL from Firebase Storage

Jonathan Fager: I'm trying to get "Long Persistent Download Links" for a file in a Firebase bucket. I have changed its permissions to service firebase.storage { match /b/project-xxx.appspot.com/o { match /{allPaths=**} { allow read, write; }

How to get URL getDownloadURL from Firebase Storage

Jonathan Fager I'm trying to get "Long Persistent Download Links" for a file in a Firebase bucket. I have changed its permissions to service firebase.storage { match /b/project-xxx.appspot.com/o { match /{allPaths=**} { allow read, write; } }

How to get URL getDownloadURL from Firebase Storage

Jonathan Fager: I'm trying to get "Long Persistent Download Links" for a file in a Firebase bucket. I have changed its permissions to service firebase.storage { match /b/project-xxx.appspot.com/o { match /{allPaths=**} { allow read, write; }

Angularfire2: How to retrieve file content from Firebase storage

Cupid I am developing an Ionic application using Angulfire2. I have successfully uploaded files to Firebase. I found that the method getDownloadURL retrieves the file URL and it displays fine in the HTML page. However, I can't find a way to download the conten

Angularfire2: How to retrieve file content from Firebase storage

Cupid I am developing an Ionic application using Angulfire2. I have successfully uploaded files to Firebase. I found that the method getDownloadURL retrieves the file URL and it displays fine in the HTML page. However, I can't find a way to download the conten

Angularfire2: How to retrieve file content from Firebase storage

Cupid I am developing an Ionic application using Angulfire2. I have successfully uploaded files to Firebase. I found that the method getDownloadURL retrieves the file URL and it displays fine in the HTML page. However, I can't find a way to download the conten

Angularfire2: How to retrieve file content from Firebase storage

Cupid I am developing an Ionic application using Angulfire2. I have successfully uploaded files to Firebase. I found that the method getDownloadURL retrieves the file URL and it displays fine in the HTML page. However, I can't find a way to download the conten

Angularfire2: How to retrieve file content from Firebase storage

Cupid I am developing an Ionic application using Angulfire2. I have successfully uploaded files to Firebase. I found that the method getDownloadURL retrieves the file URL and it displays fine in the HTML page. However, I can't find a way to download the conten

Firebase Storage's getDownloadUrl() method cannot be resolved

Neeraj Sewani To upload images to Firebase Storage, attach to a StorageReference instance . When I override the method, the instance I am calling , but it gives me an erroraddOnSuccessListeneronSuccessgetDownloadUrl()taskSnapshot Cannot resolve method getDownl

Firebase Storage's getDownloadUrl() method cannot be resolved

Neeraj Sewani To upload images to Firebase Storage, attach to a StorageReference instance . When I override the method, the instance I am calling , but it gives me an erroraddOnSuccessListeneronSuccessgetDownloadUrl()taskSnapshot Cannot resolve method getDownl

Firebase Storage's getDownloadUrl() method cannot be resolved

Neeraj Sewani To upload images to Firebase Storage, attach to a StorageReference instance . When I override the method, the instance I am calling , but it gives me an erroraddOnSuccessListeneronSuccessgetDownloadUrl()taskSnapshot Cannot resolve method getDownl

Unable to get URL from Firebase's getDownloadURL

Stern Shaw: I'm using Firebase storage to store images into it, the images are easily uploaded to the storage, but when I try to get the url of the image form Promise, it doesn't return any url. const imageSaveHandler = (e) => { e.preventDefault(); c

Unable to get URL from Firebase's getDownloadURL

Stern Shaw: I'm using Firebase storage to store images into it, the images are easily uploaded to the storage, but when I try to get the url of the image form Promise, it doesn't return any url. const imageSaveHandler = (e) => { e.preventDefault(); c

Unable to get URL from Firebase's getDownloadURL

Stern Shaw: I'm using firebase storage to store images into it, the images are easy to upload to the storage, but when I try to get the url of the image form Promise, it doesn't return any url. const imageSaveHandler = (e) => { e.preventDefault(); co

Flutter : getDownloadUrl (firebase storage)

killian I have a problem with my flutter application. I want to upload an image to firebase storage and get the URL but the getDownloadUrl method doesn't work. Here is my code : import 'package:firebase_storage/firebase_storage.dart' as firebase_storage; ...

Flutter : getDownloadUrl (firebase storage)

killian I have a problem with my flutter application. I want to upload an image to firebase storage and get the URL but the getDownloadUrl method doesn't work. Here is my code : import 'package:firebase_storage/firebase_storage.dart' as firebase_storage; ...

Flutter : getDownloadUrl (firebase storage)

killian I have a problem with my flutter application. I want to upload an image to firebase storage and get the URL but the getDownloadUrl method doesn't work. Here is my code : import 'package:firebase_storage/firebase_storage.dart' as firebase_storage; ...

Flutter : getDownloadUrl (firebase storage)

killian I have a problem with my flutter application. I want to upload an image to firebase storage and get the URL but the getDownloadUrl method doesn't work. Here is my code : import 'package:firebase_storage/firebase_storage.dart' as firebase_storage; ...

Token validity for Firebase Storage getDownloadUrl

Bumblebee I have a problem with the "token" in the url(&token=) when using FirebaseStorage's getDownloadUrl. https://firebasestorage.googleapis.com/v0/b/someapplication.appspot.com/o/images%2Fsample.png?alt=media&token=123456 It says to return a "long lived" d