Back

[SOLVED] Linking Storage file and Database Doc

  • 0
  • Self Hosted
  • Web
  • Storage
BloodThermic
25 May, 2023, 08:19

I am making a reviews section on a website. I am needing to figure out how to like the document (The Review) and the File (The Profile Photo) together so I can call them together? My only idea I got is to get the file name but I am not sure how to extract that

TL;DR
The user was trying to figure out how to link a storage file and a database document together. They were advised to use the `createFile` function and store the file ID in the respective document. The user also asked for the best way to grab the ID when using `ID.unique()`, and they were told to store the `fileId` and `bucketId` in a collection/document. The issue was resolved, and the user was happy with the solution.
BloodThermic
25 May, 2023, 08:21

I can just the the id and store it in the doc right

safwan
25 May, 2023, 08:21

Every file in a bucket has a fileId and is associated with a bucketId. You can store these two values in some form of collection/document.

safwan
25 May, 2023, 08:22

I hope that makes sense

BloodThermic
25 May, 2023, 08:22

Yes it does. I just noticed that! Feel free to close now

safwan
25 May, 2023, 08:23

[SOLVED] Linking Storage file and Database Doc

BloodThermic
25 May, 2023, 08:30

Linking Storage file and Database Doc

BloodThermic
25 May, 2023, 08:30

@safwan Quick question sorry! What is the best way of grabbing the id from the file when using ID.unique

safwan
25 May, 2023, 08:33

Not an issue!

I'm assuming you're using ID.unique() for creating the document.

As for grabbing the file ID, it depends. I grab the IDs whenever a new file is created and store it in the respective document. If you have existing files that you need to link up, I'd suggest manually copy-pasting them lmao

safwan
25 May, 2023, 08:34

If you have a user interface that let's a user select the photo they want to use, you can use the list files endpoint to list all the files in a bucket, and then based on the selection, grab the file ID.

BloodThermic
25 May, 2023, 08:38
TypeScript
        storage.createFile('646eb0cc07db82e6dff9', ID.unique(id), profilePhoto.files[0])
        console.log(id)
    }```
BloodThermic
25 May, 2023, 08:38

This is what I got

BloodThermic
25 May, 2023, 08:39

I am needing to know how to grab it the moment its created so I can put it in the respective document

safwan
25 May, 2023, 08:41

That's perfect. So after storage.createFile finishes executing, it returns a File Object. Here's what I would do:

TypeScript
async function PPUpload() {
        var newFile = await storage.createFile('646eb0cc07db82e6dff9', ID.unique(id), profilePhoto.files[0])
        console.log(newFile.$id)
}
safwan
25 May, 2023, 08:42

I haven't used js in a while for referring to object keys, and there's a good chance that newFile.$id may not work lol

safwan
25 May, 2023, 08:42

Just check and lmk

BloodThermic
25 May, 2023, 08:57

All works good! Your amazing Thank you for your time!

BloodThermic
25 May, 2023, 08:57

[SOLVED] Linking Storage file and Database Doc

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