feature/file-manager-user-only-read
This commit is contained in:
@@ -5,7 +5,7 @@ Las claves de objeto deben generarse con ``core.s3_keys`` (p. ej. ``csv_import_k
|
||||
``s3_key_for_csv_import``); no construir prefijos ``tenants/...`` aquí.
|
||||
"""
|
||||
import logging
|
||||
from typing import Optional
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
import boto3
|
||||
from botocore.config import Config
|
||||
@@ -111,6 +111,59 @@ def presigned_get_url(key: str, expires_in: Optional[int] = None) -> str:
|
||||
)
|
||||
|
||||
|
||||
def list_objects_tree(
|
||||
prefix: str,
|
||||
delimiter: str = "/",
|
||||
max_keys: int = 100,
|
||||
continuation_token: Optional[str] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Lista objetos/prefijos como árbol virtual.
|
||||
|
||||
Retorna:
|
||||
- ``prefixes``: subcarpetas (CommonPrefixes)
|
||||
- ``objects``: objetos directos bajo ``prefix``
|
||||
- ``next_continuation_token`` y ``is_truncated`` para paginación
|
||||
"""
|
||||
params: Dict[str, Any] = {
|
||||
"Bucket": settings.S3_BUCKET,
|
||||
"Prefix": prefix,
|
||||
"Delimiter": delimiter,
|
||||
"MaxKeys": max(1, min(int(max_keys), 500)),
|
||||
}
|
||||
if continuation_token:
|
||||
params["ContinuationToken"] = continuation_token
|
||||
|
||||
resp = _client().list_objects_v2(**params)
|
||||
common_prefixes: List[str] = [
|
||||
p.get("Prefix", "") for p in (resp.get("CommonPrefixes") or []) if p.get("Prefix")
|
||||
]
|
||||
objects: List[Dict[str, Any]] = []
|
||||
for obj in resp.get("Contents") or []:
|
||||
key = obj.get("Key")
|
||||
if not key:
|
||||
continue
|
||||
if key == prefix:
|
||||
# Marcador de carpeta (objeto vacío con mismo nombre del prefijo).
|
||||
continue
|
||||
objects.append(
|
||||
{
|
||||
"key": key,
|
||||
"size": int(obj.get("Size", 0) or 0),
|
||||
"last_modified": obj.get("LastModified"),
|
||||
"etag": obj.get("ETag"),
|
||||
"storage_class": obj.get("StorageClass"),
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
"prefixes": common_prefixes,
|
||||
"objects": objects,
|
||||
"next_continuation_token": resp.get("NextContinuationToken"),
|
||||
"is_truncated": bool(resp.get("IsTruncated")),
|
||||
}
|
||||
|
||||
|
||||
def s3_key_for_csv_import(
|
||||
tenant_id,
|
||||
company_id: int,
|
||||
|
||||
Reference in New Issue
Block a user