github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/python/examples/sdk/uploading-objects.ipynb (about)

     1  {
     2   "cells": [
     3    {
     4     "cell_type": "markdown",
     5     "source": [
     6      "# Uploading objects to AIS with the SDK"
     7     ],
     8     "metadata": {
     9      "collapsed": false
    10     }
    11    },
    12    {
    13     "cell_type": "code",
    14     "execution_count": null,
    15     "outputs": [],
    16     "source": [
    17      "pip install aistore"
    18     ],
    19     "metadata": {
    20      "collapsed": false
    21     }
    22    },
    23    {
    24     "cell_type": "code",
    25     "execution_count": null,
    26     "outputs": [],
    27     "source": [
    28      "from aistore import Client\n",
    29      "\n",
    30      "# Use the client class to get a reference to a named bucket:\n",
    31      "ais_url = \"http://localhost:8080\"\n",
    32      "client = Client(ais_url)\n",
    33      "\n",
    34      "# All object operations are done within the context of a bucket\n",
    35      "bucket = client.bucket(\"my-bck\")"
    36     ],
    37     "metadata": {
    38      "collapsed": false
    39     }
    40    },
    41    {
    42     "cell_type": "markdown",
    43     "source": [
    44      "## There are 4 primary ways to create objects in AIS with the SDK\n",
    45      "1. object.put_content creates an object with raw bytes\n",
    46      "2. object.put_file creates an object from file contents\n",
    47      "3. object.promote takes files the AIS storage targets can access and promotes them to objects\n",
    48      "4. bucket.put_files puts an entire directory as multiple objects in AIS"
    49     ],
    50     "metadata": {
    51      "collapsed": false
    52     }
    53    },
    54    {
    55     "cell_type": "markdown",
    56     "source": [
    57      "### Object.put_data"
    58     ],
    59     "metadata": {
    60      "collapsed": false
    61     }
    62    },
    63    {
    64     "cell_type": "code",
    65     "execution_count": null,
    66     "outputs": [],
    67     "source": [
    68      "data_obj = bucket.object(\"my-data-object\")\n",
    69      "data_obj.put_content(b\"raw bytes content\")\n",
    70      "bucket.list_objects()"
    71     ],
    72     "metadata": {
    73      "collapsed": false
    74     }
    75    },
    76    {
    77     "cell_type": "markdown",
    78     "source": [
    79      "### Object.put_file"
    80     ],
    81     "metadata": {
    82      "collapsed": false
    83     }
    84    },
    85    {
    86     "cell_type": "code",
    87     "execution_count": null,
    88     "outputs": [],
    89     "source": [
    90      "import tempfile\n",
    91      "\n",
    92      "# Create a temporary file to demonstrate put_file\n",
    93      "with tempfile.NamedTemporaryFile() as f:\n",
    94      "    f.write(b\"content inside of a local file\")\n",
    95      "    f.flush()\n",
    96      "    bucket.object(\"my-file-object\").put_file(f.name)\n",
    97      "bucket.list_objects()"
    98     ],
    99     "metadata": {
   100      "collapsed": false
   101     }
   102    },
   103    {
   104     "cell_type": "markdown",
   105     "source": [
   106      "### Object.promote\n",
   107      "\n",
   108      "This method only works if the filepath provided can be accessed by the AIS storage targets. See [the aiatscale.org blog](https://aiatscale.org/blog/2022/03/17/promote) for details on promoting.\n",
   109      "It will work in a local example if AIS is running on the same machine, shown below."
   110     ],
   111     "metadata": {
   112      "collapsed": false
   113     }
   114    },
   115    {
   116     "cell_type": "code",
   117     "execution_count": null,
   118     "outputs": [],
   119     "source": [
   120      "from pathlib import Path\n",
   121      "import shutil\n",
   122      "\n",
   123      "# Set up an example folder with multiple files\n",
   124      "example_dir = Path().absolute().joinpath(\"promote-example\")\n",
   125      "example_dir.mkdir(exist_ok=True)\n",
   126      "filenames = [f\"file_{i}\" for i in range(10)]\n",
   127      "for filename in filenames:\n",
   128      "    filepath = example_dir.joinpath(filename)\n",
   129      "    with open(filepath, \"w\") as file:\n",
   130      "        file.write(\"content in each test file\")\n",
   131      "# Promote the entire directory (more options available in the docs)\n",
   132      "bucket.object(\"promoted-object\").promote(str(example_dir))\n",
   133      "\n",
   134      "# Delete the example folder\n",
   135      "shutil.rmtree(example_dir)\n",
   136      "\n",
   137      "bucket.list_objects()"
   138     ],
   139     "metadata": {
   140      "collapsed": false
   141     }
   142    },
   143    {
   144     "cell_type": "markdown",
   145     "source": [
   146      "### Bucket.put_files"
   147     ],
   148     "metadata": {
   149      "collapsed": false
   150     }
   151    },
   152    {
   153     "cell_type": "code",
   154     "execution_count": null,
   155     "outputs": [],
   156     "source": [
   157      "# Again, set up an example folder with multiple files\n",
   158      "example_dir = Path().absolute().joinpath(\"put_files-example\")\n",
   159      "example_dir.mkdir(exist_ok=True)\n",
   160      "filenames = [f\"file_{i}\" for i in range(10)]\n",
   161      "for filename in filenames:\n",
   162      "    filepath = example_dir.joinpath(filename)\n",
   163      "    with open(filepath, \"w\") as file:\n",
   164      "        file.write(\"content in each test file\")\n",
   165      "# Put each file in the entire directory (more options available in the docs, including recursive put)\n",
   166      "bucket.put_files(str(example_dir), obj_prefix=\"example-put-\")\n",
   167      "\n",
   168      "# Delete the example folder\n",
   169      "shutil.rmtree(example_dir)\n",
   170      "\n",
   171      "bucket.list_objects()"
   172     ],
   173     "metadata": {
   174      "collapsed": false
   175     }
   176    },
   177    {
   178     "cell_type": "markdown",
   179     "source": [],
   180     "metadata": {
   181      "collapsed": false
   182     }
   183    }
   184   ],
   185   "metadata": {
   186    "kernelspec": {
   187     "display_name": "Python 3",
   188     "language": "python",
   189     "name": "python3"
   190    },
   191    "language_info": {
   192     "codemirror_mode": {
   193      "name": "ipython",
   194      "version": 2
   195     },
   196     "file_extension": ".py",
   197     "mimetype": "text/x-python",
   198     "name": "python",
   199     "nbconvert_exporter": "python",
   200     "pygments_lexer": "ipython2",
   201     "version": "2.7.6"
   202    }
   203   },
   204   "nbformat": 4,
   205   "nbformat_minor": 0
   206  }