【问题标题】:Firebase cloud function: Error: EISDIR: illegal operation on a directoryFirebase云功能:错误:EISDIR:对目录的非法操作
【发布时间】:2020-04-12 21:15:50
【问题描述】:

我正在尝试从 url 下载图像,然后将其上传到我的 firebase 云存储。 这是我正在使用的代码。

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';    
const download = require('image-downloader');
const tmp = require('tmp');

export const downloadFunction = functions.https.onCall(async (data, context) => {
        var bucket = admin.storage().bucket();


      await  tmp.dir(async function _tempDirCreated(err: any, path: any) {
          if (err) throw err;

          const options = {
            url: 'theUrlIWantToPutInTheStorage',
            dest: path,
          }
          console.log('Dir: ', path);

          await download.image(options)
          .then(async () => {
            console.log('Saved');

            await bucket.upload(path, {
              destination: "testfolder/test.jpg",
              metadata: "metadata",
            });

          })
          .catch((err2: any) => console.error(err2))


        });


      });

但是从 firebase 控制台(日志)我得到这个错误:

    { Error: EISDIR: illegal operation on a directory, read errno: -21, code: 'EISDIR', syscall: 'read' }

我做错了什么?

提前致谢!

【问题讨论】:

  • 这个console.log('Saved')出现在日志中了吗?
  • “testfolder/test.jpg”是目录吗?

标签: javascript node.js typescript firebase google-cloud-functions


【解决方案1】:

您提供给方法uploadpath 应该是文件而不是目录。

upload(pathString, optionsopt, callbackopt) → {Promise.<UploadResponse>}

将文件上传到存储桶。这是包装File#createWriteStream 的便捷方法。

例子:

const options = {
  destination: 'new-image.png',
  resumable: true,
  validation: 'crc32c',
  metadata: {
    metadata: {
      event: 'Fall trip to the zoo'
    }
  }
};

bucket.upload('local-image.png', options, function(err, file) {
  // Your bucket now contains:
  // - "new-image.png" (with the contents of `local-image.png')

  // `file` is an instance of a File object that refers to your new file.
});

https://googleapis.dev/nodejs/storage/latest/Bucket.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-13
    • 2020-07-25
    • 2021-06-20
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    相关资源
    最近更新 更多