4

I'm trying to download some images that I have uploaded to my Google Cloud Storage (aka into buckets). I'm unable to use the .ref() method on any of my const storage or const bucket because they are part of the admin SDK. The admin.storage has only the method .bucket() (https://firebase.google.com/docs/reference/admin/node/admin.storage.Storage).

I'm able to access the buckets. bucket.getFiles() works, and the result comes out to be an Array of Files objects with a ton of metadata (like file name, bucket owner, etc.). How can I get my images from the cloud storage and insert them into html objects?

var admin = require("firebase-admin");

var serviceAccount = require("./randomDB-f12d3-admin-correctly-working.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://randomDB-f12d3.firebaseio.com",
  storageBucket: "randomDB-f12d3.appspot.com"
});

const gcs  = require("@google-cloud/storage");
gcs.projectId = "randomDB-f12d3";
gcs.keyFilename = "randomDB-f12d3-firebase-admin-correctly-working.json";

exports.getFile = functions.https.onRequest((req, res) => {
  
  cors(req, res, () => {
    if (req.method !== "GET") {
      return res.status(500).json({
        message: "Not allowed"
      });
    }

    const storage = admin.storage();
    const bucket = admin.storage().bucket();

    bucket.getFiles().then((result)=>{
      
      console.log(result);
      res.send(result);
    });

  });

});
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
shaotime
  • 39
  • 1
  • 3
  • You can use .file() to get a specific file. See the [GCS client API documentation](https://cloud.google.com/nodejs/docs/reference/storage/2.0.x/File) as well as the [Firebase samples repo](https://github.com/firebase/functions-samples/blob/Node-8/generate-thumbnail/functions/index.js) for examples – Jen Person Dec 03 '18 at 18:16

1 Answers1

7

The Admin SDK for Cloud Storage is just a wrapper around the @google-cloud/storage module. When you call admin.storage(), what you're getting back is a Storage object from that library. With admin.storage().bucket(), you're getting the default storage Bucket for your project. From there, you should use file() to create a reference to files in that bucket and download them as needed.

Sebastien F.
  • 1,613
  • 2
  • 23
  • 40
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I'm trying to call storage.ref() to make a reference to the file I want to download. However, I'm getting an error saying that .ref() is not a method for storage. Why is this happening? I'm not able to do anything with the storage object... – shaotime Dec 05 '18 at 00:41
  • That's because there is no ref() with the Cloud Storage API. Please read my answer again, and click the links to view the API documentatio. – Doug Stevenson Dec 05 '18 at 00:43
  • 1
    Answer has broken links in it. Side to that it does not answer the question. – David Ritchie Jul 15 '21 at 13:10
  • 4
    The "download as needed" part is where this answer comes up short. You should elaborate as it is the core part of the question. – paul_f Jul 31 '21 at 09:06