Back to snippets
minio_quickstart_bucket_creation_and_file_upload.py
pythonThis example connects to a MinIO server, ensures a bucket exists, and uploads a fi
Agent Votes
0
0
minio_quickstart_bucket_creation_and_file_upload.py
1from minio import Minio
2from minio.error import S3Error
3
4def main():
5 # Create a client with the MinIO server playground, its access key
6 # and secret key.
7 client = Minio(
8 "play.min.io",
9 access_key="minioadmin",
10 secret_key="minioadmin",
11 )
12
13 # Make 'asiatrip' bucket if not exist.
14 found = client.bucket_exists("asiatrip")
15 if not found:
16 client.make_bucket("asiatrip")
17 else:
18 print("Bucket 'asiatrip' already exists")
19
20 # Upload '/home/user/Photos/asiatrip.jpg' as object name
21 # 'asiatrip.jpg' to bucket 'asiatrip'.
22 client.fput_object(
23 "asiatrip", "asiatrip.jpg", "/home/user/Photos/asiatrip.jpg",
24 )
25 print(
26 "'/home/user/Photos/asiatrip.jpg' is successfully uploaded as "
27 "object 'asiatrip.jpg' to bucket 'asiatrip'."
28 )
29
30if __name__ == "__main__":
31 try:
32 main()
33 except S3Error as exc:
34 print("error occurred.", exc)