add auto_create_bucket defaulting to false, enabled for tests

This commit is contained in:
Matthew Farrellee 2025-08-21 19:53:26 -04:00
parent d67d679baf
commit 72036891e8
5 changed files with 47 additions and 0 deletions

View file

@ -13,6 +13,7 @@ AWS S3-based file storage provider for scalable cloud file management with metad
| `aws_access_key_id` | `str \| None` | No | | AWS access key ID (optional if using IAM roles) | | `aws_access_key_id` | `str \| None` | No | | AWS access key ID (optional if using IAM roles) |
| `aws_secret_access_key` | `str \| None` | No | | AWS secret access key (optional if using IAM roles) | | `aws_secret_access_key` | `str \| None` | No | | AWS secret access key (optional if using IAM roles) |
| `endpoint_url` | `str \| None` | No | | Custom S3 endpoint URL (for MinIO, LocalStack, etc.) | | `endpoint_url` | `str \| None` | No | | Custom S3 endpoint URL (for MinIO, LocalStack, etc.) |
| `auto_create_bucket` | `<class 'bool'>` | No | False | Automatically create the S3 bucket if it doesn't exist |
| `metadata_store` | `utils.sqlstore.sqlstore.SqliteSqlStoreConfig \| utils.sqlstore.sqlstore.PostgresSqlStoreConfig` | No | sqlite | SQL store configuration for file metadata | | `metadata_store` | `utils.sqlstore.sqlstore.SqliteSqlStoreConfig \| utils.sqlstore.sqlstore.PostgresSqlStoreConfig` | No | sqlite | SQL store configuration for file metadata |
## Sample Configuration ## Sample Configuration
@ -23,6 +24,7 @@ region: ${env.AWS_REGION:=us-east-1}
aws_access_key_id: ${env.AWS_ACCESS_KEY_ID:=} aws_access_key_id: ${env.AWS_ACCESS_KEY_ID:=}
aws_secret_access_key: ${env.AWS_SECRET_ACCESS_KEY:=} aws_secret_access_key: ${env.AWS_SECRET_ACCESS_KEY:=}
endpoint_url: ${env.S3_ENDPOINT_URL:=} endpoint_url: ${env.S3_ENDPOINT_URL:=}
auto_create_bucket: ${env.S3_AUTO_CREATE_BUCKET:=false}
metadata_store: metadata_store:
type: sqlite type: sqlite
db_path: ${env.SQLITE_STORE_DIR:=~/.llama/dummy}/s3_files_metadata.db db_path: ${env.SQLITE_STORE_DIR:=~/.llama/dummy}/s3_files_metadata.db

View file

@ -108,6 +108,41 @@ The S3 provider requires the following permissions:
} }
``` ```
### Automatic Bucket Creation
By default, the S3 provider expects the bucket to already exist. If you want the provider to automatically create the bucket when it doesn't exist, set `auto_create_bucket: true` in your configuration:
```yaml
config:
bucket_name: my-bucket
auto_create_bucket: true # Will create bucket if it doesn't exist
region: us-east-1
```
**Note**: When `auto_create_bucket` is enabled, the provider will need additional permissions:
```json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:ListBucket",
"s3:CreateBucket"
],
"Resource": [
"arn:aws:s3:::your-bucket-name",
"arn:aws:s3:::your-bucket-name/*"
]
}
]
}
```
### Bucket Policy (Optional) ### Bucket Policy (Optional)
For additional security, you can add a bucket policy: For additional security, you can add a bucket policy:

View file

@ -21,6 +21,9 @@ class S3FilesImplConfig(BaseModel):
default=None, description="AWS secret access key (optional if using IAM roles)" default=None, description="AWS secret access key (optional if using IAM roles)"
) )
endpoint_url: str | None = Field(default=None, description="Custom S3 endpoint URL (for MinIO, LocalStack, etc.)") endpoint_url: str | None = Field(default=None, description="Custom S3 endpoint URL (for MinIO, LocalStack, etc.)")
auto_create_bucket: bool = Field(
default=False, description="Automatically create the S3 bucket if it doesn't exist"
)
metadata_store: SqlStoreConfig = Field(description="SQL store configuration for file metadata") metadata_store: SqlStoreConfig = Field(description="SQL store configuration for file metadata")
@classmethod @classmethod
@ -31,6 +34,7 @@ class S3FilesImplConfig(BaseModel):
"aws_access_key_id": "${env.AWS_ACCESS_KEY_ID:=}", "aws_access_key_id": "${env.AWS_ACCESS_KEY_ID:=}",
"aws_secret_access_key": "${env.AWS_SECRET_ACCESS_KEY:=}", "aws_secret_access_key": "${env.AWS_SECRET_ACCESS_KEY:=}",
"endpoint_url": "${env.S3_ENDPOINT_URL:=}", "endpoint_url": "${env.S3_ENDPOINT_URL:=}",
"auto_create_bucket": "${env.S3_AUTO_CREATE_BUCKET:=false}",
"metadata_store": SqliteSqlStoreConfig.sample_run_config( "metadata_store": SqliteSqlStoreConfig.sample_run_config(
__distro_dir__=__distro_dir__, __distro_dir__=__distro_dir__,
db_name="s3_files_metadata.db", db_name="s3_files_metadata.db",

View file

@ -59,6 +59,11 @@ async def _create_bucket_if_not_exists(client: boto3.client, config: S3FilesImpl
except ClientError as e: except ClientError as e:
error_code = e.response["Error"]["Code"] error_code = e.response["Error"]["Code"]
if error_code == "404": if error_code == "404":
if not config.auto_create_bucket:
raise RuntimeError(
f"S3 bucket '{config.bucket_name}' does not exist. "
f"Either create the bucket manually or set 'auto_create_bucket: true' in your configuration."
) from e
try: try:
# For us-east-1, we can't specify LocationConstraint # For us-east-1, we can't specify LocationConstraint
if config.region == "us-east-1": if config.region == "us-east-1":

View file

@ -37,6 +37,7 @@ def s3_config(tmp_path):
return S3FilesImplConfig( return S3FilesImplConfig(
bucket_name="test-bucket", bucket_name="test-bucket",
region="not-a-region", region="not-a-region",
auto_create_bucket=True,
metadata_store=SqliteSqlStoreConfig(db_path=db_path.as_posix()), metadata_store=SqliteSqlStoreConfig(db_path=db_path.as_posix()),
) )