Creating a file service with Foundation Models and MCP
With VK Object Storage, you can create a file service where object storage serves as the primary data storage system, while Foundation Models and MCP (Model Context Protocol) speed up application development and deployment. VK Object Storage provides an S3-compatible API, allowing you to use standard SDKs (such as Boto3 for Python) to upload, download, and manage files.
The following data is used as an example:
- Bucket name:
ai-file-service-bucket - Region:
ru-msk(Moscow) - Endpoint URL:
https://hb.ru-msk.vkcloud-storage.ru - S3 keys:
<ACCESS_KEY>and<SECRET_KEY>
-
Create an account for the VK Object Storage service, if you have not done so yet:
-
Go to the VK Cloud management console.
-
Select a project.
-
Go to the Object Storage → Аккаунты section.
-
Click the Добавить аккаунт or Добавить button.
-
Set the account name.
-
Click the Создать button.
-
In the window that opens, copy and save the access key identifier (Access Key ID) and the secret key (Secret Key).
-
-
Create a bucket
ai-file-service-bucketin the Moscow region. -
Make sure that AWS CLI is installed and configured to work with the created VK Object Storage account.
-
Specify the connection details for the bucket in the configuration:
[default]region = ru-mskoutput = jsons3 =addressing_style = path -
Make sure that you have Python 3.10+ and the
pippackage manager installed. -
Install Boto3, the official SDK for Python:
pip install boto3 -
Make sure that you have access to Foundation Models and the VK Cloud MCP server.
Use Foundation Models to generate the file service skeleton. Formulate a prompt for the model describing the task: a Python web application (Flask/FastAPI) with functions for uploading, downloading, and getting links to files stored in S3-compatible storage.
Example prompt for Foundation Models:
Create a Python web application (FastAPI) for a file service.The application should:- Accept files via HTTP POST requests and upload them to S3-compatible storage (Boto3).- Return a list of uploaded files.- Generate presigned URLs for downloading files.- Use environment variables for configuration (endpoint, access key, secret key, bucket name).
Save the generated code to the project directory, for example ai-file-service/.
Configure the application to work with VK Object Storage via Boto3. Initialize the S3 client by explicitly specifying endpoint_url and region_name to prevent the SDK from attempting to connect to the standard AWS endpoints.
Example of Boto3 client initialization:
import boto3from botocore.client import Configimport os# Credentials and parameters from environment variablesaccess_key = os.environ.get('S3_ACCESS_KEY')secret_key = os.environ.get('S3_SECRET_KEY')endpoint_url = os.environ.get('S3_ENDPOINT_URL', 'https://hb.ru-msk.vkcloud-storage.ru')region_name = os.environ.get('S3_REGION', 'ru-msk')bucket_name = os.environ.get('S3_BUCKET_NAME', 'ai-file-service-bucket')# S3 client initializations3_client = boto3.client('s3',endpoint_url=endpoint_url,aws_access_key_id=access_key,aws_secret_access_key=secret_key,region_name=region_name,config=Config(s3={'addressing_style': 'path'}, request_checksum_calculation="when_required", response_checksum_validation=None))
Example of a function for uploading a file to the bucket:
def upload_file(file_data: bytes, object_key: str) -> str:"""Uploads a file to VK Object Storage and returns the object key."""s3_client.put_object(Bucket=bucket_name,Key=object_key,Body=file_data)return object_key
Example of a function for generating a presigned URL for downloading a file:
def generate_download_url(object_key: str, expiration: int = 3600) -> str:"""Generates a presigned URL for downloading a file."""url = s3_client.generate_presigned_url('get_object',Params={'Bucket': bucket_name, 'Key': object_key},ExpiresIn=expiration)return url
Example of a function for getting a list of uploaded files:
def list_files() -> list:"""Returns a list of objects in the bucket."""response = s3_client.list_objects_v2(Bucket=bucket_name)if 'Contents' in response:return [obj['Key'] for obj in response['Contents']]return []
Configure the environment variables for the application:
export S3_ACCESS_KEY=<ACCESS_KEY>export S3_SECRET_KEY=<SECRET_KEY>export S3_ENDPOINT_URL=https://hb.ru-msk.vkcloud-storage.ruexport S3_REGION=ru-mskexport S3_BUCKET_NAME=ai-file-service-bucket
Run the application locally and verify its operation with VK Object Storage.
-
Run the application:
cd ai-file-serviceuvicorn main:app --host 0.0.0.0 --port 8000 -
Upload a test file via an HTTP request:
curl -X POST http://localhost:8000/upload \-F "file=@test-document.txt" -
Verify that the file has appeared in the bucket using AWS CLI:
aws s3 ls s3://ai-file-service-bucket --endpoint-url https://hb.ru-msk.vkcloud-storage.ru -
Request a presigned URL for downloading the file:
curl http://localhost:8000/download-url/test-document.txt -
Download the file using the received link and verify that the contents match the original.
MCP (Model Context Protocol) allows you to automate the assembly and deployment of the application. Connect the MCP server to your project to manage VK Cloud infrastructure through a single interface.
- Install the MCP client and configure the connection to the VK Cloud MCP server.
- Configure the MCP server to work with VK Object Storage: specify the endpoint URL, access keys, and the
ai-file-service-bucketbucket name in the MCP configuration. - Make sure that the MCP server has access to VK Cloud resources: it can create and manage containerized applications and read bucket configurations.
Use MCP to automate the Docker image build and the application deployment to Container Apps.
-
Create a
Dockerfilefor the application:FROM python:3.12-slimWORKDIR /appCOPY requirements.txt .RUN pip install --no-cache-dir -r requirements.txtCOPY . .CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] -
Create a
requirements.txtfile:fastapiuvicornboto3python-multipart -
Use the MCP server to build the image and deploy it to Container Apps.
-
Pass the environment variables to the containerized application:
S3_ACCESS_KEY—<ACCESS_KEY>S3_SECRET_KEY—<SECRET_KEY>S3_ENDPOINT_URL—https://hb.ru-msk.vkcloud-storage.ruS3_REGION—ru-mskS3_BUCKET_NAME—ai-file-service-bucket
-
Get the URL of the deployed application from Container Apps.
-
Upload a test file through the deployed application:
curl -X POST https://<APP_URL>/upload \-F "file=@test-document.txt" -
Verify that the file has appeared in the
ai-file-service-bucketbucket:aws s3 ls s3://ai-file-service-bucket --endpoint-url https://hb.ru-msk.vkcloud-storage.ru -
Request the list of files via the application API:
curl https://<APP_URL>/files -
Get a presigned URL and download the file to verify that the storage works correctly:
curl https://<APP_URL>/download-url/test-document.txt -
Download the file using the received link and check its contents.
The created resources are charged. If you no longer need them:
- Delete the containerized application in Container Apps.
- Delete the bucket
ai-file-service-bucket.