Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

284
Visualizações
Can not get url image storage firebase [ Error creating access token]

enter image description here

I tried uploading the image to firebase and now I can't get the image URL

I uploaded the image successfully and can't get the image's URL link

   const firebaseAdmin = require('firebase-admin');
const { v4: uuidv4 } = require('uuid');
// change the path of json file
const serviceAccount = require('./nodejs-be-upload-image-firebase-adminsdk-vzkga-bd1696a09f.json');

const admin = firebaseAdmin.initializeApp({
  credential: firebaseAdmin.credential.cert(serviceAccount),
});
const storageRef = admin.storage().bucket('gs://nodejs-be-upload-image.appspot.com');

async function uploadFile(path, filename) {
  // Upload the File
  const storage = await storageRef.upload(path, {
    public: true,
    destination: `/uploads/${filename}`,
    metadata: {
      firebaseStorageDownloadTokens: uuidv4(),
      cacheControl: 'public, max-age=315360000',
      contentType: 'image/jpeg',
    },
  });

 //I want to get the URL of the image here
    return storage[0];
//  return storage[0].metadata.mediaLink;
}
module.exports = { uploadFile };
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The Firebase Admin SDK wraps the Google Cloud Storage SDK, so their APIs are the same. The Cloud Storage SDK doesn't offer download URLs that are exactly like the ones provided by the mobile and web SDKs.

By setting the image or file as public (public: true) you wont be able to set a custom firebaseStorageDownloadTokens metadata.


There are some ways that you can get the public URL of an uploaded file.

Using getSignedUrl:

const admin = firebaseAdmin.initializeApp({
  credential: firebaseAdmin.credential.cert({
    // serviceAccount
  }),
});

const gs = 'gs://'
const bucketName = '<project-id>.appspot.com';
const storageRef = admin.storage().bucket(`${gs}${bucketName}`);

async function uploadFile(path, filename) {
  // Upload the File
  const destination = `uploads/${filename}`;

  const storage = await storageRef.upload(path, {
    public: true,
    destination: destination,
  })
  .then(() => {
    const file = storageRef.file(destination);
    return file.getSignedUrl({
      action: 'read',
      expires: '03-25-2023'
    }).then(signedUrls => {
      // signedUrls[0] contains the file's public URL
      console.log(signedUrls[0]);
    });
  });
}

Using the Cloud Storage link:

async function uploadFile(path, filename) {
  // Upload the File
  const destination = `uploads/${filename}`;

  const storage = await storageRef.upload(path, {
    public: true,
    destination: destination,
  })
  .then(() => {
  console.log(`https://storage.googleapis.com/${bucketName}/${destination}`);
  });
}

If you really do want to use customMetadata that has the firebaseStorageDownloadTokens then I would suggest to use the Web SDKs instead. Here's the documentation to start:

  • Get started with Cloud Storage on Web.
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda