github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/python/examples/sdk/accessing-objects.ipynb (about) 1 { 2 "cells": [ 3 { 4 "cell_type": "markdown", 5 "metadata": { 6 "collapsed": false 7 }, 8 "source": [ 9 "## Accessing AIStore objects with the SDK" 10 ] 11 }, 12 { 13 "cell_type": "code", 14 "execution_count": null, 15 "metadata": { 16 "collapsed": false 17 }, 18 "outputs": [], 19 "source": [ 20 "pip install aistore" 21 ] 22 }, 23 { 24 "cell_type": "markdown", 25 "metadata": { 26 "collapsed": false 27 }, 28 "source": [ 29 "### Use the client class to get a reference to a bucket:" 30 ] 31 }, 32 { 33 "cell_type": "code", 34 "execution_count": null, 35 "metadata": { 36 "collapsed": false 37 }, 38 "outputs": [], 39 "source": [ 40 "from aistore import Client\n", 41 "from pathlib import Path\n", 42 "\n", 43 "ais_url = \"http://localhost:8080\"\n", 44 "client = Client(ais_url)\n", 45 "bucket = client.bucket(\"my-bck\").create(exist_ok=True)" 46 ] 47 }, 48 { 49 "cell_type": "markdown", 50 "metadata": { 51 "collapsed": false 52 }, 53 "source": [ 54 "### First, create a few objects" 55 ] 56 }, 57 { 58 "cell_type": "code", 59 "execution_count": null, 60 "metadata": { 61 "collapsed": false 62 }, 63 "outputs": [], 64 "source": [ 65 "object_names = [f\"example_obj_{i}\" for i in range(10)]\n", 66 "for name in object_names:\n", 67 " bucket.object(name).put_content(b\"object content\")\n", 68 "# Create one with a different prefix\n", 69 "bucket.object(\"prefix-example\").put_content(b\"prefixed object content\")" 70 ] 71 }, 72 { 73 "cell_type": "markdown", 74 "metadata": { 75 "collapsed": false 76 }, 77 "source": [ 78 "## There are 3 ways to list the objects inside a bucket\n", 79 "1. list_objects provides a page of objects and the data to get the next page\n", 80 "2. list_objects_iter returns an iterator over all objects\n", 81 "3. list_all_objects returns a single list of all objects (calling list_objects until exhausted)" 82 ] 83 }, 84 { 85 "cell_type": "markdown", 86 "metadata": { 87 "collapsed": false 88 }, 89 "source": [ 90 "### list_objects" 91 ] 92 }, 93 { 94 "cell_type": "code", 95 "execution_count": null, 96 "metadata": { 97 "collapsed": false 98 }, 99 "outputs": [], 100 "source": [ 101 "objects = []\n", 102 "response = bucket.list_objects(page_size=3)\n", 103 "objects.extend(response.entries)\n", 104 "# Use the info from the previous response to get the next page of objects\n", 105 "bucket.list_objects(\n", 106 " uuid=response.uuid, continuation_token=response.continuation_token, page_size=3\n", 107 ")\n", 108 "objects.extend(response.entries)\n", 109 "# Now we should have the first 2 pages of size 3 each\n", 110 "print(objects)\n", 111 "print(\"Total number of objects:\", len(objects))" 112 ] 113 }, 114 { 115 "cell_type": "markdown", 116 "metadata": { 117 "collapsed": false 118 }, 119 "source": [ 120 "### list_objects_iter" 121 ] 122 }, 123 { 124 "cell_type": "code", 125 "execution_count": null, 126 "metadata": { 127 "collapsed": false 128 }, 129 "outputs": [], 130 "source": [ 131 "iterator = bucket.list_objects_iter(prefix=\"example_\", props=\"name,size,copies\")\n", 132 "for bucket_entry in iterator:\n", 133 " print(bucket_entry)" 134 ] 135 }, 136 { 137 "cell_type": "markdown", 138 "metadata": { 139 "collapsed": false 140 }, 141 "source": [ 142 "### list_all_objects" 143 ] 144 }, 145 { 146 "cell_type": "code", 147 "execution_count": null, 148 "metadata": { 149 "collapsed": false 150 }, 151 "outputs": [], 152 "source": [ 153 "bucket.list_all_objects(prefix=\"example_\")" 154 ] 155 }, 156 { 157 "cell_type": "markdown", 158 "metadata": { 159 "collapsed": false 160 }, 161 "source": [ 162 "## Call head() on an object to view properties" 163 ] 164 }, 165 { 166 "cell_type": "code", 167 "execution_count": null, 168 "metadata": { 169 "collapsed": false 170 }, 171 "outputs": [], 172 "source": [ 173 "bucket.object(object_names[0]).head()" 174 ] 175 }, 176 { 177 "cell_type": "markdown", 178 "metadata": { 179 "collapsed": false 180 }, 181 "source": [ 182 "## Object.get returns an ObjectReader object with a few ways to access the data" 183 ] 184 }, 185 { 186 "cell_type": "code", 187 "execution_count": null, 188 "metadata": { 189 "collapsed": false 190 }, 191 "outputs": [], 192 "source": [ 193 "object_reader = bucket.object(object_names[0]).get()\n", 194 "print(\"Read all from the stream:\", object_reader.read_all())\n", 195 "print(\"Raw response:\", object_reader.raw())" 196 ] 197 }, 198 { 199 "cell_type": "markdown", 200 "metadata": { 201 "collapsed": false 202 }, 203 "source": [ 204 "### For larger objects, you can read in chunks" 205 ] 206 }, 207 { 208 "cell_type": "code", 209 "execution_count": null, 210 "metadata": { 211 "collapsed": false 212 }, 213 "outputs": [], 214 "source": [ 215 "print(\"Chunks:\")\n", 216 "for chunk in bucket.object(object_names[0]).get(chunk_size=3):\n", 217 " print(chunk)" 218 ] 219 }, 220 { 221 "cell_type": "markdown", 222 "metadata": { 223 "collapsed": false 224 }, 225 "source": [ 226 "### Or you can provide your own writer" 227 ] 228 }, 229 { 230 "cell_type": "code", 231 "execution_count": null, 232 "metadata": { 233 "collapsed": false 234 }, 235 "outputs": [], 236 "source": [ 237 "# Pass a writer that appends to a file\n", 238 "filename = Path().absolute().joinpath(\"example-obj-writer\")\n", 239 "with open(filename, \"ab\") as writer:\n", 240 " bucket.object(object_names[0]).get(writer=writer)\n", 241 "\n", 242 "# Read from the file to see the output\n", 243 "with open(filename, \"rb\") as reader:\n", 244 " print(reader.read())\n", 245 "\n", 246 "filename.unlink()" 247 ] 248 }, 249 { 250 "attachments": {}, 251 "cell_type": "markdown", 252 "metadata": { 253 "collapsed": false 254 }, 255 "source": [ 256 "## Working with multiple objects\n", 257 "AIS supports multi-object operations on groups of objects. For examples of working with groups of objects see [here](https://github.com/NVIDIA/aistore/blob/master/python/examples/sdk/multi-object-operations.ipynb)" 258 ] 259 } 260 ], 261 "metadata": { 262 "kernelspec": { 263 "display_name": "Python 3", 264 "language": "python", 265 "name": "python3" 266 }, 267 "language_info": { 268 "codemirror_mode": { 269 "name": "ipython", 270 "version": 2 271 }, 272 "file_extension": ".py", 273 "mimetype": "text/x-python", 274 "name": "python", 275 "nbconvert_exporter": "python", 276 "pygments_lexer": "ipython2", 277 "version": "3.11.1 (main, Dec 7 2022, 01:11:34) [GCC 11.3.0]" 278 }, 279 "vscode": { 280 "interpreter": { 281 "hash": "ead1b95f633dc9c51826328e1846203f51a198c6fb5f2884a80417ba131d4e82" 282 } 283 } 284 }, 285 "nbformat": 4, 286 "nbformat_minor": 0 287 }