2

I have successfully uploaded files to Firebase's storage via Google Cloud Storage through JS! What I noticed is that unlike files uploaded directly, the files uploaded through Google Cloud only have a Storage Location URL, which isn't a full URL, which means it cannot be read! I'm wondering if there is a way to generate a full URL on upload for the "Download URL" part of Firebase's actual storage.

enter image description here

Code being used:

    var filename = image.substring(image.lastIndexOf("/") + 1).split("?")[0];
    var gcs = gcloud.storage();
     var bucket = gcs.bucket('bucket-name-here.appspot.com');

     request(image).pipe(bucket.file('photos/' + filename).createWriteStream(
     {metadata: {contentType: 'image/jpeg'}}))
      .on('error', function(err) {})
    .on('finish', function() {



     console.log(imagealt);    


  });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
HalesEnchanted
  • 615
  • 2
  • 9
  • 20

2 Answers2

4

When using the GCloud client, you want to use getSignedUrl() to download the file, like so:

bucket.file('photos/' + filename).getSignedUrl({
  action: 'read',
  expires: '03-17-2025'
}, function(err, url) {
  if (err) {
    console.error(err);
    return;
  }

  // The file is now available to read from this URL.
  request(url, function(err, resp) {
    // resp.statusCode = 200
  });
});
Andreas Grimm
  • 195
  • 1
  • 7
Mike McDonald
  • 15,609
  • 2
  • 46
  • 49
  • 1
    Hello, thank you Mike! How do I combine it with createWriteStream or should it a code on it's own? – HalesEnchanted Jun 16 '16 at 05:08
  • Within the `on('finish', function() {/* make the call to get the download URL */});` is a likely place, since it's uploaded properly at that point. – Mike McDonald Jun 16 '16 at 06:24
  • Are you a star or are you a star! Thank you very much :) The link still doesn't show up in Firebase's storage but it returns a working google cloud URL! – HalesEnchanted Jun 16 '16 at 20:30
  • Not a problem. The link doesn't show up because it's actually creating a different URL--these ones expire after a certain amount of time, so they're slightly different. – Mike McDonald Jun 16 '16 at 20:36
2

You can either:

a) Create a download url through the firebase console

create new download link

b) if you attempt to get the downloadurl programmatically from a firebase client, one will be created on the fly for you.

Benjamin Wulfe
  • 1,705
  • 13
  • 9
  • Hey, thank you! The first one just creates a token? It isn't a full URL :( The second one sounds about right, how do I go about it? – HalesEnchanted Jun 15 '16 at 19:55
  • the first one should create a "full url" creating a token is what makes that possible. What do you see that suggests that its not a download url and only the token? – Benjamin Wulfe Jun 16 '16 at 13:49
  • the second one requires you use the firebase storage sdk. See the [getting started guides](https://firebase.google.com/docs/storage/web/download-files) for more info. – Benjamin Wulfe Jun 16 '16 at 13:51