Skip to content

Commit fe6e566

Browse files
committed
edits
1 parent e51c059 commit fe6e566

File tree

1 file changed

+123
-30
lines changed

1 file changed

+123
-30
lines changed

docs/develop/access-the-swarm/upload-and-download.md

Lines changed: 123 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@ id: upload-and-download
44
---
55

66

7-
Uploading to Swarm has two steps: (1) **buy storage** as a **postage stamp batch**—you can store more data or for longer by spending more xBZZ or topping up the batch—and (2) **upload using the batch ID** to prove the upload is paid. The upload returns a **Swarm reference** (hash), anyone with that reference can download the content.
7+
Uploading to Swarm has two steps: (1) **buy storage** as a **postage stamp batch** with a unique **batch ID**—and (2) **upload using the batch ID**. The upload returns a **Swarm reference hash**, anyone with that reference can download the content.
88

99

1010
**Before you begin:**
1111
* You need a running Bee node connected to Gnosis Chain and funded with **xBZZ** and **xDAI**.
1212
* Uploads always require a **postage stamp batch**.
13-
* Ultra-light nodes can download but **cannot upload**, since they cannot buy stamps.
13+
* Ultra-light nodes can download but **cannot upload**.
1414

1515

1616
## Upload & Download with bee-js
1717

1818
The `bee-js` library is the **official SDK for building Swarm-based applications**. It works in both **browser** and **Node.js** environments and **greatly simplifies development** compared with using the Bee HTTP API directly. It is the recommended method for developing applications on Swarm.
1919

20+
Refer to the [`bee-js` documentation](https://bee-js.ethswarm.org/) for more usage guides.
21+
2022
:::tip
21-
Some
22-
**Environment-specific methods**
23+
**Environment-specific methods:**
2324
* **Browser-only:** [`uploadFiles`](https://bee-js.ethswarm.org/docs/api/classes/Bee/#uploadfiles) (multi-file via `File[]`/`FileList`)
2425
* **Node.js-only:** [`uploadFilesFromDirectory`](https://bee-js.ethswarm.org/docs/api/classes/Bee/#uploadfiles) (recursively reads local filesystem to upload multiple files in a directory using `fs`),
2526
* **Both:** [`uploadFile`](https://bee-js.ethswarm.org/docs/api/classes/Bee/#uploadfile) (with some environment specific usage), [`downloadFile`](https://bee-js.ethswarm.org/docs/api/classes/Bee/#downloadfile)
@@ -29,26 +30,35 @@ Some
2930

3031
**Step-by-step Walkthrough:**
3132

32-
1. Create a Bee client
33+
1. Create a Bee client:
34+
3335
`const bee = new Bee("http://localhost:1633")`
3436

35-
2. Buy storage (postage stamp batch)
37+
2. Buy storage (postage stamp batch) by specifying storage size and duration:
38+
3639
`const batchId = await bee.buyStorage(Size.fromGigabytes(1), Duration.fromDays(1))`
3740

38-
3. Read the file from disk
41+
3. Read the file from disk:
42+
3943
`const data = await readFile("./hello.txt")`
4044

41-
4. Upload bytes with filename & content type → get reference
45+
4. Upload bytes with filename & content type → get reference:
46+
4247
`const { reference } = await bee.uploadFile(batchId, data, "hello.txt", { contentType: "text/plain" })`
4348

44-
5. Download the file by reference
49+
5. Download the file by reference:
50+
4551
`const file = await bee.downloadFile(reference)`
4652

47-
6. Log the downloaded file’s metadata and contents
48-
`console.log(file.name)`
49-
`console.log(file.contentType)`
50-
`console.log(file.data.toUtf8())`
53+
6. Log the downloaded file’s title and metadata:
54+
55+
`console.log(file.name)`
56+
57+
`console.log(file.contentType)`
5158

59+
`console.log(file.data.toUtf8())`
60+
61+
5262
**Full example:**
5363

5464
```js
@@ -87,20 +97,34 @@ When working with browsers you can use the [`File` interface](https://developer.
8797
**Walkthrough**
8898

8999
1. Initialize a Bee object using the API endpoint of a Bee node:
100+
90101
`const bee = new Bee("http://localhost:1633")`
91102

92103
2. Buy storage and get postage stamp batch ID:
104+
93105
`const batchId = await bee.buyStorage(Size.fromGigabytes(1), Duration.fromDays(1))`
94106

95107
3. Create a `File` object:
108+
96109
`const file = new File(["Hello Swarm!"], "hello.txt", { type: "text/plain" })`
97110

98111
4. Use batch ID to upload → get reference:
112+
99113
`const { reference } = await bee.uploadFile(batchId, file)`
100114

101115
5. Download by reference:
116+
102117
`const downloaded = await bee.downloadFile(reference)`
103118

119+
6. Log the downloaded file’s title and metadata:
120+
121+
`console.log(downloaded.name) // "hello.txt"`
122+
123+
`console.log(file.contentType) // "text/plain"`
124+
125+
`console.log(downloaded.data.toUtf8()) // prints file content`
126+
127+
104128
```js
105129

106130
import { Bee, Size, Duration } from "@ethersphere/bee-js"
@@ -126,7 +150,7 @@ console.log(downloaded.data.toUtf8()) // prints file content
126150

127151
### Multiple files — Browser
128152

129-
Use **`uploadFiles`** (browser-only). It accepts `File[]`/`FileList`. When using `<input type="file" webkitdirectory multiple>`, each file’s **relative path** is preserved. To download a specific file later, pass the **collection reference** plus the **same relative path**.
153+
Use **`uploadFiles`** for multi-file upload in the browser. It accepts `File[]`/`FileList`. When using `<input type="file" webkitdirectory multiple>`, each file’s **relative path** is preserved. To download a specific file later, pass the **collection reference** plus the **same relative path**.
130154

131155

132156
1. Initialize a Bee object using the API endpoint of a Bee node:
@@ -143,12 +167,19 @@ Use **`uploadFiles`** (browser-only). It accepts `File[]`/`FileList`. When using
143167
]
144168
```
145169

146-
4. Upload multiple files (collection) → get collection reference
170+
4. Upload multiple files (collection) → get collection reference:
147171
`const res = await bee.uploadFiles(batchId, files)`
148172

149-
5. Download files by relative path
173+
5. Download files by relative paths:
150174
`const logo = await bee.downloadFile(res.reference, "images/logo.png")`
151175

176+
6. Log the downloaded file’s title and contents:
177+
178+
`console.log(page.name) // "index.html"`
179+
180+
`console.log(page.data.toUtf8()) // prints file content`
181+
182+
152183
```js
153184
import { Bee, Size, Duration } from "@ethersphere/bee-js"
154185

@@ -186,20 +217,26 @@ console.log(style.data.toUtf8()) // prints file content
186217

187218
**Step-by-step Walkthrough:**
188219

189-
1. Create a Bee client
220+
1. Initialize a Bee object using the API endpoint of a Bee node:
221+
190222
`const bee = new Bee("http://localhost:1633")`
191223

192-
2. Buy storage
224+
2. Buy storage and get postage stamp batch ID:
225+
193226
`const batchId = await bee.buyStorage(Size.fromGigabytes(1), Duration.fromDays(1))`
194227

195-
3. Recursively upload a local directory → get collection reference
228+
3. Recursively upload a local directory → get collection reference:
229+
196230
`const res = await bee.uploadFilesFromDirectory(batchId, "./site")`
197231

198-
4. Download one file by its relative path
232+
4. Download one file by its relative path:
233+
199234
`const page = await bee.downloadFile(res.reference, "index.html")`
200235

201-
5. Log the downloaded file name and contents
236+
5. Log the downloaded file name and contents:
237+
202238
`console.log(page.name ?? "index.html")`
239+
203240
`console.log(page.data.toUtf8())`
204241

205242
**Full example:**
@@ -229,23 +266,78 @@ console.log(stylesheet.name) // "example.txt"
229266
console.log(stylesheet.data.toUtf8()) // prints file content
230267
```
231268

232-
> Tip: For **binary** files, don’t convert to UTF-8 — log `file.data.length` or write to disk instead.
233-
234-
235269

236270
## Upload & Download with Swarm CLI
237271

238-
The `swarm-cli` offers a convenient command-line interface for Bee node interaction. It's a convenient tool for node management or one-off uploads and downloads.
272+
The `swarm-cli` tool offers a convenient command-line interface for Bee node interaction. It's a convenient tool for node management or one-off uploads and downloads.
273+
274+
Refer to [the official README](https://github.com/ethersphere/swarm-cli/blob/master/README.md) for a more complete usage guide.
239275

240276
Buy storage via an interactive prompt (capacity + TTL), then upload:
241277

242278
```bash
243279
swarm-cli stamp create
244-
# ...follow prompts for capacity (e.g., 1GB) and time-to-live (e.g., 1d, 1w)...
280+
```
281+
282+
Follow the interactive prompts:
283+
284+
```bash
285+
For swarm cli, use "stamp create", which looks like this:
286+
287+
PS C:\Users\noahm> swarm-cli stamp create
288+
Please provide the total capacity of the postage stamp batch
289+
This represents the total size of data that can be uploaded
290+
Example: 1GB
291+
292+
Please provide the time-to-live (TTL) of the postage stamps
293+
Defines the duration after which the stamp will expire
294+
Example: 1d, 1w, 1month
295+
296+
You have provided the following parameters:
297+
Capacity: 1.074 GB
298+
TTL: 7 days
299+
300+
Cost: 0.6088166475825152 xBZZ
301+
Available: 10000.0000000000000000 xBZZ
302+
Type: Immutable
303+
? Confirm the purchase Yes
304+
... Creating postage batch. This may take up to 5 minutes.
305+
```
306+
307+
Once you have a valid stamp batch, you can find it using `swarm-cli stamp list`
308+
309+
```bash
310+
swarm-cli stamp list
311+
```
312+
313+
```bash
314+
Stamp ID: 6dd0c4bbb6d62ba6c5fae3b000301c961ee584dd32846291821d789d7582ae36
315+
Usage: 0%
316+
Capacity (immutable): 2.380 GB remaining out of 2.380 GB
317+
TTL: A few seconds (2025-09-21)
318+
------------------------------------------------------------------------------------------------------------------------
319+
Stamp ID: d13210952ec60b01a3c0027602743921736d6b277e9e70dd00d0d95fd878acbc
320+
Usage: 0%
321+
Capacity (immutable): 2.380 GB remaining out of 2.380 GB
322+
TTL: A few seconds (2025-09-21)
323+
```
324+
325+
Use `swarm-cli upload` along with a valid batch ID to upload a file:
326+
327+
```bash
245328
swarm-cli upload test.txt --stamp <BATCH_ID>
246329
```
247330

248-
Download content:
331+
You can also simply use:
332+
333+
```bash
334+
swarm-cli upload <PATH_TO_FILE>
335+
```
336+
337+
And an interactive prompt will walk your through stamp selection and the rest of the upload.
338+
339+
340+
Upon upload, a Swarm reference hash will be returned which can then be used to download content:
249341

250342
```bash
251343
swarm-cli download <REFERENCE> ./output/
@@ -255,17 +347,18 @@ swarm-cli download <REFERENCE> ./output/
255347

256348
The **Bee HTTP API** offers the **lowest-level access** to a Bee node. However, it is **more complex and difficult to use** than **bee-js** or **swarm-cli** because you must manage headers, content types, and postage parameters yourself. **Unless you specifically require raw HTTP control**, we **do not recommend** using the Bee API directly. Instead use **bee-js** for application development and **swarm-cli** for command-line interaction.
257349

350+
Refer to the [Bee API reference specification](https://docs.ethswarm.org/api/) for detailed usage information.
351+
258352
The Bee API exposes three HTTP endpoints:
259353

260354
* **`/bzz`** — upload & download files/directories (most common)
261355
* **`/bytes`** — upload & download raw data
262356
* **`/chunks`** — upload & download individual chunks
263357

264358

265-
### Upload with **/bzz**
266-
267-
While both `swarm-cli` and `bee-js` allow for postage stamp batches to be purchased by specifying the storage duration and data size, the actual call to the Bee API requires an `amount` and `depth` parameters. The relationship between these parameters and the storage size and duration of the batch is complex. Therefore `bee-js` and `swarm-cli` (which allow batches to be purchased by data size/duration which are then converted to `depth`/`amount`) are strongly encouraged for newcomers to development on Swarm. [Learn more](/docs/develop/access-the-swarm/buy-a-stamp-batch).
359+
#### Upload with **/bzz**
268360

361+
While both `swarm-cli` and `bee-js` allow for postage stamp batches to be purchased by specifying the storage duration and data size, the actual call to the Bee API requires an `amount` and `depth` parameters. The relationship between these parameters and the storage size and duration of the batch is complex. Therefore `bee-js` and `swarm-cli` (which allow batches to be purchased by data size/duration which are then converted to `depth`/`amount`) are strongly encouraged for newcomers to development on Swarm. [Learn more](/docs/develop/tools-and-features/buy-a-stamp-batch).
269362

270363
1. Buy a postage batch:
271364

0 commit comments

Comments
 (0)