Skip to content

Commit 6eefd94

Browse files
authored
s3: add client api to interact with s3 server (#87)
* s3: add preliminary support to interact with s3 * s3: add more functionality and split in two files
1 parent afb8bc2 commit 6eefd94

File tree

4 files changed

+138
-3
lines changed

4 files changed

+138
-3
lines changed

faasmctl/tasks/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from . import logs
1010
from . import monitor
1111
from . import restart
12+
from . import s3
1213
from . import scale
1314
from . import status
1415
from . import upload
@@ -23,6 +24,7 @@
2324
logs,
2425
monitor,
2526
restart,
27+
s3,
2628
scale,
2729
status,
2830
upload,

faasmctl/tasks/s3.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from invoke import task
2+
from faasmctl.util.s3 import (
3+
list_buckets as do_list_buckets,
4+
clear_bucket as do_clear_bucket,
5+
list_objects as do_list_objects,
6+
upload_file as do_upload_file,
7+
upload_dir as do_upload_dir,
8+
dump_object as do_dump_object,
9+
)
10+
11+
12+
@task
13+
def list_buckets(ctx):
14+
"""
15+
List available buckets
16+
"""
17+
do_list_buckets()
18+
19+
20+
@task
21+
def clear_bucket(ctx, bucket):
22+
"""
23+
Clear (i.e. remove) bucket
24+
"""
25+
do_clear_bucket(bucket)
26+
27+
28+
@task
29+
def list_objects(ctx, bucket, recursive=False):
30+
"""
31+
List available objects in bucket
32+
"""
33+
do_list_objects(bucket, recursive)
34+
35+
36+
@task
37+
def upload_file(ctx, bucket, host_path, s3_path):
38+
"""
39+
Upload a file to S3
40+
"""
41+
do_upload_file(bucket, host_path, s3_path)
42+
43+
44+
@task
45+
def upload_dir(ctx, bucket, host_path, s3_path):
46+
"""
47+
Upload all the files in a directory to S3
48+
"""
49+
do_upload_dir(bucket, host_path, s3_path)
50+
51+
52+
@task
53+
def dump_object(ctx, bucket, path):
54+
"""
55+
Dump the contents of an object in S3
56+
"""
57+
response = do_dump_object(bucket, path)
58+
59+
print(response.data)

faasmctl/util/s3.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from faasmctl.util.config import get_faasm_ini_file, get_faasm_ini_value
2+
from minio import Minio
3+
from minio.error import S3Error
4+
from os import listdir
5+
from os.path import isfile, join
6+
7+
8+
def get_minio_client():
9+
minio_port = get_faasm_ini_value(get_faasm_ini_file(), "Faasm", "minio_port")
10+
11+
client = Minio(
12+
"localhost:{}".format(minio_port),
13+
access_key="minio",
14+
secret_key="minio123",
15+
secure=False,
16+
region="",
17+
)
18+
19+
return client
20+
21+
22+
def list_buckets():
23+
client = get_minio_client()
24+
for bucket in client.list_buckets():
25+
print(bucket)
26+
27+
28+
def list_objects(bucket, recursive=False):
29+
client = get_minio_client()
30+
for bucket_key in client.list_objects(bucket, recursive=recursive):
31+
print(bucket_key.object_name)
32+
33+
34+
def clear_bucket(bucket):
35+
client = get_minio_client()
36+
37+
# Got to make sure the bucket is empty first
38+
for bucket_key in client.list_objects(bucket, recursive=True):
39+
client.remove_object(bucket, bucket_key.object_name)
40+
41+
client.remove_bucket(bucket)
42+
43+
44+
def upload_file(bucket, host_path, s3_path):
45+
client = get_minio_client()
46+
47+
# Create the bucket if it does not exist
48+
found = client.bucket_exists(bucket)
49+
if not found:
50+
client.make_bucket(bucket)
51+
52+
# Upload the file, renaming it in the process
53+
try:
54+
client.fput_object(bucket, s3_path, host_path)
55+
except S3Error as ex:
56+
print("error: error uploading file to s3: {}".format(ex))
57+
raise RuntimeError("error: error uploading file to s3")
58+
59+
60+
def upload_dir(bucket, host_path, s3_path):
61+
for f in listdir(host_path):
62+
host_file_path = join(host_path, f)
63+
64+
if isfile(host_file_path):
65+
s3_file_path = join(s3_path, f)
66+
upload_file(bucket, host_file_path, s3_file_path)
67+
68+
69+
def dump_object(bucket, path):
70+
client = get_minio_client()
71+
response = client.get_object(bucket, path)
72+
73+
return response

pyproject.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ classifiers = [
1616
"Operating System :: OS Independent",
1717
]
1818
dependencies = [
19-
"invoke == 2.1.3",
20-
"requests == 2.31.0",
21-
"protobuf == 4.23.4",
19+
"invoke >= 2.1.3",
20+
"requests >= 2.31.0",
21+
"protobuf >= 4.23.4",
22+
"minio >= 7.2.7",
2223
]
2324

2425
[project.urls]

0 commit comments

Comments
 (0)