Fix s3 and add new DB fields
This commit is contained in:
@@ -52,24 +52,44 @@ def upload_asset(
|
||||
FileNotFoundError: If the file doesn't exist
|
||||
ClientError: If the API returns an error
|
||||
"""
|
||||
|
||||
# Enable boto3 debug logging to see request/response details
|
||||
# import logging
|
||||
# boto3.set_stream_logger('botocore', logging.DEBUG)
|
||||
# logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
|
||||
userinfo = requests.get(f"{base_url}/api/userinfo", headers={"Authorization": f"Bearer {api_key}"})
|
||||
userinfo.raise_for_status()
|
||||
user_data = userinfo.json()
|
||||
|
||||
s3creds = requests.post(f"{base_url}/api/s3keys", headers={"Authorization": f"Bearer {api_key}"})
|
||||
s3creds.raise_for_status()
|
||||
s3_creds = s3creds.json()
|
||||
|
||||
|
||||
access_key = s3_creds.get("access_key")
|
||||
secret_key = s3_creds.get("secret_key")
|
||||
|
||||
print(f"\nS3 Credentials obtained:")
|
||||
print(f" Access Key: {access_key}")
|
||||
print(f" Secret Key: {secret_key[:10]}...{secret_key[-10:]}") # Masked for security
|
||||
print(f" Bucket: {user_data.get('bucket')}")
|
||||
|
||||
file_path_obj = Path(file_path)
|
||||
if not file_path_obj.exists():
|
||||
raise FileNotFoundError(f"File not found: {file_path}")
|
||||
|
||||
# Configure boto3 client for S3-compatible API
|
||||
print(f"\nConfiguring boto3 client:")
|
||||
print(f" Endpoint URL: {base_url}/s3")
|
||||
print(f" Region: us-east-1")
|
||||
print(f" Signature Version: s3v4")
|
||||
print(f" Addressing Style: path")
|
||||
|
||||
s3_client = boto3.client(
|
||||
"s3",
|
||||
endpoint_url=f"{base_url}/s3",
|
||||
aws_access_key_id=api_key,
|
||||
aws_secret_access_key="", # Not used for API key auth
|
||||
aws_access_key_id=access_key,
|
||||
aws_secret_access_key=secret_key,
|
||||
region_name="us-east-1",
|
||||
config=Config(
|
||||
signature_version="s3v4",
|
||||
@@ -80,11 +100,21 @@ def upload_asset(
|
||||
try:
|
||||
# Upload the file
|
||||
file_size = file_path_obj.stat().st_size
|
||||
print(f"Uploading {file_path} ({file_size} bytes) as '{key}'...")
|
||||
bucket = user_data['bucket']
|
||||
|
||||
# print(f"\n{'='*60}")
|
||||
# print(f"Starting upload:")
|
||||
# print(f" File: {file_path}")
|
||||
# print(f" Size: {file_size} bytes ({file_size / (1024*1024):.2f} MB)")
|
||||
# print(f" Bucket: {bucket}")
|
||||
# print(f" Key: {key}")
|
||||
# print(f" Endpoint: {base_url}/s3/{bucket}/{key}")
|
||||
# print(f"{'='*60}\n")
|
||||
|
||||
with open(file_path, "rb") as f:
|
||||
print(f"Opened file: {file_path}")
|
||||
response = s3_client.put_object(
|
||||
Bucket=user_data['bucket'],
|
||||
Bucket=bucket,
|
||||
Key=key,
|
||||
Body=f,
|
||||
)
|
||||
@@ -102,10 +132,23 @@ def upload_asset(
|
||||
except ClientError as e:
|
||||
error_code = e.response.get("Error", {}).get("Code")
|
||||
error_message = e.response.get("Error", {}).get("Message")
|
||||
# status_code = e.response.get("ResponseMetadata", {}).get("HTTPStatusCode")
|
||||
# headers = e.response.get("ResponseMetadata", {}).get("HTTPHeaders", {})
|
||||
#
|
||||
# print(f"\n{'='*60}", file=sys.stderr)
|
||||
print(f"Error uploading file: {error_code} - {error_message}", file=sys.stderr)
|
||||
# print(f"HTTP Status Code: {status_code}", file=sys.stderr)
|
||||
# print(f"Response Headers: {headers}", file=sys.stderr)
|
||||
# print(f"Full Response: {e.response}", file=sys.stderr)
|
||||
# print(f"{'='*60}\n", file=sys.stderr)
|
||||
raise
|
||||
except Exception as e:
|
||||
print(f"\n{'='*60}", file=sys.stderr)
|
||||
print(f"Unexpected error: {e}", file=sys.stderr)
|
||||
print(f"Error type: {type(e).__name__}", file=sys.stderr)
|
||||
import traceback
|
||||
traceback.print_exc(file=sys.stderr)
|
||||
print(f"{'='*60}\n", file=sys.stderr)
|
||||
raise
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user