Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] RHOAIENG-664: Adding examples directory upstream #385

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
259 changes: 259 additions & 0 deletions examples/s3client_examples/s3client_examples.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "7bd9e936-d099-44e0-abec-ccc89f1ef3ac",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"#Upgrade pip to the latest version\n",
"!pip3 install --upgrade pip\n",
"\n",
"#Install boto3\n",
"!pip3 install boto3\n",
"\n",
"#Import boto3 libraries\n",
"import os \n",
"import boto3 \n",
"from botocore.client import Config\n",
"from boto3 import session\n",
"\n",
"#show Boto3 package version\n",
"!pip3 show boto3"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "79a496c7-f97b-462b-a1d6-861151c7058e",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"#Create an S3 client\n",
"\n",
"#Define credentials\n",
"key_id = os.environ.get('AWS_ACCESS_KEY_ID') \n",
"secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY') \n",
"endpoint = os.environ.get('AWS_S3_ENDPOINT')\n",
"region = os.environ.get('AWS_DEFAULT_REGION')\n",
"\n",
"#Define client session\n",
"session = boto3.session.Session(aws_access_key_id=key_id, \n",
"aws_secret_access_key=secret_key) \n",
"\n",
"#Define client connection\n",
"s3_client = boto3.client('s3', aws_access_key_id=key_id, \n",
"aws_secret_access_key=secret_key,aws_session_token=None,\n",
" config=boto3.session.Config(signature_version='s3v4'),\n",
" endpoint_url=endpoint,\n",
" region_name=region) "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "63dba9c1-0259-47d1-aad4-40f833a9fee4",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"#List available buckets\n",
"s3_client.list_buckets() "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Print names of available buckets\n",
"for bucket in s3_client.list_buckets()['Buckets']: \n",
" print(bucket['Name']) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#List files in a bucket\n",
"bucket_name = '<bucket_name>' \n",
"s3_client.list_objects_v2(Bucket=bucket_name) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Print only names of files\n",
"bucket_name = '<bucket_name>' \n",
"for key in s3_client.list_objects_v2(Bucket=bucket_name)['Contents']: \n",
" print(key['Key']) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Create bucket\n",
"s3_client.create_bucket(Bucket='<bucket_name>')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#List buckets - verify new bucket exists\n",
"for bucket in s3_client.list_buckets()['Buckets']: \n",
" print(bucket['Name'])"
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "131de47b-dc66-4def-ba53-a4d60317c2c8",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"#Upload file to bucket\n",
"s3_client.upload_file('<file_name>', '<bucket_name>', '<object_name>')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#List files to verify file has been uploaded\n",
"for key in s3_client.list_objects_v2(Bucket='<bucket_name>')['Contents']: \n",
" print(key['Key']) "
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "88f61489-3db6-487f-8d98-d41f67e1cb27",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"#Download file from bucket\n",
"s3_client.download_file('<bucket_name>', '<object_name>', '<file_name>')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Copy files between buckets\n",
"copy_source = {\n",
" 'Bucket': '<bucket_name>',\n",
" 'Key': '<key>'\n",
"}\n",
"s3_client.copy(copy_source, '<destination bucket>', '<destination_key>')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#List files in destination bucket to verify that the file has been copied\n",
"bucket_name = '<bucket_name>' \n",
"for key in s3_client.list_objects_v2(Bucket=bucket_name)['Contents']:\n",
" print(key['Key'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Delete file from bucket\n",
"s3_client.delete_object(Bucket='<bucket_name>', Key='<object_key>')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#List files to verify that the file has been deleted\n",
"bucket_name = '<bucket_name>' \n",
"for key in s3_client.list_objects_v2(Bucket=bucket_name)['Contents']:\n",
" print(key['Key'])\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Delete bucket\n",
"s3_client.delete_bucket(Bucket='<bucket_name>')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#List buckets to verify that the bucket has been deleted\n",
"for bucket in s3_client.list_buckets()['Buckets']: \n",
" print(bucket['Name'])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9",
"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.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}