{ "cells": [ { "cell_type": "markdown", "id": "7652d8ea-a8fc-4e22-9ff2-58bc678c3cf6", "metadata": {}, "source": [ "# S3 User Storage Access" ] }, { "cell_type": "code", "execution_count": 20, "id": "440de59e-eb07-41b8-a3a2-aa6c2d6157ab", "metadata": {}, "outputs": [], "source": [ "import os\n", "import boto3\n", "import s3fs" ] }, { "cell_type": "code", "execution_count": 14, "id": "1407edd6-6e65-414f-ac42-4fcbe85a8228", "metadata": {}, "outputs": [], "source": [ "# start an s3 session with boto3\n", "s3 = boto3.client('s3')\n", "\n", "bucket_name = \"bioscape-smce-user-bucket\"\n", "sub_dir_name = \"edlang\"\n", "object_name = \"example_write.txt\"" ] }, { "cell_type": "code", "execution_count": 15, "id": "387a1bad-c738-4310-b58e-fdfd156d4e28", "metadata": {}, "outputs": [], "source": [ "# creates a dummy file\n", "with open(object_name, 'w') as f:\n", " f.write(\"Hello World!\")" ] }, { "cell_type": "code", "execution_count": 16, "id": "d42f9bef-dfd8-43b9-9550-e27cbdb809cd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File uploaded successfully to s3://bioscape-smce-user-bucket/example_write.txt\n" ] } ], "source": [ "# upload the file with boto3\n", "try:\n", " response = s3.upload_file(\"example_write.txt\", bucket_name, os.path.join(sub_dir_name, object_name))\n", " print(f\"File uploaded successfully to s3://{bucket_name}/{object_name}\")\n", "except Exception as e:\n", " print(f\"Error uploading file: {e}\")" ] }, { "cell_type": "code", "execution_count": 17, "id": "e34d740c-b09c-4352-8541-7ab6e2f81970", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Contents of the file:\n", "Hello World!\n" ] } ], "source": [ "# read the file with boto3\n", "try:\n", " response = s3.get_object(Bucket=bucket_name, Key=os.path.join(sub_dir_name, object_name))\n", " content = response['Body'].read().decode('utf-8')\n", " print(content)\n", "except Exception as e:\n", " print(f\"Error reading file from S3: {e}\")" ] }, { "cell_type": "code", "execution_count": 21, "id": "60750b4a-f444-4910-bc5b-a0696a919676", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World!\n" ] } ], "source": [ "# read the file with s3fs\n", "s3 = s3fs.S3FileSystem(anon=False)\n", "\n", "# Read the file from S3\n", "with s3.open(os.path.join(bucket_name, sub_dir_name, object_name), 'r') as f:\n", " content = f.read()\n", "print(content)" ] } ], "metadata": { "kernelspec": { "display_name": "edlang-edlang-upload", "language": "python", "name": "conda-env-edlang-edlang-upload-py" }, "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.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }