"Cloud function encounters an ENOENT error, indicating that the file or directory does not exist while

Just starting out with cloud functions and figuring out how to upload an image from Flutter image_picker to cloud storage. Sharing the relevant cloud function code below:

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

Encountering this error message:

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

Seems like I'm struggling with the correct storage directory path. Have tried various combinations without success:

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

Any ideas on where I might be going wrong with the file path? Currently using the .path property of an XFile.

Edit: Including my clientside code here:

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

Appreciate any advice or guidance!

Answer №1

Cloud Functions operate on a completely remote server in the cloud, rather than the device your app is running on. As a result, they are unable to access files stored locally on the device.

If you need to upload a file from the app, it's recommended to utilize the Firebase Storage SDK designed for your app's specific platform instead of relying on Cloud Functions.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Accessing 'this' within a Firebase callback function

As I work on my Vue.js application, I encounter an issue with fetching data from Firebase when the component mounts. While I am able to successfully retrieve the data, I am struggling to write it into a Vue.js data property. I have tried using this.propert ...

Supporting right-to-left (RTL) localization in Angular 2 and later versions

When it comes to incorporating right-to-left (RTL) support into a localized Angular 2+ application, particularly for languages like Hebrew and Arabic, what is considered the best approach? I have explored various tutorials, including Internationalization ...

Pause and anticipate a request in MongoDB

I am facing an issue with my Typescript async method used to query a MongoDB database using the nodejs driver. The compiler seems to be indicating that the "await" before "myConnectedClient" has no effect on the type of this expression. This has left me co ...

The element is inferred to have an 'any' type due to the inability to use a 'string' type expression to index the 'Palette' type

Encountering an issue: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Palette'. No index signature with a parameter of type 'string' was found on type &ap ...

Improve your typing accuracy by enforcing strict null checks

I'm currently looking to enable strict null checks in my TypeScript 2.0 project, but I'm encountering some challenges with the typings of a dependency that is nested within another dependency (referred to as the dependency grandparent). To provi ...

Error: The element 'scrollable' is not recognized in Angular2

I recently updated my angular2 project to the final release after previously using angular2 RC5 for development. However, I encountered an error message stating "scrollable is not a known element." If I change <scrollable><!--- angular code -- ...

What is the method for expanding an object type using a string literal type?

I am encountering an issue with the code snippet below. In this code, type B is meant to extend type A by expanding the acceptable values for the property type. However, despite this intention, the assignment is resulting in a type error. type A = { ...

Using Promise.all like Promise.allSettled

I am looking to streamline the handling of Promise.allSettled in a more generic way. Currently when using allSettled, it returns a list of both fulfilled and rejected results. I find this cumbersome and do not want to handle it everywhere in my code. My g ...

How come the splice method is changing the value of the original object?

There's something strange happening with this code I'm trying out. Code: const x = [{ a: 'alpha', b: 'beta' }, { a: 'gamma' }]; const y = x[0]; y.a = 'delta'; x.splice(1, 0, y) console.log(x) Output: [ ...

My default value is being disregarded by the Angular FormGroup

I am facing an issue with my FormGroup in my form where it is not recognizing the standard values coming from my web api. It sets all the values to null unless I manually type something into the input field. This means that if I try to update a user, it en ...

How can I implement a right-click menu for a row in a React table and how can I retrieve its properties?

I recently incorporated the react-table package into my project, and everything is working smoothly. However, I am looking to add a context menu that allows me to perform actions like cancel or pause when right-clicking on a row. I am using React with Type ...

Having trouble with my React component timer not functioning properly

How can I utilize the Header Component as a Clock timer for my webpage to update every second? Despite searching on Google, I couldn't find examples that match my requirements. Why is the tick() function not functioning properly even though there are ...

Tips for keeping a React-Table row expanded during re-render processes

I am working on a straightforward react-table that includes an expanded sub-component linked to a Redux state object. Whenever the sub-component is expanded, I make an API call to retrieve additional data for lazy-loading into the Redux store. The Redux s ...

What is the best way to create a custom hook that updates in response to changes in state?

My situation involves a custom hook that handles a specific state variable. After making changes to the state, it doesn't update right away. To solve this issue, I need to subscribe to it using useEffect. The challenge is that I can't directly ...

How can I build TypeScript using the react-native-typescript-transformer?

Ever since Expo SDK 31, TypeScript support has been seamlessly integrated. This is a fantastic development. However, it appears that babel-typescript is being used. In my project on Expo SDK 33, I require the use of react-native-typescript-transformer. Is ...

The Jest mock is infiltrating the function logic instead of simply returning a rejected promise

The Issue at Hand I am currently working on testing the correct handling of errors when a use case function returns a rejected promise along with the appropriate status code. However, I seem to be encountering an issue where instead of getting a rejected ...

Typing Redux Thunk with middleware in TypeScript: A comprehensive guide

I recently integrated Redux into my SPFx webpart (using TypeScript), and everything seems to be working fine. However, I'm struggling with typing the thunk functions and could use some guidance on how to properly type them. Here's an example of ...

I am experiencing difficulties updating my Firebase dependencies

I'm encountering an issue with updating my firebase dependencies in my project. Here is the content of my pubspec.yaml file: name: customimageclassifier description: Crowdsource data for ML models version: 1.0.4 environment: sdk: ">=2.2. ...

What is the process for modifying object information using string input in typescript?

How can I update an object's value in TypeScript based on a string input related to the object key? const objData = { // random value A: 11, B: 13, C: 53, innerObj: { AStatus: true, BStatus: false, CStatus: true, }, }; type Item ...

Aurelia: The passing down of views and view-models

In the process of developing an Aurelia app, I am tasked with creating functionality that allows users to display various lists for different resources. These lists share common features such as a toolbar with search and refresh capabilities, along with a ...