1 | # Blockstack Storage JS
|
2 |
|
3 | Blockstack storage API access for Javascript clients.
|
4 |
|
5 | ## Creating a file
|
6 |
|
7 | ```
|
8 | new Promise((resolve), (reject) => {
|
9 | blockstack.putFile("/hello_world", "hello world!")
|
10 | .then(() => {
|
11 |
|
12 | // /hello_world exists now, and has the contents "hello world!".
|
13 | resolve(true);
|
14 | });
|
15 | });
|
16 | ```
|
17 |
|
18 | ## Reading a file
|
19 |
|
20 | ```
|
21 | new Promise((resolve), (reject) => {
|
22 | blockstack.getFile("/hello_world")
|
23 | .then((fileContents) => {
|
24 |
|
25 | // get back the file /hello_world
|
26 | assert(fileContents === "hello world!");
|
27 | resolve(true);
|
28 | });
|
29 | });
|
30 | ```
|
31 |
|
32 | ```
|
33 | new Promise((resolve), (reject) => {
|
34 | blockstack.getFile("/non/existant/file")
|
35 | .then((absentFileContents) => {
|
36 |
|
37 | // no data if it doesn't exist
|
38 | assert(absentFileContents === null);
|
39 | resolve(true);
|
40 | });
|
41 | });
|
42 | ```
|
43 |
|
44 | ## Making a directory
|
45 |
|
46 | NOTE: this is not part of the "stable" API. Do not rely on it.
|
47 |
|
48 | ```
|
49 | new Promise((resolve), (reject) => {
|
50 | blockstack.mkdir("/home")
|
51 | .then(() => {
|
52 |
|
53 | return blockstack.mkdir("/home/demo1");
|
54 | })
|
55 | .then(() => {
|
56 |
|
57 | return blockstack.mkdir("/home/demo2");
|
58 | })
|
59 | .then(() => {
|
60 |
|
61 | return blockstack.mkdir("/home/demo3");
|
62 | })
|
63 | .then(() => {
|
64 |
|
65 | // directory '/home' exists, and has
|
66 | // children 'demo1', 'demo2', and 'demo3'
|
67 | resolve(true);
|
68 | });
|
69 | });
|
70 | ```
|
71 |
|
72 | ## Listing directories
|
73 |
|
74 | NOTE: this is not part of the "stable" API. Do not rely on it.
|
75 |
|
76 | ```
|
77 | new Promise((resolve), (reject) => {
|
78 | blockstack.listdir("/home")
|
79 | .then((dir) => {
|
80 |
|
81 | // have 'demo1', 'demo2', and 'demo3'
|
82 | assert(dir.children.length === 3);
|
83 |
|
84 | for (let name of ['demo1', 'demo2', 'demo3']) {
|
85 | if( !Object.keys(dir['children']).includes(name) ) {
|
86 | reject(`Missing ${name}`);
|
87 | }
|
88 | }
|
89 |
|
90 | resolve(true);
|
91 | });
|
92 | });
|
93 | ```
|
94 |
|
95 | ## Stat path
|
96 |
|
97 | ```
|
98 | new Promise((resolve), (reject) => {
|
99 | blockstack.stat("/home")
|
100 | .then((dirHeader) => {
|
101 | // path exists
|
102 | resolve(true);
|
103 | })
|
104 | .catch((error) => {
|
105 | // path does not exist
|
106 | reject(error);
|
107 | });
|
108 | });
|
109 | ```
|
110 |
|
111 | ## Deleting a file
|
112 |
|
113 | ```
|
114 | new Promise((resolve), (reject) => {
|
115 | blockstack.deleteFile("/hello_world")
|
116 | .then(() => {
|
117 | // file was deleted
|
118 | resolve(true);
|
119 | })
|
120 | .catch((error) => {
|
121 | // file does not exist or is inaccessable
|
122 | reject(error);
|
123 | });
|
124 | });
|
125 | ```
|
126 |
|
127 |
|
128 | ## Removing a directory
|
129 |
|
130 | NOTE: this is not part of the "stable" API. Do not rely on it.
|
131 |
|
132 | ```
|
133 | new Promise((resolve), (reject) => {
|
134 | blockstack.rmdir("/home/demo1")
|
135 | .then(() => {
|
136 | // can delete empty directories,
|
137 | // but not non-empty ones.
|
138 | return blockstack.rmdir("/home")
|
139 | .catch((error) => {
|
140 |
|
141 | // delete children
|
142 | return Promise.all([blockstack.rmdir("/home/demo2"), blockstack.rmdir("/home/demo3")]);
|
143 | })
|
144 | .then((results) => {
|
145 |
|
146 | // delete parent
|
147 | return blockstack.rmdir("/home");
|
148 | })
|
149 | .then(() => {
|
150 |
|
151 | // / is now empty
|
152 | resolve(true);
|
153 | });
|
154 | });
|
155 | });
|
156 | ```
|
157 |
|