编程语言
首页 > 编程语言> > javascript – 获取图片网址Firebase存储(管理员)

javascript – 获取图片网址Firebase存储(管理员)

作者:互联网

我必须将图像上传到firebase存储.我没有使用网络版的存储空间(我不应该使用它).我正在使用firebase管理员.
没问题,我上传文件没有困难,我得到变量“文件”中的结果.

如果我访问firebase存储控制台,图像就在那里.行.

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
        }) ;

现在,我有一些问题.

>为什么这么多信息和这么多对象作为对upload()方法的响应?回顾这个巨大的对象,我在元数据中找到了一个名为mediaLink的属性,它是图像的下载URL.但…
>为什么网址与firebase显示的网址不同?为什么我找不到downloadURL属性?
>如何获得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

>如果我使用mediaLink网址是否有任何问题与不同的网址? (阅读,从ios和Web客户端更新)

解决方法:

查看Google Cloud Storage: Node.js Client文档,它们有一个指向sample code的链接,该链接显示了如何执行此操作.另请参阅File class documentation示例(如下)

// 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);
  });

标签:javascript,firebase,google-cloud-storage,firebase-storage
来源: https://codeday.me/bug/20190910/1801743.html