简体   繁体   中英

ENOENT no such file or directory error in cloud function upload image

I'm new to cloud functions and working on creating a function that (among other things) uploads an image from Flutter image_picker to cloud storage. Below is the relevant cloud fn code:

  // upload image to storage
        const bucket = admin.storage().bucket();
        const destination = data.storageCollection + '/' + data.itemID;

      
        const filePath = data.filePath;

        console.log('filePath is ' + data.filePath);

        await bucket.upload(filePath, {
          destination: destination,
          gzip: true,
        });

I'm getting this error:

functions error in uploadProductPic: [firebase_functions/internal] project: undefined. Function: uploadImage. Error: Error: ENOENT: no such file or directory, open '/data/user/0/com.appIdentifier.here/cache/a2b21481-ab56-4af5-bc97-4c2d050107e12579524263120800910.jpg'

Maybe I'm just bad at storage directories, but I'm pretty sure that's the absolute path. I've also tried other combinations:

'data/user/0/com.appIdentifier.here/cache/a2b21481-ab56-4af5-bc97-4c2d050107e12579524263120800910.jpg'
'cache/a2b21481-ab56-4af5-bc97-4c2d050107e12579524263120800910.jpg'
'/cache/a2b21481-ab56-4af5-bc97-4c2d050107e12579524263120800910.jpg'

And nothing has worked. Can someone tell my why my path is wrong here? I'm just getting it from the.path property of an XFile. I've found some similar stack posts but haven't yet found a solution.

Edit: Here's my clientside code

  Future<void> uploadProductPic(String productID, XFile file) async {
try {
  await functions.httpsCallable('uploadImage').call(<String, String>{
    "filePath": file.path,
    "itemID": productID,
    "storageCollection": "productPic",
    "itemCollection": "products"
  });
} catch (e) {error catching stuff}

And here's my full cloud function:

/**
 *
 * @param {string} filePath
 * @param {string} itemID
 * @param {string} storageCollection
 * @param {string} itemCollection
 */

exports.uploadImage = functions.region("australia-southeast1")
    .https.onCall(async (data, context) => {
 
  // Only allow authorised users to execute this function.
  if (!(context.auth && context.auth.token)) {
    throw new functions.https.HttpsError(
        "permission-denied",
        "Must be an administrative user to upload an image."
    );
  }

  try {

    // upload image to storage
    const bucket = admin.storage().bucket();
    const destination = data.storageCollection + '/' + data.itemID;

    const filePath = data.filePath;
   
    await bucket.upload(filePath, {
      destination: destination,
      gzip: true,
    });

    // get download url
    const url = await bucket.getDownloadURL(destination);

 // upload url to database
        const firebaseRef = data.itemCollection + '/' + data.itemID;
        await firebaseTools.firestore.updateDoc(firebaseRef, {'imageURL': url});

  } catch (err) {
    throw new functions.https.HttpsError("internal", "project: " +
  process.env.GCP_PROJECT +
  ". Function: uploadImage. Error: " + String(err));
  }
}
);

Thank you for the help!

Cloud Functions run on a fully remote machine in the cloud, not on the device where your app is installed. It simply cannot access local files on the device.

If you want to upload a file from the app, you should use the Firebase Storage SDK for your app's platform, and not Cloud Functions.

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