HFS: Files

Hello World sequence: Store a file on Hedera File Service (HFS), and retrieve the same file from the network.

What you will accomplish


Prerequisites

Before you begin, you should have completed the following Hello World sequence:


Get started: Set up project

To follow along, start with the main branch, which is the default branch of the repo. This gives you the initial state from which you can follow along with the steps as described in the tutorial.

You should already have this from the "Create and Fund Account" sequence. If you have not completed this, you are strongly encouraged to do so.

Alternatively, you may wish to create a .env file and populate it as required.

In the terminal, from the hello-future-world directory, enter the subdirectory for this sequence.

cd 01-hfs-files-sdk/

Reuse the .env file by copying the one that you have previously created into the directory for this sequence.

cp ../00-create-fund-account/.env ./
Check that you have copied the `.env` file correctly

To do so, use the pwd command to check that you are indeed in the right subdirectory within the repo.

pwd

This should output a path that ends with /hello-future-world/01-hfs-files-sdk. If not, you will need to start over.

/some/path/hello-future-world/01-hfs-files-sdk

Next, use the ls command to check that the .env file has been copied into this subdirectory.

ls -a

The first few line of the output should look display .env. If not, you'll need to start over.

.
..
.env

Next, install the dependencies using npm.

npm install

Then open the script-hfs-files-sdk.js file in a code editor, such as VS Code.

You will also need a file to write onto the network. Copy the sample text file provided.

cp my-file.txt.sample my-file.txt

Edit my-file.txt to replace YOUR_NAME with your name (or nickname) in a code editor. For example, if you used "bguiz", the file contents should now be the following.

my-file.txt
Hello future! - bguiz

Write the script

An almost complete script has already been prepared for you, script-hfs-files-sdk.js, and you will only need to make a few modifications (outlined below) to run successfully.

Step 1: File create transaction

The contents of my-file.txt have been read from disk and stored in a Buffer named localFileContents.

Set the contents of localFileContents in FileCreateTransaction, to write your file onto Hedera Testnet.

script-hfs-files-sdk.js
        .setContents(localFileContents.toString())

Step 2: File contents query

After the FileCreateTransaction has been executed, the response will contain a file ID. Read this file from the network by querying it.

Set the file ID in FileContentsQuery.

script-hfs-files-sdk.js
        .setFileId(fileId);

Run the script

In the terminal, run the script using the following command:

node script-hfs-files-sdk.js

You should see output similar to the following:

fileId: 0.0.5835692
fileCreateTxId: 0.0.1186@1699277862.561525871
txExplorerUrl: https://hashscan.io/testnet/transaction/0.0.1186@1699277862.561525871
localFileContents: Hello future! - bguiz

networkFileContents: Hello future! - bguiz

To verify that both the FileCreateTransaction and FileContentsQuery have worked, check that localFileContents and networkFileContents are the same. This indicates that what has been stored on Hedera Testnet is the same as what you have on disk.

Open txExplorerUrl in your browser and check that:

  • The transaction exists (1)

  • The "type" is shown as "FILE CREATE" (2)


Complete

Congratulations, you have completed the Hedera File Service Hello World sequence! 🎉🎉🎉

You have learned how to:


Next Steps

Now that you have completed this Hello World sequence, you have interacted with Hedera File Service (HFS). There are other Hello World sequences for Hedera Smart Contract Service (HSCS), and Hedera Token Service (HTS), which you may wish to check out next.


Cheat sheet

Skip to final state

The repo, github.com/hedera-dev/hello-future-world, is intended to be used alongside this tutorial.

To skip ahead to the final state, use the completed branch. You may use this to compare your implementation to the completed steps of the tutorial.

git fetch origin completed:completed
git checkout completed

Alternatively, you may view the completed branch on Github: github.com/hedera-dev/hello-future-world/tree/completed/01-hfs-files-sdk


Writer: Brendan Editors: Abi, Michiel, Ryan, Krystal

Last updated