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.
Reuse the .env
file by copying the one that you have previously created into the directory for this sequence.
Next, install the dependencies using npm
.
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.
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.
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.
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
.
Run the script
In the terminal, run the script using the following command:
You should see output similar to the following:
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
Last updated