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 | ```
|
47 | new Promise((resolve), (reject) => {
|
48 | blockstack.mkdir("/home")
|
49 | .then(() => {
|
50 |
|
51 | return blockstack.mkdir("/home/demo1");
|
52 | })
|
53 | .then(() => {
|
54 |
|
55 | return blockstack.mkdir("/home/demo2");
|
56 | })
|
57 | .then(() => {
|
58 |
|
59 | return blockstack.mkdir("/home/demo3");
|
60 | })
|
61 | .then(() => {
|
62 |
|
63 | // directory '/home' exists, and has
|
64 | // children 'demo1', 'demo2', and 'demo3'
|
65 | resolve(true);
|
66 | });
|
67 | });
|
68 | ```
|
69 |
|
70 | ## Listing directories
|
71 |
|
72 | ```
|
73 | new Promise((resolve), (reject) => {
|
74 | blockstack.listdir("/home")
|
75 | .then((dir) => {
|
76 |
|
77 | // have 'demo1', 'demo2', and 'demo3'
|
78 | assert(dir.children.length === 3);
|
79 |
|
80 | for (let name of ['demo1', 'demo2', 'demo3']) {
|
81 | if( !Object.keys(dir['children']).includes(name) ) {
|
82 | reject(`Missing ${name}`);
|
83 | }
|
84 | }
|
85 |
|
86 | resolve(true);
|
87 | });
|
88 | });
|
89 | ```
|
90 |
|
91 | ## Stat path
|
92 |
|
93 | ```
|
94 | new Promise((resolve), (reject) => {
|
95 | blockstack.stat("/home")
|
96 | .then((dirHeader) => {
|
97 | // path exists
|
98 | resolve(true);
|
99 | })
|
100 | .catch((error) => {
|
101 | // path does not exist
|
102 | reject(error);
|
103 | });
|
104 | });
|
105 | ```
|
106 |
|
107 | ## Deleting a file
|
108 |
|
109 | ```
|
110 | new Promise((resolve), (reject) => {
|
111 | blockstack.deleteFile("/hello_world")
|
112 | .then(() => {
|
113 | // file was deleted
|
114 | resolve(true);
|
115 | })
|
116 | .catch((error) => {
|
117 | // file does not exist or is inaccessable
|
118 | reject(error);
|
119 | });
|
120 | });
|
121 | ```
|
122 |
|
123 |
|
124 | ## Removing a directory
|
125 |
|
126 | ```
|
127 | new Promise((resolve), (reject) => {
|
128 | blockstack.rmdir("/home/demo1")
|
129 | .then(() => {
|
130 | // can delete empty directories,
|
131 | // but not non-empty ones.
|
132 | return blockstack.rmdir("/home")
|
133 | .catch((error) => {
|
134 |
|
135 | // delete children
|
136 | return Promise.all([blockstack.rmdir("/home/demo2"), blockstack.rmdir("/home/demo3")]);
|
137 | })
|
138 | .then((results) => {
|
139 |
|
140 | // delete parent
|
141 | return blockstack.rmdir("/home");
|
142 | })
|
143 | .then(() => {
|
144 |
|
145 | // / is now empty
|
146 | resolve(true);
|
147 | });
|
148 | });
|
149 | });
|
150 | ```
|
151 |
|