You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
8
7
9
-
10
8
**Before you begin:**
11
-
* You need a running Bee node connected to Gnosis Chain and funded with **xBZZ** and **xDAI**.
12
-
* Uploads always require a **postage stamp batch**.
13
-
* Ultra-light nodes can download but **cannot upload**.
14
9
10
+
- You need a running Bee node connected to Gnosis Chain and funded with **xBZZ** and **xDAI**.
11
+
- Uploads always require a **postage stamp batch**.
12
+
- Ultra-light nodes can download but **cannot upload**.
15
13
16
-
## Upload & Download with bee-js
14
+
## Upload & Download with bee-js
17
15
18
16
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.
19
17
20
18
Refer to the [`bee-js` documentation](https://bee-js.ethswarm.org/) for more usage guides.
21
19
22
20
:::tip
23
21
**Environment-specific methods:**
24
-
***Browser-only:**[`uploadFiles`](https://bee-js.ethswarm.org/docs/api/classes/Bee/#uploadfiles) (multi-file via `File[]`/`FileList`)
25
-
***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`),
26
-
***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)
27
-
:::
22
+
23
+
-**Browser-only:**[`uploadFiles`](https://bee-js.ethswarm.org/docs/api/classes/Bee/#uploadfiles) (multi-file via `File[]`/`FileList`)
24
+
-**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`),
25
+
-**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)
26
+
:::
28
27
29
28
### Single file — Node.js
30
29
@@ -51,43 +50,46 @@ Refer to the [`bee-js` documentation](https://bee-js.ethswarm.org/) for more usa
console.log(downloaded.data.toUtf8()) // prints file content
148
+
constdownloaded=awaitbee.downloadFile(reference);
149
+
console.log(downloaded.name);// "hello.txt"
150
+
console.log(file.contentType);// "text/plain"
151
+
console.log(downloaded.data.toUtf8());// prints file content
149
152
```
150
153
151
-
### Multiple files — Browser
154
+
### Multiple files — Browser
152
155
153
156
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**.
154
157
155
-
156
158
1. Initialize a Bee object using the API endpoint of a Bee node:
// 5) Log the file name and contents to the terminal
262
-
console.log(page.name)// "root.txt"
263
-
console.log(page.data.toUtf8())// prints file content
274
+
console.log(page.name);// "root.txt"
275
+
console.log(page.data.toUtf8());// prints file content
264
276
265
-
console.log(stylesheet.name)// "example.txt"
266
-
console.log(stylesheet.data.toUtf8())// prints file content
277
+
console.log(stylesheet.name);// "example.txt"
278
+
console.log(stylesheet.data.toUtf8());// prints file content
267
279
```
268
280
269
-
270
281
## Upload & Download with Swarm CLI
271
282
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.
283
+
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
284
274
285
Refer to [the official README](https://github.com/ethersphere/swarm-cli/blob/master/README.md) for a more complete usage guide.
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).
370
+
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).
0 commit comments