简体   繁体   中英

Firebase cloud function: Error: EISDIR: illegal operation on a directory

I'm trying to download an image from an url and then uploading it to my firebase cloud storage. This is the code i'm using.

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


        });


      });

But from the firebase console (logs) I get this error:

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

What am I doing wrong?

Thanks in advance!

The path that you provide to the method upload should be a file and not a directory.

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

Upload a file to the bucket. This is a convenience method that wraps File#createWriteStream .

Example :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM