-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathminio_example.py
More file actions
120 lines (98 loc) · 3.89 KB
/
Copy pathminio_example.py
File metadata and controls
120 lines (98 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import io
import json
from datetime import timedelta
from minio import Minio
from testcontainers.community.minio import MinioContainer
def basic_example():
with MinioContainer() as minio:
# Get connection parameters
host = minio.get_container_host_ip()
port = minio.get_exposed_port(minio.port)
access_key = minio.access_key
secret_key = minio.secret_key
# Create MinIO client
client = Minio(f"{host}:{port}", access_key=access_key, secret_key=secret_key, secure=False)
print("Connected to MinIO")
# Create bucket
bucket_name = "test-bucket"
client.make_bucket(bucket_name)
print(f"Created bucket: {bucket_name}")
# List buckets
buckets = client.list_buckets()
print("\nBuckets:")
for bucket in buckets:
print(f"- {bucket.name} (created: {bucket.creation_date})")
# Upload test files
test_files = {"test1.txt": "Hello from test1", "test2.txt": "Hello from test2", "test3.txt": "Hello from test3"}
for filename, content in test_files.items():
data = io.BytesIO(content.encode())
client.put_object(bucket_name, filename, data, len(content.encode()), content_type="text/plain")
print(f"Uploaded {filename}")
# List objects
objects = client.list_objects(bucket_name)
print("\nObjects in bucket:")
for obj in objects:
print(f"- {obj.object_name} (size: {obj.size} bytes)")
# Get object
print("\nObject contents:")
for filename in test_files:
response = client.get_object(bucket_name, filename)
content = response.read().decode()
print(f"{filename}: {content}")
# Create directory structure
client.put_object(
bucket_name, "folder1/test4.txt", io.BytesIO(b"Hello from test4"), 15, content_type="text/plain"
)
print("\nCreated directory structure")
# List objects with prefix
objects = client.list_objects(bucket_name, prefix="folder1/")
print("\nObjects in folder1:")
for obj in objects:
print(f"- {obj.object_name}")
# Copy object
client.copy_object(bucket_name, "test1.txt", f"{bucket_name}/folder1/test1_copy.txt")
print("\nCopied object")
# Get object metadata
stat = client.stat_object(bucket_name, "test1.txt")
print("\nObject metadata:")
print(
json.dumps(
{
"name": stat.object_name,
"size": stat.size,
"content_type": stat.content_type,
"last_modified": stat.last_modified.isoformat(),
},
indent=2,
)
)
# Generate presigned URL
url = client.presigned_get_object(bucket_name, "test1.txt", expires=timedelta(hours=1))
print(f"\nPresigned URL: {url}")
# Set bucket policy
policy = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {"AWS": "*"},
"Action": ["s3:GetObject"],
"Resource": [f"arn:aws:s3:::{bucket_name}/*"],
}
],
}
client.set_bucket_policy(bucket_name, json.dumps(policy))
print("\nSet bucket policy")
# Get bucket policy
policy = client.get_bucket_policy(bucket_name)
print("\nBucket policy:")
print(json.dumps(json.loads(policy), indent=2))
# Remove objects
for filename in test_files:
client.remove_object(bucket_name, filename)
print(f"Removed {filename}")
# Remove bucket
client.remove_bucket(bucket_name)
print(f"\nRemoved bucket: {bucket_name}")
if __name__ == "__main__":
basic_example()