Back

file url returns 404 even when the read permission is set for the logged in user

  • 0
  • Web
  • Storage
Ernest
25 Nov, 2023, 19:28

As D5 mentioned earlier you need to pass a jwt to get the resource in stored in your bucket even if it's a preview image. Checkout the jwt docs https://appwrite.io/docs/products/auth/jwt

TL;DR
User is experiencing a 404 error when trying to access a file URL, even though the read permission is set for the logged in user. They are unsure if editing the hosts file or enabling 3rd party cookies will solve the issue. They also question if hosting Appwrite console on a custom domain is necessary. They mention storing a session token in localStorage and ask if that is enough. Another user suggests editing the hosts file or enabling 3rd party cookies. Pratik, the original user, mentions that they do not have a domain to test on. They ask for more information on what to save in localStorage and inquire if using
ideclon
26 Nov, 2023, 06:35

getFilePreview() should be returning the image itself, not a link to the image

manazo
27 Nov, 2023, 17:03

but how can we pass headers in html? what i'm doing is just <img src={imageSrc}/>. i'm not getting the file by a manual fetch request

manazo
27 Nov, 2023, 17:06

in the cient SDK, it returns a URL to the image and in the server SDK, it returns the image itself. and besides, i'm storing the link of the image in the corresponding doc for easy access

Ernest
28 Nov, 2023, 08:48

I mostly work on mobile (Flutter) and can pass in the httpHeader to the package that handles displaying the image. From the docs have you tried setting the jwt on the client?

TypeScript
const client = new Client()
    .setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
    .setProject('<PROJECT_ID>')                  // Your project ID
    .setJWT('eyJJ9.eyJ...886ca');  
Ernest
28 Nov, 2023, 08:55

Also, found this in the docs

Images Some use cases do not allow custom headers, such as embedding images from Appwrite in HTML. In these cases, you can provide the Appwrite project ID using the query parameter project.

<img src="[ENDPOINT]/v1/storage/buckets/[BUCKET_ID]/files/[FILE_ID]/preview?project=[PROJECT_ID]"/>

Source: https://appwrite.io/docs/apis/rest

manazo
28 Nov, 2023, 09:08

yes I do have the project ID passed as the query parameter. my image link looks like below https://cloud.appwrite.io/v1/storage/buckets/[BUCKET_ID]/files/[FILE_ID]/preview?project=[PROJECT_ID]&width=300&quality=50

the width and quality params are passed for better performance so that the browser doesn't need to load the original full-sized high-quality image

manazo
28 Nov, 2023, 09:12

what i can assume is, by the project id, appwrite backend can only know which project this request belongs to. but my each file has permissions set for specific users. so for some reason, appwrite backend cannot determine which user is sending this request

Ernest
28 Nov, 2023, 09:21

Yes, that is why the jwt is needed but I'm not sure where it should be set on the web SDK. Have you tried setting the jwt on the client?

manazo
28 Nov, 2023, 09:38

tho i haven't tried it yet, but after checking the actual code of setJWT method, it doesn't seem to be something helpful the setJWT method sets a header for every request we do using the client SDK. but in my case, I'm not even making any requests. I'm just embedding the link in my HTML

setJWT method code is below:

TypeScript
setJWT(value: string): this {
        this.headers['X-Appwrite-JWT'] = value;
        this.config.jwt = value;
        return this;
    }
Ernest
28 Nov, 2023, 18:29

@Drake Sorry for pinging but please can you help here. Should Pratik use the rest api instead of the web sdk?

Drake
28 Nov, 2023, 18:36

On web, you need to have the cookies set up properly. This involves making sure the Appwrite cookie is a first party cookie so the browser sends it when requesting the file from Appwrite.

manazo
29 Nov, 2023, 16:18

currently, the session token is saved in the localStorage under the cookieFallback name. isn't that enough? cuz all other client-side features that require a logged-in user is working quite properly. and it's currently in the development phase at the localhost

Drake
29 Nov, 2023, 16:52

No it's not

manazo
29 Nov, 2023, 21:37

then what more info i need to save on the localStorage?

Drake
30 Nov, 2023, 18:14
manazo
2 Dec, 2023, 07:58

atm my app is in localhost and don't have any domain to test it out. so should i think it'll be solved automatically if i add a custom domain?

Drake
3 Dec, 2023, 00:49

You can edit your hosts file or enable 3rd party cookies

manazo
4 Dec, 2023, 03:48

what do u mean by editing hosts file? and i'm not planning to host appwrite console in a custom domain. so is there any way i can make it work without a custom domain?

Drake
4 Dec, 2023, 07:22

Your hosts file allows you to point domains to IPs locally. Google how to edit hosts file on <your OS>

Drake
4 Dec, 2023, 07:22

No, you'll need a custom domain. Typically, you would host your app on something like example.com and then you would create a custom project domain like appwrite.example.com

manazo
6 Dec, 2023, 09:18

means the only way to resolve this issue is to host appwrite console in a custom domain (sub-domain of my main project)?

Drake
6 Dec, 2023, 10:19

You just need to add a CNAME DNS record

Drake
6 Dec, 2023, 10:20

Or enable 3rd party cookies for local testing

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