Files/folders

Once you have a vault in Tusky, you can structure your data into files and folders.

Upload file

From path

const path = "/path/to/my/file.jpg";
const uploadId = await tusky.file.upload(vaultId, path);

From stream

import { createReadStream } from "fs";
const fileStream = createReadStream("/path/to/my/file.jpg");

const uploadId  = await tusky.file.upload(vaultId, fileStream, { name: "file.jpg", mimeType: "image/jpeg" });

From buffer

const uploadId = await tusky.file.upload(vaultId, new Blob(["hello world"]), {
    name: "hello_world.txt",
    mimeType: "text/plain",
});

Get file metadata

The field uploadId returned during file.upload() call is the internal Tusky file identifier populated immediately during upload, while blobId and blobObjectId are computed in an asynchronous manner while encoding & uploading the file to Walrus, hence these fields may take some time to be populated.

See the detailed file fields description here.

const fileMetadata = await tusky.file.get(uploadId);

// blobId - file reference off chain
// computed deterministically from blob content
console.log("File Walrus blob id: " + fileMetadata.blobId);

// blobObjectId - file reference on chain
console.log("File Sui object id: " + fileMetadata.blobObjectId);

Download file

Buffer

method file.arrayBuffer() will put whole file in memory, for larger files we recommend using file.stream() or file.download()

const fileBuffer = await tusky.file.arrayBuffer(fileId);

Stream

Stream file

const fileStream = await tusky.file.stream(fileId);

Stream file & save it in provided path in your local file system

await tusky.file.download(fileId, { path: "path/to/save/your/file" });

Create folder

const { id: folderId } = await tusky.folder.create(vaultId, "your folder name");

Now you can upload your next file into the newly created folder

const uploadId = await tusky.file.upload(vaultId, fileBuffer, { parentId: folderId });

List files

List all user files

const files = await tusky.file.listAll();

List all files within the vault

const files = await tusky.file.listAll({ vaultId: vaultId });

List all files within the folder

const files = await tusky.file.listAll({ parentId: folderId });