forked from hiero-ledger/hiero-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileAppendChunkedExample.java
More file actions
142 lines (120 loc) · 5.13 KB
/
Copy pathFileAppendChunkedExample.java
File metadata and controls
142 lines (120 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// SPDX-License-Identifier: Apache-2.0
package com.hedera.hashgraph.sdk.examples;
import com.hedera.hashgraph.sdk.*;
import com.hedera.hashgraph.sdk.logger.LogLevel;
import com.hedera.hashgraph.sdk.logger.Logger;
import io.github.cdimascio.dotenv.Dotenv;
import java.util.Collections;
import java.util.Objects;
/**
* How to append to already created file.
*/
class FileAppendChunkedExample {
/*
* See .env.sample in the examples folder root for how to specify values below
* or set environment variables with the same names.
*/
/**
* Operator's account ID.
* Used to sign and pay for operations on Hedera.
*/
private static final AccountId OPERATOR_ID =
AccountId.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_ID")));
/**
* Operator's private key.
*/
private static final PrivateKey OPERATOR_KEY =
PrivateKey.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_KEY")));
/**
* HEDERA_NETWORK defaults to testnet if not specified in dotenv file.
* Network can be: localhost, testnet, previewnet or mainnet.
*/
private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK", "testnet");
/**
* SDK_LOG_LEVEL defaults to SILENT if not specified in dotenv file.
* Log levels can be: TRACE, DEBUG, INFO, WARN, ERROR, SILENT.
* <p>
* Important pre-requisite: set simple logger log level to same level as the SDK_LOG_LEVEL,
* for example via VM options: -Dorg.slf4j.simpleLogger.log.org.hiero=trace
*/
private static final String SDK_LOG_LEVEL = Dotenv.load().get("SDK_LOG_LEVEL", "SILENT");
public static void main(String[] args) throws Exception {
System.out.println("Big File Append Example Start!");
/*
* Step 0:
* Create and configure the SDK Client.
*/
Client client = ClientHelper.forName(HEDERA_NETWORK);
// All generated transactions will be paid by this account and signed by this key.
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
// Attach logger to the SDK Client.
client.setLogger(new Logger(LogLevel.valueOf(SDK_LOG_LEVEL)));
var operatorPublicKey = OPERATOR_KEY.getPublicKey();
/*
* Step 1:
* Submit the file create transaction.
*/
// The file is required to be a byte array,
// you can easily use the bytes of a file instead.
String fileContents = "Hedera hashgraph is great!";
System.out.println("Creating new file...");
TransactionResponse fileCreateTxResponse = new FileCreateTransaction()
// Use the same key as the operator to "own" this file.
.setKeys(operatorPublicKey)
.setContents(fileContents)
// The default max fee of 1 Hbar is not enough to create a file (starts around ~1.1 Hbar).
.setMaxTransactionFee(Hbar.from(2))
.execute(client);
TransactionReceipt fileCreateTxReceipt = fileCreateTxResponse.getReceipt(client);
FileId newFileId = fileCreateTxReceipt.fileId;
Objects.requireNonNull(newFileId);
System.out.println("Created new file with ID: " + newFileId);
/*
* Step 2:
* Query file info to check its size after creation.
*/
FileInfo fileInfoAfterCreate = new FileInfoQuery().setFileId(newFileId).execute(client);
System.out.println("Created file size after create (according to `FileInfoQuery`): " + fileInfoAfterCreate.size
+ " bytes.");
/*
* Step 3:
* Create new file contents that will be appended to a file.
*/
StringBuilder contents = new StringBuilder();
for (int i = 0; i <= 4096 * 9; i++) {
contents.append("1");
}
/*
* Step 4:
* Append new file contents to a file.
*/
System.out.println("Appending new contents to the created file...");
new FileAppendTransaction()
.setNodeAccountIds(Collections.singletonList(fileCreateTxResponse.nodeId))
.setFileId(newFileId)
.setContents(contents.toString())
.setMaxChunks(40)
.setMaxTransactionFee(Hbar.from(100))
.freezeWith(client)
.execute(client)
.getReceipt(client);
/*
* Step 5:
* Query file info to check its size after append.
*/
FileInfo fileInfoAfterAppend = new FileInfoQuery().setFileId(newFileId).execute(client);
if (fileInfoAfterCreate.size < fileInfoAfterAppend.size) {
System.out.println(
"File size after append (according to `FileInfoQuery`): " + fileInfoAfterAppend.size + " bytes.");
} else {
throw new Exception("File append was unsuccessful! (Fail)");
}
/*
* Clean up:
* Delete created file.
*/
new FileDeleteTransaction().setFileId(newFileId).execute(client).getReceipt(client);
client.close();
System.out.println("Big File Append Example Complete!");
}
}