Back

[Solved] ReactJS bucket downloads

  • 0
  • Web
Gildas Quiniou
30 Mar, 2023, 17:05

Hi, can somebody tell/guide me to a demo for downloading a bucket file once I get the URL from the Get File for Download API, with ReactJS? My main concern is about protected (teams, roles) files, as a fetch() won't propagate my API credentials, afaik... The Web SDK doc talks about automatic download due to the attachment header attribute in the response, but how to achieve this in a ReactJS app? Thx

TL;DR
The user was having trouble downloading a bucket file using ReactJS. After some troubleshooting, they found that adding the cookie stored in localstorage to the fetch header solved the issue. They provided their code as an example for others who might be stuck.
joeyouss
30 Mar, 2023, 18:05

hi, allow me a moment to look into this

joeyouss
30 Mar, 2023, 18:21

TBH it should work but can you share the error message or screenshot. Authorization is done with cookies. Browser automatically append those cookies to all requests to Appwrite API. If you have source code somewhere, then you can share that too w me

Gildas Quiniou
30 Mar, 2023, 20:12

Thanks, sure here is my code :

TypeScript
import { Client, Account, Storage } from "appwrite"

const client = new Client()
const storage = new Storage(client)

client
   .setEndpoint('http://localhost/v1') // Your API Endpoint
   .setProject('xxxxxxxxxx') // Your project ID

const account = new Account(client);
const sess = await account.createEmailSession(
   'toto@toto.com',
   'totototo'
)

const result = storage.getFileDownload('ref_data', 'tariff_names');

// Up to here it works

// This fetch fails with 401 unless I set Read perms on ref_data.tariff_names for everybody
fetch(result)
   .then(response => response.json())
   .catch(err => {
      console.error('Pb fetch', err)
   }).then(data => {
      console.log('Fetch OK')
      console.log(data)
   })

I also tried creating a JWT token and added it to my fetch without better result. BTW, I didn't find any other way to download my file, maybe I missed something but I can't find any method in the storage class...

Gildas Quiniou
31 Mar, 2023, 16:42

I finally got it. I need to add the cookie stored in localstorage to the fetch header, following this tip (https://discord.com/channels/564160730845151244/564161955963731980/751462114740142160). I hope the localstorage will be consistent along all future versions. I'd prefer something integrated to the SDK...

Gildas Quiniou
31 Mar, 2023, 16:43

[Solved] ReactJS bucket downloads

Gildas Quiniou
31 Mar, 2023, 17:03

BTW, I also succeeded with JWT, in a similar way (continuing my code excerpt above) :

TypeScript
const jwt = await account.createJWT()
fetch(result, {
   method: "GET",
   credentials: "include",  // Seems to be related to CORS
   mode: "cors",
   headers: {
      "x-appwrite-jwt": jwt.jwt
   }
)

NB : this code runs in an async function thus the await keyword. Note the JWT has some restrictions (per hour) Hope this help people stucked like me! πŸ˜‰

Drake
31 Mar, 2023, 17:07

in production, you should use a custom domain to ensure the cookie is set properly and used from your app. See https://appwrite.io/docs/custom-domains

Gildas Quiniou
31 Mar, 2023, 17:08

Thanks Steven, I noticed it in the logs and I will do! πŸ˜‰

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