Back

POST FILE TO THE CLOUD FUNCTION FROM CLIENT SIDE

  • 1
  • Self Hosted
  • Flutter
  • Storage
Mosh Ontong
30 Oct, 2023, 06:15

In current new version of Cloud Function how to POST a file from flutter client side to the Cloud Function?

TL;DR
To upload files to a cloud function from the client side, you can use the storage API instead of sending files directly to the function. Here's an example code snippet: ```dart import 'package:appwrite/appwrite.dart'; void main() async { // Initialize the SDK final client = Client() .setEndpoint('https://cloud.appwrite.io/v1') .setProject('[PROJECT_ID]'); final storage = Storage(client); // Upload the file final file = await storage.createFile( bucketId: '[BUCKET_ID]', fileId: ID.unique(), file: Input
Haimantika
30 Oct, 2023, 14:59

createFile should help. Example code:

TypeScript
import 'package:appwrite/appwrite.dart';

void main() async {
  // Initialize the SDK
  final client = Client()
      .setEndpoint('https://cloud.appwrite.io/v1')
      .setProject('[PROJECT_ID]');

  final storage = Storage(client);

  // Upload the file
  final file = await storage.createFile(
    bucketId: '[BUCKET_ID]',
    fileId: ID.unique(),
    file: InputFile.fromPath(
      path: './path-to-file/file.jpg',
      filename: 'file.jpg',
    ),
  );

  // Do something with the response
  print(file);
}
Drake
30 Oct, 2023, 19:04

I'm pretty sure there's still a max limit of how much the body can be. As such, I don't recommend sending files directly to a function. It would be better to use the storage API and create a file, then, your function can download the file and do whatever

Mosh Ontong
31 Oct, 2023, 01:59

I would like to upload files to a cloud function for a specific reason: to create logs. For instance, when a doctor uploads a file to a particular consultation room ID, this action should be added to the consultation logs. This ensures that all participants in that consultation are informed of the new upload.

While it's possible to use a trigger function to log new files added to a specific storage bucket, there are limitations to this approach. Specifically, it's challenging to determine which consultation room the file belongs to, who uploaded the file, and which consultation log ID should be used for this action.

Due to these challenges, I considered using a cloud function to handle file uploads and perform the necessary logic. However, as you pointed out, there are storage limitations uploading on cloud functions. Therefore, I've requested a feature on the Storage API to address this:

GitHub Issue: Add feature to Storage API

Drake
31 Oct, 2023, 17:35

@Mosh Ontong soooo actually, looks like there is no limit to how big the body can be...however, we may not be handling binary data very well 😬

Mosh Ontong
31 Oct, 2023, 17:40

ohh but this feature request I have been posted is this possible to implement? I think the firebase storage they have this they call it custom metadata

https://firebase.google.com/docs/storage/web/file-metadata#custom_metadata

it is just like pref property from User object

Drake
31 Oct, 2023, 20:33

responded in the issue

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more