Back to Puter

FS

src/docs/src/FS.md

26.05.410.0 KB
Original Source

The Cloud Storage API lets you store and manage data in the cloud.

It comes with a comprehensive but familiar file system operations including write, read, delete, move, and copy for files, plus powerful directory management features like creating directories, listing contents, and much more.

With Puter.js, you don't need to worry about setting up storage infrastructure such as configuring buckets, managing CDNs, or ensuring availability, since everything is handled for you. Additionally, with the User-Pays Model, you don't have to worry about storage or bandwidth costs, as users of your application cover their own usage.

Features

<div style="overflow:hidden; margin-bottom: 30px;"> <div class="example-group active" data-section="write"><span>Write File</span></div> <div class="example-group" data-section="read"><span>Read File</span></div> <div class="example-group" data-section="mkdir"><span>Create Directory</span></div> <div class="example-group" data-section="readdir"><span>List Directory</span></div> <div class="example-group" data-section="rename"><span>Rename</span></div> <div class="example-group" data-section="copy"><span>Copy</span></div> <div class="example-group" data-section="move"><span>Move</span></div> <div class="example-group" data-section="stat"><span>Get Info</span></div> <div class="example-group" data-section="delete"><span>Delete</span></div> <div class="example-group" data-section="upload"><span>Upload</span></div> </div> <div class="example-content" data-section="write" style="display:block;">

Create a new file containing "Hello, world!"

html;fs-write
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        // Create a new file called "hello.txt" containing "Hello, world!"
        puter.fs.write('hello.txt', 'Hello, world!').then(() => {
            puter.print('File written successfully');
        })
    </script>
</body>
</html>
</div> <div class="example-content" data-section="read">

Reads data from a file

html;fs-read
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            // (1) Create a random text file
            let filename = puter.randName() + ".txt";
            await puter.fs.write(filename, "Hello world! I'm a file!");
            puter.print(`"${filename}" created
`);

            // (2) Read the file and print its contents
            let blob = await puter.fs.read(filename);
            let content = await blob.text();
            puter.print(`"${filename}" read (content: "${content}")
`);
        })();
    </script>
</body>
</html>
</div> <div class="example-content" data-section="mkdir">

Create a new directory

html;fs-mkdir
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        // Create a directory with random name
        let dirName = puter.randName();
        puter.fs.mkdir(dirName).then((directory) => {
            puter.print(`"${dirName}" created at ${directory.path}`);
        }).catch((error) => {
            puter.print('Error creating directory:', error);
        });
    </script>
</body>
</html>
</div> <div class="example-content" data-section="readdir">

Read a directory

html;fs-readdir
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        puter.fs.readdir('./').then((items) => {
            // print the path of each item in the directory
            puter.print(`Items in the directory:
${items.map((item) => item.path)}
`);
        }).catch((error) => {
            puter.print(`Error reading directory: ${error}`);
        });
    </script>
</body>
</html>
</div> <div class="example-content" data-section="rename">

Rename a file

html;fs-rename
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            // Create hello.txt
            await puter.fs.write('hello.txt', 'Hello, world!');
            puter.print(`"hello.txt" created
`);

            // Rename hello.txt to hello-world.txt
            await puter.fs.rename('hello.txt', 'hello-world.txt')
            puter.print(`"hello.txt" renamed to "hello-world.txt"
`);
        })();
    </script>
</body>
</html>
</div> <div class="example-content" data-section="copy">

Copy a file

html;fs-copy
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
    (async () => {
        // (1) Create a random text file
        let filename = puter.randName() + '.txt';
        await puter.fs.write(filename, 'Hello, world!');
        puter.print(`Created file: "${filename}"
`);

        // (2) create a random directory
        let dirname = puter.randName();
        await puter.fs.mkdir(dirname);
        puter.print(`Created directory: "${dirname}"
`);

        // (3) Copy the file into the directory
        puter.fs.copy(filename, dirname).then((file)=>{
            puter.print(`Copied file: "${filename}" to directory "${dirname}"
`);
        }).catch((error)=>{
            puter.print(`Error copying file: "${error}"
`);
        });
    })()
    </script>
</body>
</html>
</div> <div class="example-content" data-section="move">

Move a file

html;fs-move
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
    (async () => {
        // (1) Create a random text file
        let filename = puter.randName() + '.txt';
        await puter.fs.write(filename, 'Hello, world!');
        puter.print(`Created file: ${filename}
`);

        // (2) create a random directory
        let dirname = puter.randName();
        await puter.fs.mkdir(dirname);
        puter.print(`Created directory: ${dirname}
`);

        // (3) Move the file into the directory
        await puter.fs.move(filename, dirname);
        puter.print(`Moved file: ${filename} to directory ${dirname}
`);

        // (4) Delete the file and directory (cleanup)
        await puter.fs.delete(dirname + '/' + filename);
        await puter.fs.delete(dirname);
    })();
    </script>
</body>
</html>
</div> <div class="example-content" data-section="stat">

Get information about a file

html;fs-stat
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            // () create a file
            await puter.fs.write('hello.txt', 'Hello, world!');
            puter.print('hello.txt created
');

            // (2) get information about hello.txt
            const file = await puter.fs.stat('hello.txt');
            puter.print(`hello.txt name: ${file.name}
`);
            puter.print(`hello.txt path: ${file.path}
`);
            puter.print(`hello.txt size: ${file.size}
`);
            puter.print(`hello.txt created: ${file.created}
`);
        })()
    </script>
</body>
</html>
</div> <div class="example-content" data-section="delete">

Delete a file

html;fs-delete
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            // (1) Create a random file
            let filename = puter.randName();
            await puter.fs.write(filename, 'Hello, world!');
            puter.print('File created successfully
');

            // (2) Delete the file
            await puter.fs.delete(filename);
            puter.print('File deleted successfully');
        })();
    </script>
</body>
</html>
</div> <div class="example-content" data-section="upload">

Upload a file from a file input

html;fs-upload
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <input type="file" id="file-input" />
    <script>
        // File input
        let fileInput = document.getElementById('file-input');

        // Upload the file when the user selects it
        fileInput.onchange = () => {
            puter.fs.upload(fileInput.files).then((file) => {
                puter.print(`File uploaded successfully to: ${file.path}`);
            })
        };
    </script>
</body>
</html>
</div>

Functions

These cloud storage features are supported out of the box when using Puter.js:

Examples

You can see various Puter.js Cloud Storage features in action from the following examples:

Tutorials