jooneez commited on
Commit
71502ca
Β·
verified Β·
1 Parent(s): d938114

Upload 2 files

Browse files
Files changed (2) hide show
  1. S3Access.ipynb +136 -0
  2. S3AccessWriteBack.ipynb +161 -0
S3Access.ipynb ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "6205f0fb-db74-44d2-bfb1-98760790125c",
7
+ "metadata": {},
8
+ "outputs": [
9
+ {
10
+ "ename": "SyntaxError",
11
+ "evalue": "invalid syntax (3925509796.py, line 1)",
12
+ "output_type": "error",
13
+ "traceback": [
14
+ "\u001b[0;36m Cell \u001b[0;32mIn[1], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m pip install boto3\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
15
+ ]
16
+ }
17
+ ],
18
+ "source": [
19
+ "pip install boto3\n",
20
+ "pip install pandas"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": null,
26
+ "id": "4afe577f-283e-431a-8109-56228af5f203",
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "import boto3\n",
31
+ "import json\n",
32
+ "import pandas as pd\n",
33
+ "import matplotlib.pyplot as plt"
34
+ ]
35
+ },
36
+ {
37
+ "cell_type": "code",
38
+ "execution_count": null,
39
+ "id": "80ea967b-3787-4c5a-b90b-692782bb9460",
40
+ "metadata": {},
41
+ "outputs": [],
42
+ "source": [
43
+ "# Initialize a session using your credentials\n",
44
+ "session = boto3.Session(\n",
45
+ " aws_access_key_id='AKIAZ3CL7RBT67UNF7CU',\n",
46
+ " aws_secret_access_key='1C1gUZflYJ0Q8voiI4Iv+DBBXeLv0w9PQjOZm8Kr',\n",
47
+ " region_name='us-west-2' # e.g., us-east-1\n",
48
+ ")\n",
49
+ "\n",
50
+ "# Create an S3 client using the session\n",
51
+ "s3 = session.client('s3')"
52
+ ]
53
+ },
54
+ {
55
+ "cell_type": "code",
56
+ "execution_count": null,
57
+ "id": "b05018cb-03d4-4d47-9c60-149a8f73cf1c",
58
+ "metadata": {},
59
+ "outputs": [],
60
+ "source": [
61
+ "bucket_name = 'caetestbucket'\n",
62
+ "\n",
63
+ "try:\n",
64
+ " contents = s3.list_objects(Bucket=bucket_name)['Contents']\n",
65
+ " for item in contents:\n",
66
+ " print(item['Key'])\n",
67
+ "except NoCredentialsError:\n",
68
+ " print(\"Credentials not available\")\n",
69
+ "except KeyError:\n",
70
+ " print(\"Bucket is empty or does not exist\")"
71
+ ]
72
+ },
73
+ {
74
+ "cell_type": "code",
75
+ "execution_count": null,
76
+ "id": "028e3460-0485-4bfc-aa41-044276439838",
77
+ "metadata": {},
78
+ "outputs": [],
79
+ "source": [
80
+ "import boto3\n",
81
+ "import json\n",
82
+ "\n",
83
+ "file_key = 'MOCK_VEHICLE.json'\n",
84
+ "\n",
85
+ "response = s3.get_object(Bucket=bucket_name, Key=file_key)\n",
86
+ "# Assuming each line in the file is a separate JSON object\n",
87
+ "vehicle_data_lines = response['Body'].read().decode('utf-8').splitlines()\n",
88
+ "\n",
89
+ "vehicle_data = []\n",
90
+ "for line in vehicle_data_lines:\n",
91
+ " try:\n",
92
+ " vehicle = json.loads(line)\n",
93
+ " vehicle_data.append(vehicle)\n",
94
+ " except json.JSONDecodeError as e:\n",
95
+ " print(f\"Error decoding JSON: {e}\")"
96
+ ]
97
+ },
98
+ {
99
+ "cell_type": "code",
100
+ "execution_count": null,
101
+ "id": "4e4c78c3-bd9d-417e-9b96-9046cce33df2",
102
+ "metadata": {},
103
+ "outputs": [],
104
+ "source": []
105
+ },
106
+ {
107
+ "cell_type": "code",
108
+ "execution_count": null,
109
+ "id": "ebc83569-2645-4f5d-a8c4-1192dd7aa72d",
110
+ "metadata": {},
111
+ "outputs": [],
112
+ "source": []
113
+ }
114
+ ],
115
+ "metadata": {
116
+ "kernelspec": {
117
+ "display_name": "venv",
118
+ "language": "python",
119
+ "name": "venv"
120
+ },
121
+ "language_info": {
122
+ "codemirror_mode": {
123
+ "name": "ipython",
124
+ "version": 3
125
+ },
126
+ "file_extension": ".py",
127
+ "mimetype": "text/x-python",
128
+ "name": "python",
129
+ "nbconvert_exporter": "python",
130
+ "pygments_lexer": "ipython3",
131
+ "version": "3.12.3"
132
+ }
133
+ },
134
+ "nbformat": 4,
135
+ "nbformat_minor": 5
136
+ }
S3AccessWriteBack.ipynb ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 3,
6
+ "id": "6205f0fb-db74-44d2-bfb1-98760790125c",
7
+ "metadata": {},
8
+ "outputs": [
9
+ {
10
+ "name": "stdout",
11
+ "output_type": "stream",
12
+ "text": [
13
+ "Defaulting to user installation because normal site-packages is not writeable\n",
14
+ "Collecting boto3\n",
15
+ " Downloading boto3-1.34.83-py3-none-any.whl.metadata (6.6 kB)\n",
16
+ "Collecting botocore<1.35.0,>=1.34.83 (from boto3)\n",
17
+ " Downloading botocore-1.34.83-py3-none-any.whl.metadata (5.7 kB)\n",
18
+ "Collecting jmespath<2.0.0,>=0.7.1 (from boto3)\n",
19
+ " Downloading jmespath-1.0.1-py3-none-any.whl.metadata (7.6 kB)\n",
20
+ "Collecting s3transfer<0.11.0,>=0.10.0 (from boto3)\n",
21
+ " Downloading s3transfer-0.10.1-py3-none-any.whl.metadata (1.7 kB)\n",
22
+ "Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /opt/anaconda/envs/myenv/lib/python3.8/site-packages (from botocore<1.35.0,>=1.34.83->boto3) (2.9.0)\n",
23
+ "Collecting urllib3<1.27,>=1.25.4 (from botocore<1.35.0,>=1.34.83->boto3)\n",
24
+ " Downloading urllib3-1.26.18-py2.py3-none-any.whl.metadata (48 kB)\n",
25
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m48.9/48.9 kB\u001b[0m \u001b[31m758.9 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m \u001b[36m0:00:01\u001b[0m\n",
26
+ "\u001b[?25hRequirement already satisfied: six>=1.5 in /opt/anaconda/envs/myenv/lib/python3.8/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.35.0,>=1.34.83->boto3) (1.16.0)\n",
27
+ "Downloading boto3-1.34.83-py3-none-any.whl (139 kB)\n",
28
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m139.3/139.3 kB\u001b[0m \u001b[31m2.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
29
+ "\u001b[?25hDownloading botocore-1.34.83-py3-none-any.whl (12.1 MB)\n",
30
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m12.1/12.1 MB\u001b[0m \u001b[31m24.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
31
+ "\u001b[?25hDownloading jmespath-1.0.1-py3-none-any.whl (20 kB)\n",
32
+ "Downloading s3transfer-0.10.1-py3-none-any.whl (82 kB)\n",
33
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m82.2/82.2 kB\u001b[0m \u001b[31m2.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
34
+ "\u001b[?25hDownloading urllib3-1.26.18-py2.py3-none-any.whl (143 kB)\n",
35
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m143.8/143.8 kB\u001b[0m \u001b[31m5.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
36
+ "\u001b[?25hInstalling collected packages: urllib3, jmespath, botocore, s3transfer, boto3\n",
37
+ "Successfully installed boto3-1.34.83 botocore-1.34.83 jmespath-1.0.1 s3transfer-0.10.1 urllib3-1.26.18\n",
38
+ "Note: you may need to restart the kernel to use updated packages.\n"
39
+ ]
40
+ }
41
+ ],
42
+ "source": [
43
+ "pip install boto3"
44
+ ]
45
+ },
46
+ {
47
+ "cell_type": "code",
48
+ "execution_count": 23,
49
+ "id": "4afe577f-283e-431a-8109-56228af5f203",
50
+ "metadata": {},
51
+ "outputs": [],
52
+ "source": [
53
+ "import boto3\n",
54
+ "import json\n",
55
+ "import pandas as pd\n",
56
+ "import matplotlib.pyplot as plt"
57
+ ]
58
+ },
59
+ {
60
+ "cell_type": "code",
61
+ "execution_count": 15,
62
+ "id": "80ea967b-3787-4c5a-b90b-692782bb9460",
63
+ "metadata": {},
64
+ "outputs": [],
65
+ "source": [
66
+ "# Initialize a session using your credentials\n",
67
+ "session = boto3.Session(\n",
68
+ " aws_access_key_id='AKIAZ3CL7RBT67UNF7CU',\n",
69
+ " aws_secret_access_key='1C1gUZflYJ0Q8voiI4Iv+DBBXeLv0w9PQjOZm8Kr',\n",
70
+ " region_name='us-west-2' # e.g., us-east-1\n",
71
+ ")\n",
72
+ "\n",
73
+ "# Create an S3 client using the session\n",
74
+ "s3 = session.client('s3')"
75
+ ]
76
+ },
77
+ {
78
+ "cell_type": "code",
79
+ "execution_count": 21,
80
+ "id": "b05018cb-03d4-4d47-9c60-149a8f73cf1c",
81
+ "metadata": {},
82
+ "outputs": [
83
+ {
84
+ "name": "stdout",
85
+ "output_type": "stream",
86
+ "text": [
87
+ "MOCK_PEOPLE.json\n",
88
+ "MOCK_TRAVEL.json\n",
89
+ "MOCK_VEHICLE.json\n"
90
+ ]
91
+ }
92
+ ],
93
+ "source": [
94
+ "bucket_name = 'caetestbucket'\n",
95
+ "\n",
96
+ "try:\n",
97
+ " contents = s3.list_objects(Bucket=bucket_name)['Contents']\n",
98
+ " for item in contents:\n",
99
+ " print(item['Key'])\n",
100
+ "except NoCredentialsError:\n",
101
+ " print(\"Credentials not available\")\n",
102
+ "except KeyError:\n",
103
+ " print(\"Bucket is empty or does not exist\")"
104
+ ]
105
+ },
106
+ {
107
+ "cell_type": "code",
108
+ "execution_count": 25,
109
+ "id": "028e3460-0485-4bfc-aa41-044276439838",
110
+ "metadata": {},
111
+ "outputs": [],
112
+ "source": [
113
+ "import boto3\n",
114
+ "import json\n",
115
+ "\n",
116
+ "file_key = 'MOCK_VEHICLE.json'\n",
117
+ "\n",
118
+ "response = s3.get_object(Bucket=bucket_name, Key=file_key)\n",
119
+ "# Assuming each line in the file is a separate JSON object\n",
120
+ "vehicle_data_lines = response['Body'].read().decode('utf-8').splitlines()\n",
121
+ "\n",
122
+ "vehicle_data = []\n",
123
+ "for line in vehicle_data_lines:\n",
124
+ " try:\n",
125
+ " vehicle = json.loads(line)\n",
126
+ " vehicle_data.append(vehicle)\n",
127
+ " except json.JSONDecodeError as e:\n",
128
+ " print(f\"Error decoding JSON: {e}\")"
129
+ ]
130
+ },
131
+ {
132
+ "cell_type": "code",
133
+ "execution_count": null,
134
+ "id": "4e4c78c3-bd9d-417e-9b96-9046cce33df2",
135
+ "metadata": {},
136
+ "outputs": [],
137
+ "source": []
138
+ }
139
+ ],
140
+ "metadata": {
141
+ "kernelspec": {
142
+ "display_name": "Python 3 (ipykernel)",
143
+ "language": "python",
144
+ "name": "python3"
145
+ },
146
+ "language_info": {
147
+ "codemirror_mode": {
148
+ "name": "ipython",
149
+ "version": 3
150
+ },
151
+ "file_extension": ".py",
152
+ "mimetype": "text/x-python",
153
+ "name": "python",
154
+ "nbconvert_exporter": "python",
155
+ "pygments_lexer": "ipython3",
156
+ "version": "3.8.19"
157
+ }
158
+ },
159
+ "nbformat": 4,
160
+ "nbformat_minor": 5
161
+ }