8
votes

I have to upload an image to the firebase storage. I'm not using the web version of storage (I shouldn't use it). I am using the firebase admin. No problem, I upload the file without difficulty and I get the result in the variable "file".

and if I access the firebase storage console, the image is there. all right.

return admin.storage().bucket().upload(filePath, {destination: 'demo/images/restaurantCover.jpg', 
                                                            metadata:{contentType: 'image/jpeg'}
                                                            public: true
                                                           }).then(file =>{
            console.log(`file --> ${JSON.stringify(file, null, 2)}`);
            let url = file["0"].metadata.mediaLink; // image url
            return resolve(res.status(200).send({data:file})); // huge data
        }) ;

Now, I have some questions.

  1. Why so much information and so many objects as a response to the upload () method? Reviewing the immense object, I found a property called mediaLink inside metadata and it is the download url of the image. but...

  2. Why is the url different from the one shown by firebase? Why can not I find the downloadURL property?

  3. How can get the url of firebase?

firebase: https://firebasestorage.googleapis.com/v0/b/myfirebaseapp.appspot.com/o/demo%2Fimages%2Fthumb_restaurant.jpg?alt=media&token=bee96b71-2094-4492-96aa-87469363dd2e

mediaLink: https://www.googleapis.com/download/storage/v1/b/myfirebaseapp.appspot.com/o/demo%2Fimages%2Frestaurant.jpg?generation=1530193601730593&alt=media

  1. If I use the mediaLink url is there any problem with different urls? (read, update from ios and Web Client)
1
Did you ever found a solution to this ? I am using the same setup (firebase admin) with nodejs and currently storing the mediaLink URL in mongodb as reference.Pascal Lamers
@PascalLamers not yetmakitocode
I am having this problem as well, I think it has something to do with the fact that the admin api overlaps with google cloud. The callback relates to the storage system in google cloud which I don't have set up and returns nothing about firebase :/. For now I am combing the firebase link and using a template literal to fill in the relevant information. With the 'destination' in the template you have to replace forward slashes with '%2F'. I have ignored the token query as I don't think it has any effect on a public object. Wish firebase would fix this up to be more like the client sdk :(kaleidawave

1 Answers

-1
votes

Looking at Google Cloud Storage: Node.js Client documentation, they have a link to sample code which shows exactly how to do this. Also, see the File class documentation example (below)

// Imports the Google Cloud client library
const Storage = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const filename = 'File to access, e.g. file.txt';

// Gets the metadata for the file
storage
  .bucket(bucketName)
  .file(filename)
  .getMetadata()
  .then(results => {
    const metadata = results[0];

    console.log(`File: ${metadata.name}`);
    console.log(`Bucket: ${metadata.bucket}`);
    console.log(`Storage class: ${metadata.storageClass}`);
    console.log(`Self link: ${metadata.selfLink}`);
    console.log(`ID: ${metadata.id}`);
    console.log(`Size: ${metadata.size}`);
    console.log(`Updated: ${metadata.updated}`);
    console.log(`Generation: ${metadata.generation}`);
    console.log(`Metageneration: ${metadata.metageneration}`);
    console.log(`Etag: ${metadata.etag}`);
    console.log(`Owner: ${metadata.owner}`);
    console.log(`Component count: ${metadata.component_count}`);
    console.log(`Crc32c: ${metadata.crc32c}`);
    console.log(`md5Hash: ${metadata.md5Hash}`);
    console.log(`Cache-control: ${metadata.cacheControl}`);
    console.log(`Content-type: ${metadata.contentType}`);
    console.log(`Content-disposition: ${metadata.contentDisposition}`);
    console.log(`Content-encoding: ${metadata.contentEncoding}`);
    console.log(`Content-language: ${metadata.contentLanguage}`);
    console.log(`Metadata: ${metadata.metadata}`);
    console.log(`Media link: ${metadata.mediaLink}`);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });