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**—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 referencehash**, anyone with that reference can download the content.
8
8
9
9
10
10
**Before you begin:**
11
11
* You need a running Bee node connected to Gnosis Chain and funded with **xBZZ** and **xDAI**.
12
12
* 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**.
14
14
15
15
16
16
## Upload & Download with bee-js
17
17
18
18
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
19
20
+
Refer to the [`bee-js` documentation](https://bee-js.ethswarm.org/) for more usage guides.
21
+
20
22
:::tip
21
-
Some
22
-
**Environment-specific methods**
23
+
**Environment-specific methods:**
23
24
***Browser-only:**[`uploadFiles`](https://bee-js.ethswarm.org/docs/api/classes/Bee/#uploadfiles) (multi-file via `File[]`/`FileList`)
24
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`),
25
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)
@@ -29,26 +30,35 @@ Some
29
30
30
31
**Step-by-step Walkthrough:**
31
32
32
-
1. Create a Bee client
33
+
1. Create a Bee client:
34
+
33
35
`const bee = new Bee("http://localhost:1633")`
34
36
35
-
2. Buy storage (postage stamp batch)
37
+
2. Buy storage (postage stamp batch) by specifying storage size and duration:
@@ -126,7 +150,7 @@ console.log(downloaded.data.toUtf8()) // prints file content
126
150
127
151
### Multiple files — Browser
128
152
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**.
130
154
131
155
132
156
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
143
167
]
144
168
```
145
169
146
-
4. Upload multiple files (collection) → get collection reference
170
+
4. Upload multiple files (collection) → get collection reference:
147
171
`const res = await bee.uploadFiles(batchId, files)`
148
172
149
-
5. Download files by relative path
173
+
5. Download files by relative paths:
150
174
`const logo = await bee.downloadFile(res.reference, "images/logo.png")`
151
175
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`
console.log(stylesheet.data.toUtf8()) // prints file content
230
267
```
231
268
232
-
> Tip: For **binary** files, don’t convert to UTF-8 — log `file.data.length` or write to disk instead.
233
-
234
-
235
269
236
270
## Upload & Download with Swarm CLI
237
271
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.
239
275
240
276
Buy storage via an interactive prompt (capacity + TTL), then upload:
241
277
242
278
```bash
243
279
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`
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.
257
349
350
+
Refer to the [Bee API reference specification](https://docs.ethswarm.org/api/) for detailed usage information.
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**
268
360
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).
0 commit comments