Uploading a file to cozy drive


#1

Hi, I want to upload a file to a cozy drive using cozy-client.
How can it be realized?
cozy-client-js has the following methods, but cozy-client could not find them… Maybe you can save the file to CouchDB instead?
Files API


#2

Thank you @moriokatky. I just added this use case in our docs (https://github.com/cozy/cozy-client/pull/536 will available soon on https://docs.cozy.io/en/cozy-client/api/cozy-stack-client/#filecollection)

So the idea is to use FileCollection.createFile()

You can find a few usage exemples here : https://github.com/cozy/cozy-libs/blob/88b38551d1deab26607ba92ab74b31b356ae27c3/packages/cozy-doctypes/src/File.js


#3

Thank you for your quick response.


#4

Hi, I want to import and use the examples Files.js,
You should be able to use it like this…
CozyFile.uploadFileWithConflictStrategy(~~~

but I get this error. What should I do?

import CozyFile from ‘./Files.js’
–> WARNING
"export ‘default’ (imported as ‘CozyFile’) was not found in ‘./Files.js’

import { CozyFile } from ‘./Files.js’
–> WARNING
"export ‘CozyFile’ was not found in ‘./Files.js’

and…
How can I find dirId? Is the root value a special?


#5

You have to install cozy-doctypes :
with yarn : yarn add cozy-doctypes

Then: import CozyFiles
import { CozyFile } from 'cozy-doctypes'

Once cozyfiles is imported, you have to attach a client on it if not done already:

if (!CozyFile.cozyClient) CozyFile.registerClient(this.props.client)

=> this.props.client is the result of new CozyClient(...)

Then, you can start using CozyFiles method.

Our root directory has a special id io.cozy.files.root-dir

Are you developing something for the browser or for node?


#6

Hi, Thank you for your reply.

-> Are you developing something for the browser or for node?
There is a web service that can get weight and blood pressure data in json format.
I want to put that data on CozyDrive.
I was able to get json data with React fetch, but I don’t know how to put the file in CozyDrive.


#7

Hi, Thank you for teaching me the steps.

Confirmed that the following has been executed using the VSCode debugger.
CozyFile.registerClient
CozyFile.uploadFileWithConflictStrategy

However, no files were placed on Docker (Installed mycozyapp).
There was no error message.

root@37919ae8f17a:/data/cozy-storage/cozy.tools:8080# ls -al
total 20
drwxr-xr-x 5 root root 4096 Oct 17 06:05 .
drwxr-xr-x 1 root root 4096 Oct 17 01:43 …
drwxr-xr-x 2 root root 4096 Oct 17 01:43 .cozy_trash
drwxr-xr-x 2 root root 4096 Oct 17 01:43 Administrative
drwxr-xr-x 4 root root 4096 Oct 17 01:43 Photos

Is the dirId specification incorrect?

import { CozyFile } from ‘cozy-doctypes’

const client = new CozyClient({
uri: ${protocol}//${data.cozyDomain},
token: data.cozyToken,
schema
})

if (!CozyFile.cozyClient) {
CozyFile.registerClient(client)
}
const file = new ArrayBuffer(4)
var ary_u16 = new Uint16Array(file, 0, 2)
ary_u16[0] = 0x0001
ary_u16[1] = 0x0100
CozyFile.uploadFileWithConflictStrategy(‘sample.txt’, file, ‘io.cozy.files.root-dir’, ‘erase’)