You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
depot_django/test_minio_upload.py

59 lines
2.1 KiB
Python

import os
import boto3
from botocore.exceptions import ClientError
def test_minio_upload():
minio_server_url = os.environ.get('MINIO_SERVER_URL', 'http://minio:9000') # Use hardcoded for now
access_key = os.environ.get('MINIO_ACCESS_KEY')
secret_key = os.environ.get('MINIO_SECRET_KEY')
bucket_name = os.environ.get('MINIO_STATIC_BUCKET_NAME', 'static') # Use hardcoded for now
secure = os.environ.get('MINIO_SECURE', 'False').lower() == 'true'
test_file_content = b"This is a test file for MinIO upload."
test_file_name = "test_upload.txt"
print(f"Attempting to upload to MinIO at: {minio_server_url}")
print(f"Bucket: {bucket_name}")
print(f"Access Key: {access_key}")
print(f"Secure connection: {secure}")
try:
s3_client = boto3.client(
's3',
endpoint_url=minio_server_url,
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
use_ssl=secure,
verify=False # For self-signed certs or local testing
)
print(f"Attempting to upload {test_file_name} to {bucket_name}...")
s3_client.put_object(
Bucket=bucket_name,
Key=test_file_name,
Body=test_file_content,
ContentType='text/plain',
ACL='public-read' # Ensure it's publicly readable
)
print(f"Successfully uploaded {test_file_name} to {bucket_name}!")
# Verify upload by listing objects
print(f"Listing objects in {bucket_name}...")
response = s3_client.list_objects_v2(Bucket=bucket_name)
if 'Contents' in response:
for obj in response['Contents']:
print(f" - {obj['Key']}")
else:
print(" No objects found.")
except ClientError as e:
print(f"MinIO ClientError: {e}")
print(f"Error Code: {e.response.get('Error', {}).get('Code')}")
print(f"Error Message: {e.response.get('Error', {}).get('Message')}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
test_minio_upload()