Unstructured Data Management
AnalyticDB for PostgreSQL establishes a unified file directory between the OSS object storage and the database by using the adbpg_filestore extension together with the ossdbutil command-line tool and the Python SDK, implementing unified unstructured data management.
Background
As enterprise data grows explosively, unstructured data such as documents, images, audio, and video accounts for an increasing share. Such data has diverse formats and complex structures, making it difficult to inventory and search uniformly. When multiple users and businesses share storage, access isolation is hard to guarantee. Meanwhile, in large-scale AI application scenarios, high-quality corpus must be continuously fed into data processing pipelines for large model training and knowledge base construction.
To address these challenges, AnalyticDB for PostgreSQL establishes a unified file directory between the OSS object storage and the database by using the adbpg_filestore extension together with the ossdbutil command-line tool and the Python SDK, implementing unified unstructured data management:
Supports automatic discovery and metadata extraction to solve the problem of inventorying and searching massive files.
Provides fine-grained permission control to ensure access security when multiple users share storage.
Seamlessly connects with ETL tasks, simplifying the data lake ingestion and processing workflow and supporting continuous AI corpus preparation.
How it works
adbpg_filestore stores files in OSS and maintains file metadata in the pg_directory_table directory table of the database, implementing unified management in which data is stored in OSS and metadata is stored in the database. The core workflow consists of three steps:
Initialize the extension and directory table: create the extension in the database and initialize the metadata table.
Upload and download files through the CLI or SDK: ossdbutil automatically completes OSS operations and metadata synchronization.
Query and manage data through SQL: query the file directory, task status, and permissions directly in the database.
Prerequisites
An AnalyticDB for PostgreSQL instance of version 7.5.0.0 or later.
An OSS Bucket is created and is in the same region as the instance.
An AccessKey ID and AccessKey Secret with OSS access permissions are prepared.
Python 3.8 or later.
Installation and configuration
Step 1: Install the extension and initialize the directory table
To install the adbpg_filestore extension, submit a ticket to contact technical support.
Step 2: Install and configure ossdbutil
Install ossdbutil:
pip3 install ossdbutil==1.0.1Generate a configuration file template:
ossdbutil initEdit ~/.ossdbutil/config.yaml and enter the OSS and database connection information:
oss:
endpoint: "https://oss-cn-beijing.aliyuncs.com"
access_key_id: "YOUR_OSS_ACCESS_KEY_ID"
access_key_secret: "YOUR_OSS_ACCESS_KEY_SECRET"
bucket_name: "your-bucket-name"
database:
host: "your-gp-host.gpdb.rds.aliyuncs.com"
port: 5432
dbname: "your_db_name"
user: "your_db_user"
password: "your_db_password"After the configuration is complete, the CLI and the Python SDK share the same configuration.
Command-line tool (CLI) usage
The CLI is suitable for daily operations and manual operations. After installation, run ossdbutil commands directly in the terminal.
Upload files
# Upload a single file to the /reports directory (public file)
ossdbutil upload ./report.pdf /reports
# Upload an entire directory and set the owner to the current database user
ossdbutil upload /data/upload /reports --owner
# Specify the tenant when uploading
ossdbutil upload ./data.csv /project/data --tenant-id t1Download files
# Download a single file by name
ossdbutil download --file-name report.pdf --output-dir ./local
# Download an entire directory
ossdbutil download --relative-path /reports --output-dir ./local
# Download only files owned by the current user
ossdbutil download --relative-path /reports --output-dir ./local --ownerDelete files
# Delete a single file (the OSS object and the database record are deleted together)
ossdbutil delete /reports/q1.pdf
# Delete only files owned by the current user
ossdbutil delete /reports/q1.pdf --ownerCopy and upload large files
# Copy an OSS object (metadata is synchronized)
ossdbutil copy reports/q1.pdf archive/q1.pdf
# Upload a large file in multiple parts
ossdbutil multipart-upload ./big.tar.gz backup/big.tar.gzSynchronize existing data
# Synchronize the metadata of existing OSS objects to the database
ossdbutil init-from-oss --prefixes "data/,logs/"Permission management
# Add an owner to a file
ossdbutil grant /reports/q1.pdf --add-owner alice
# Grant permissions for a directory in batch (the path ends with /)
ossdbutil grant /reports/ --add-owner alice--owner is a Boolean switch. When it is enabled during upload, the owner is set to the current database user (if not specified, the file is public). When it is enabled during download or deletion, only files owned by the current user are operated on.
Python SDK usage
The Python SDK is suitable for integration in application code. Its interface style is consistent with the OSS SDK.
Initialize the client
from ossdbutil import OSSDBClient
client = OSSDBClient.from_config('~/.ossdbutil/config.yaml')Upload files
# Upload a single file
with open('report.csv', 'rb') as f:
client.put_object('reports/report.csv', f)
# Upload a directory and automatically synchronize metadata
client.batch_upload(
local_dir='data/',
remote_prefix='datasets/2024/',
enable_owner=True,
tenant_id='tenant_a'
)Query and download files
Query file metadata (SQL):
SELECT * FROM adbpg_filestore.pg_directory_table
WHERE bucket = 'your-bucket-name' AND relative_path LIKE '/reports%';Download and list:
# Download a file
data = client.get_object('reports/report.csv')
with open('report_download.csv', 'wb') as f:
f.write(data)
# List files by prefix
for obj in client.list_objects(prefix='reports/'):
print(obj['file_name'], obj['size'], obj['owner'], obj['tenant_id'])Manage file permissions
# Set the owner when uploading so that the file is accessible only to the current database user
with open('data/report.csv', 'rb') as f:
client.put_object('reports/private/report.csv', f, enable_owner=True)
# Add an owner to an existing file
client.grant_ownership(
'reports/private/report.csv',
add_owners=['bob']
)Delete files
# Delete a single file
client.delete_object('reports/report.csv')
# Delete a directory in batch and delete only files owned by the current user
client.batch_delete('reports/', filter_by_owner=True)View upload task status
Upload and delete operations are recorded in the adbpg_filestore.upload_task table. You can query the task status by using SQL:
SELECT bucket, relative_path, file_name, status, error_message, updated_at
FROM adbpg_filestore.upload_task
WHERE status != 'synced'
ORDER BY updated_at DESC;Common status description:
Status | Description |
pending | The task is created and is waiting for the OSS upload or deletion to complete. |
uploaded | The OSS upload is complete and is waiting for metadata synchronization. |
synced | The metadata is synchronized and the task is complete. |
failed | The task failed. View the cause through error_message. |
After a deletion operation succeeds, the corresponding task record is automatically cleaned up.