Skip to content

Commit

Permalink
Merge pull request #2533 from jupyter-naas/2524-feat-create-notebooks…
Browse files Browse the repository at this point in the history
…-for-crud-operations-using-naas-python-for-assets

naas asset api notebook
  • Loading branch information
FlorentLvr authored Jun 28, 2024
2 parents f52cb91 + 66d8b30 commit abc4239
Show file tree
Hide file tree
Showing 10 changed files with 1,899 additions and 23 deletions.
365 changes: 365 additions & 0 deletions Naas API/Naas_API_Create_Asset.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,365 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "28d10a4c-3c77-4088-9b67-db6e5f1ee7f0",
"metadata": {},
"source": [
"<img width=\"8%\" alt=\"Naas API.png\" src=\"https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/.github/assets/logos/Naas%20API.png\" style=\"border-radius: 15%\">"
]
},
{
"cell_type": "markdown",
"id": "90cdfd58-d74b-4ec9-aea8-72fe64903f81",
"metadata": {},
"source": [
"# Naas API - Create Asset"
]
},
{
"cell_type": "markdown",
"id": "9c897c82-4a8f-4f8c-8b00-dd66e496273b",
"metadata": {
"execution": {
"iopub.execute_input": "2024-06-03T14:17:19.410802Z",
"iopub.status.busy": "2024-06-03T14:17:19.410576Z",
"iopub.status.idle": "2024-06-03T14:17:19.420684Z",
"shell.execute_reply": "2024-06-03T14:17:19.419788Z",
"shell.execute_reply.started": "2024-06-03T14:17:19.410779Z"
}
},
"source": [
"**Tags:** #naas #api #asset #create"
]
},
{
"cell_type": "markdown",
"id": "20e4f13f-d7c4-4acf-9c53-880545f0ce76",
"metadata": {
"execution": {
"iopub.execute_input": "2024-06-03T14:17:27.697224Z",
"iopub.status.busy": "2024-06-03T14:17:27.696997Z",
"iopub.status.idle": "2024-06-03T14:17:27.705749Z",
"shell.execute_reply": "2024-06-03T14:17:27.704763Z",
"shell.execute_reply.started": "2024-06-03T14:17:27.697200Z"
}
},
"source": [
"**Author:** [Loic Laville](https://www.linkedin.com/in/loic-laville/)"
]
},
{
"cell_type": "markdown",
"id": "69456534",
"metadata": {},
"source": [
"**Last update:** 2024-06-28 (Created: 2024-06-05)"
]
},
{
"cell_type": "markdown",
"id": "543b22e8",
"metadata": {},
"source": [
"**Description:** This notebook demonstrates how to use create an Asset using Naas API from a share object in Naas API Storage. An \"Asset\" is a shearable link of an object (or file) in your storage.\n",
"\n",
"*Required: A storage with at least one object inside. Please see \"Naas API - Storage\" for more information.*"
]
},
{
"cell_type": "markdown",
"id": "f8fdf193",
"metadata": {},
"source": [
"## Input"
]
},
{
"cell_type": "markdown",
"id": "f9ae1bdf-49b2-4f53-9ae9-b396c4608e9d",
"metadata": {
"execution": {
"iopub.execute_input": "2024-06-03T14:23:07.494752Z",
"iopub.status.busy": "2024-06-03T14:23:07.494526Z",
"iopub.status.idle": "2024-06-03T14:23:07.500018Z",
"shell.execute_reply": "2024-06-03T14:23:07.497930Z",
"shell.execute_reply.started": "2024-06-03T14:23:07.494728Z"
}
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a52f150-e7e4-4415-9013-7dbdaf887892",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"try:\n",
" import naas_python\n",
"except:\n",
" %pip install naas-python --user\n",
" import naas_python"
]
},
{
"cell_type": "markdown",
"id": "d5074443-6174-4f7e-a4ba-ca1ed5b1b1fb",
"metadata": {
"execution": {
"iopub.execute_input": "2024-06-03T14:26:27.969902Z",
"iopub.status.busy": "2024-06-03T14:26:27.969608Z",
"iopub.status.idle": "2024-06-03T14:26:27.972888Z",
"shell.execute_reply": "2024-06-03T14:26:27.972264Z",
"shell.execute_reply.started": "2024-06-03T14:26:27.969871Z"
}
},
"source": [
"### Setup variables"
]
},
{
"cell_type": "markdown",
"id": "a0c5268d-a14d-42c5-bf93-722f6302cc0a",
"metadata": {},
"source": [
"Required:\n",
"- `workspace_id`: User Workspace ID\n",
"- `storage_name`: User Storage name\n",
"- `object_name`: Object file path\n",
"\n",
"Optional:\n",
"- `visibility`: \"public\" (default) or \"private\"\n",
"- `content_disposition`: \"inline\" to be displayed inside browser (default) or \"outline\" to be downloaded\n",
"- `password`: Password for private mode. If not specified a random password will be specified."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c1519102-4298-46e4-a30c-f7a4fd8e9981",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Required\n",
"workspace_id = \"f7a7e3a4-6435-48bc-8da3-88d46161c2b8\"\n",
"storage_name = \"test\"\n",
"object_name = \"object.html\"\n",
"\n",
"# Optional\n",
"visibility = \"public\"\n",
"content_disposition = \"inline\"\n",
"password = None"
]
},
{
"cell_type": "markdown",
"id": "7a680de5",
"metadata": {},
"source": [
"## Model"
]
},
{
"cell_type": "markdown",
"id": "efd00c7b-2b1f-47c4-b8e8-605c2003e321",
"metadata": {},
"source": [
"### Create asset"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "97463fa6-6044-4e69-b4ea-59fc0b243dbd",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"asset = naas_python.asset.create_asset(\n",
" workspace_id=workspace_id,\n",
" asset_creation = {\n",
" \"asset_creation\": {\n",
" \"storage_name\" : storage_name,\n",
" \"object_name\" : object_name,\n",
" \"visibility\" : visibility,\n",
" \"content_disposition\" : content_disposition,\n",
" \"password\": password\n",
" }\n",
" }\n",
")\n",
"asset"
]
},
{
"cell_type": "markdown",
"id": "fbd724f6-7437-4809-8e99-5261ebf2d506",
"metadata": {},
"source": [
"## Output"
]
},
{
"cell_type": "markdown",
"id": "74e034ef-5d00-4cfa-bb07-d79e92fb8b77",
"metadata": {},
"source": [
"### Display Result\n",
"```json\n",
"{'asset': {'id': '0c35c28b-1904-414f-a40d-5611af2511fb',\n",
" 'object_name': 'img.jpg',\n",
" 'visibility': 'private',\n",
" 'content_disposition': 'outline',\n",
" 'password': 'mypassword',\n",
" 'url': 'https://api.naas.ai/workspace/81507a26-3644-496c-a379-4c7021434aa0/asset/4dac01c6-c299-473d-b746-d83f5c16b45e/object/',\n",
" 'workspace_id': '81507a26-3644-496c-a379-4c7021434aa0',\n",
" 'storage_name': 'test-storage',\n",
" 'prefix': '81507a26-3644-496c-a379-4c7021434aa0/test-storage/directory-1',\n",
" 'object_version': 'lqUJfScvcBWUc3RhZuGmB_OkCY0ZmwhI',\n",
" 'content_type': 'image/jpeg',\n",
" 'object_updated_at': '2024-06-03T09:39:03Z',\n",
" 'asset_created_at': '2024-06-03T15:53:20.510333Z',\n",
" 'user_id': '7ed38998-59bb-473f-b14e-983279efbdca',\n",
" 'provider': 'AmazonS3',\n",
" 'provider_bucket_name': 'api-naas-storage'},\n",
" 'error': {'code': 0, 'message': 'Success'}}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c6276b2-8a43-4b79-8a6f-d2f44aeb5b99",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"asset_id = asset['asset']['id']\n",
"asset_id"
]
},
{
"cell_type": "markdown",
"id": "48ebf144",
"metadata": {},
"source": [
"```\n",
"'0c35c28b-1904-414f-a40d-5611af2511fb'\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "35c1d93a-e584-4098-825f-48e581144b49",
"metadata": {
"execution": {
"iopub.execute_input": "2024-06-03T15:57:16.339770Z",
"iopub.status.busy": "2024-06-03T15:57:16.339540Z",
"iopub.status.idle": "2024-06-03T15:57:16.342484Z",
"shell.execute_reply": "2024-06-03T15:57:16.341807Z",
"shell.execute_reply.started": "2024-06-03T15:57:16.339744Z"
}
},
"source": [
"### CLI command"
]
},
{
"cell_type": "markdown",
"id": "ef0ecd66-c59e-43d3-babf-6e921da29d29",
"metadata": {
"execution": {
"iopub.execute_input": "2024-06-03T15:58:10.611202Z",
"iopub.status.busy": "2024-06-03T15:58:10.610977Z",
"iopub.status.idle": "2024-06-03T15:58:10.626998Z",
"shell.execute_reply": "2024-06-03T15:58:10.626067Z",
"shell.execute_reply.started": "2024-06-03T15:58:10.611178Z"
}
},
"source": [
"```sh\n",
"naas-python asset create --help\n",
"\n",
"naas-python asset create \\\n",
"--workspace-id \"81507a26-3644-496c-a379-4c7021434aa0\" \\\n",
"--storage \"test-storage\" \\\n",
"--object \"directory-1/img.jpg\" \\\n",
"--visibility \"private\" \\\n",
"--content-disposition \"outline\" \\\n",
"--password \"mypassword\"\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "0eff6321-cec4-43e3-b66d-3f0d0e4bbc45",
"metadata": {
"tags": []
},
"source": [
"### Info"
]
},
{
"cell_type": "markdown",
"id": "74b8e572-f9f4-47f7-be76-fb2ee1f4d527",
"metadata": {
"execution": {
"iopub.execute_input": "2024-06-03T16:11:25.982662Z",
"iopub.status.busy": "2024-06-03T16:11:25.982335Z",
"iopub.status.idle": "2024-06-03T16:11:26.001623Z",
"shell.execute_reply": "2024-06-03T16:11:25.999844Z",
"shell.execute_reply.started": "2024-06-03T16:11:25.982628Z"
}
},
"source": [
"You can have only one asset by file, otherwise you will get the error :"
]
},
{
"cell_type": "markdown",
"id": "9d714fbe-4b96-4c88-b33e-c4a7e135faed",
"metadata": {
"execution": {
"iopub.execute_input": "2024-06-03T16:11:49.611592Z",
"iopub.status.busy": "2024-06-03T16:11:49.611367Z",
"iopub.status.idle": "2024-06-03T16:11:49.629254Z",
"shell.execute_reply": "2024-06-03T16:11:49.628239Z",
"shell.execute_reply.started": "2024-06-03T16:11:49.611567Z"
}
},
"source": [
"`[AssetConflictError]: Asset:'img.jpg' - id:'0c35c28b-1904-414f-a40d-5611af2511fb' - version:'lqUJfScvcBWUc3RhZuGmB_OkCY0ZmwhI' already exists`"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit abc4239

Please sign in to comment.