Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get file contents #2578

Merged
merged 8 commits into from
Oct 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions examples/get-file-contents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import {
PrivateKey,
FileContentsQuery,
Hbar,
FileCreateTransaction,
FileDeleteTransaction,
Client,
AccountId,
Logger,
LogLevel,
} from "@hashgraph/sdk";
import dotenv from "dotenv";

dotenv.config();
/**
* How to get file contents
*
*/

async function main() {
/**
* Step 0:
* Create and configure the SDK Client
*/
if (
!process.env.OPERATOR_ID ||
!process.env.OPERATOR_KEY ||
!process.env.HEDERA_NETWORK
) {
console.error(
"Environment variables OPERATOR_ID, OPERATOR_KEY, and HEDERA_NETWORK are required.",
);
throw new Error("Missing required environment variables.");
}

const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromStringED25519(process.env.OPERATOR_KEY);

// Create the client based on the HEDERA_NETWORK environment variable
const client = Client.forName(process.env.HEDERA_NETWORK);

client.setOperator(operatorId, operatorKey);

// Set logger
const infoLogger = new Logger(LogLevel.Info);
client.setLogger(infoLogger);

/**
* Step 1: Submit the file create transaction
*/

// Content to be stored in the file.
const fileContents = Buffer.from("Hedera is great!", "utf-8");

try {
console.log("Creating new file...");

// Create the transaction
let transaction = new FileCreateTransaction()
.setKeys([operatorKey.publicKey])
.setContents(fileContents)
.setMaxTransactionFee(new Hbar(2))
.freezeWith(client);

transaction = await transaction.sign(operatorKey);

const response = await transaction.execute(client);

const receipt = await response.getReceipt(client);

// Get the file ID
const newFileId = receipt.fileId;
console.log(`Created new file with ID: ${newFileId.toString()}`);

/**
* Step 2: Get file contents and print them
*/

const fileContentsQuery = await new FileContentsQuery()
.setFileId(newFileId)
.execute(client);

console.log("File contents: " + fileContentsQuery.toString());

// Clean up: Delete created file
const fileDeleteTxResponse = await new FileDeleteTransaction()
.setFileId(newFileId)
.execute(client);

await fileDeleteTxResponse.getReceipt(client);

console.log("File deleted successfully");
} catch (error) {
console.error("Error occurred during file creation:", error);
} finally {
client.close();
console.log("Get File Contents Example Complete!");
}
}

void main();