Skip to content

Commit e2b6662

Browse files
Feat: extend S3 storage compatibility and add knowledge base ID prefix (#6355)
### What problem does this PR solve? - Added support for S3-compatible protocols. - Enabled the use of knowledge base ID as a file prefix when storing files in S3. - Updated docker/README.md to include detailed S3 and OSS configuration instructions. ### Type of change - [x] New Feature (non-breaking change which adds functionality)
1 parent 46b5e32 commit e2b6662

File tree

4 files changed

+81
-12
lines changed

4 files changed

+81
-12
lines changed

docker/README.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ The [.env](./.env) file contains important environment variables for Docker.
136136
- `password`: The password for MinIO.
137137
- `host`: The MinIO serving IP *and* port inside the Docker container. Defaults to `minio:9000`.
138138

139+
- `oss`
140+
- `access_key`: The access key ID used to authenticate requests to the OSS service.
141+
- `secret_key`: The secret access key used to authenticate requests to the OSS service.
142+
- `endpoint_url`: The URL of the OSS service endpoint.
143+
- `region`: The OSS region where the bucket is located.
144+
- `bucket`: The name of the OSS bucket where files will be stored. When you want to store all files in a specified bucket, you need this configuration item.
145+
- `prefix_path`: Optional. A prefix path to prepend to file names in the OSS bucket, which can help organize files within the bucket.
146+
147+
- `s3`:
148+
- `access_key`: The access key ID used to authenticate requests to the S3 service.
149+
- `secret_key`: The secret access key used to authenticate requests to the S3 service.
150+
- `endpoint_url`: The URL of the S3-compatible service endpoint. This is necessary when using an S3-compatible protocol instead of the default AWS S3 endpoint.
151+
- `bucket`: The name of the S3 bucket where files will be stored. When you want to store all files in a specified bucket, you need this configuration item.
152+
- `region`: The AWS region where the S3 bucket is located. This is important for directing requests to the correct data center.
153+
- `signature_version`: Optional. The version of the signature to use for authenticating requests. Common versions include `v4`.
154+
- `addressing_style`: Optional. The style of addressing to use for the S3 endpoint. This can be `path` or `virtual`.
155+
- `prefix_path`: Optional. A prefix path to prepend to file names in the S3 bucket, which can help organize files within the bucket.
156+
139157
- `oauth`
140158
The OAuth configuration for signing up or signing in to RAGFlow using a third-party account. It is disabled by default. To enable this feature, uncomment the corresponding lines in **service_conf.yaml.template**.
141159
- `github`: The GitHub authentication settings for your application. Visit the [Github Developer Settings page](https://github.com/settings/developers) to obtain your client_id and secret_key.
@@ -152,4 +170,4 @@ The [.env](./.env) file contains important environment variables for Docker.
152170
- `api_key`: The API key for the specified LLM. You will need to apply for your model API key online.
153171

154172
> [!TIP]
155-
> If you do not set the default LLM here, configure the default LLM on the **Settings** page in the RAGFlow UI.
173+
> If you do not set the default LLM here, configure the default LLM on the **Settings** page in the RAGFlow UI.

docker/docker-compose.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
include:
22
- ./docker-compose-base.yml
33

4+
# To ensure that the container processes the locally modified `service_conf.yaml.template` instead of the one included in its image, you need to mount the local `service_conf.yaml.template` to the container.
45
services:
56
ragflow:
67
depends_on:
@@ -20,6 +21,7 @@ services:
2021
- ./nginx/proxy.conf:/etc/nginx/proxy.conf
2122
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
2223
- ../history_data_agent:/ragflow/history_data_agent
24+
- ./service_conf.yaml.template:/ragflow/conf/service_conf.yaml.template
2325

2426
env_file: .env
2527
environment:

docker/service_conf.yaml.template

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ redis:
3737
# access_key: 'access_key'
3838
# secret_key: 'secret_key'
3939
# region: 'region'
40+
# endpoint_url: 'endpoint_url'
41+
# bucket: 'bucket'
42+
# prefix_path: 'prefix_path'
43+
# signature_version: 'v4'
44+
# addressing_style: 'path'
4045
# oss:
4146
# access_key: '${ACCESS_KEY}'
4247
# secret_key: '${SECRET_KEY}'

rag/utils/s3_conn.py

+55-11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import logging
1818
import boto3
1919
from botocore.exceptions import ClientError
20+
from botocore.config import Config
2021
import time
2122
from io import BytesIO
2223
from rag.utils import singleton
@@ -30,8 +31,34 @@ def __init__(self):
3031
self.access_key = self.s3_config.get('access_key', None)
3132
self.secret_key = self.s3_config.get('secret_key', None)
3233
self.region = self.s3_config.get('region', None)
34+
self.endpoint_url = self.s3_config.get('endpoint_url', None)
35+
self.signature_version = self.s3_config.get('signature_version', None)
36+
self.addressing_style = self.s3_config.get('addressing_style', None)
37+
self.bucket = self.s3_config.get('bucket', None)
38+
self.prefix_path = self.s3_config.get('prefix_path', None)
3339
self.__open__()
3440

41+
@staticmethod
42+
def use_default_bucket(method):
43+
def wrapper(self, bucket, *args, **kwargs):
44+
# If there is a default bucket, use the default bucket
45+
actual_bucket = self.bucket if self.bucket else bucket
46+
return method(self, actual_bucket, *args, **kwargs)
47+
return wrapper
48+
49+
@staticmethod
50+
def use_prefix_path(method):
51+
def wrapper(self, bucket, fnm, *args, **kwargs):
52+
# If the prefix path is set, use the prefix path.
53+
# The bucket passed from the upstream call is
54+
# used as the file prefix. This is especially useful when you're using the default bucket
55+
if self.prefix_path:
56+
fnm = f"{self.prefix_path}/{bucket}/{fnm}"
57+
else:
58+
fnm = f"{bucket}/{fnm}"
59+
return method(self, bucket, fnm, *args, **kwargs)
60+
return wrapper
61+
3562
def __open__(self):
3663
try:
3764
if self.conn:
@@ -40,19 +67,27 @@ def __open__(self):
4067
pass
4168

4269
try:
43-
self.conn = boto3.client(
44-
's3',
45-
region_name=self.region,
46-
aws_access_key_id=self.access_key,
47-
aws_secret_access_key=self.secret_key
48-
)
70+
s3_params = {
71+
'aws_access_key_id': self.access_key,
72+
'aws_secret_access_key': self.secret_key,
73+
}
74+
if self.region in self.s3_config:
75+
s3_params['region_name'] = self.region
76+
if 'endpoint_url' in self.s3_config:
77+
s3_params['endpoint_url'] = self.endpoint_url
78+
if 'signature_version' in self.s3_config:
79+
s3_params['config'] = Config(s3={"signature_version": self.signature_version})
80+
if 'addressing_style' in self.s3_config:
81+
s3_params['config'] = Config(s3={"addressing_style": self.addressing_style})
82+
self.conn = boto3.client('s3', **s3_params)
4983
except Exception:
50-
logging.exception(f"Fail to connect at region {self.region}")
84+
logging.exception(f"Fail to connect at region {self.region} or endpoint {self.endpoint_url}")
5185

5286
def __close__(self):
5387
del self.conn
5488
self.conn = None
5589

90+
@use_default_bucket
5691
def bucket_exists(self, bucket):
5792
try:
5893
logging.debug(f"head_bucket bucketname {bucket}")
@@ -64,8 +99,9 @@ def bucket_exists(self, bucket):
6499
return exists
65100

66101
def health(self):
67-
bucket, fnm, binary = "txtxtxtxt1", "txtxtxtxt1", b"_t@@@1"
68-
102+
bucket = self.bucket
103+
fnm = "txtxtxtxt1"
104+
fnm, binary = f"{self.prefix_path}/{fnm}" if self.prefix_path else fnm, b"_t@@@1"
69105
if not self.bucket_exists(bucket):
70106
self.conn.create_bucket(Bucket=bucket)
71107
logging.debug(f"create bucket {bucket} ********")
@@ -79,6 +115,8 @@ def get_properties(self, bucket, key):
79115
def list(self, bucket, dir, recursive=True):
80116
return []
81117

118+
@use_prefix_path
119+
@use_default_bucket
82120
def put(self, bucket, fnm, binary):
83121
logging.debug(f"bucket name {bucket}; filename :{fnm}:")
84122
for _ in range(1):
@@ -94,12 +132,16 @@ def put(self, bucket, fnm, binary):
94132
self.__open__()
95133
time.sleep(1)
96134

135+
@use_prefix_path
136+
@use_default_bucket
97137
def rm(self, bucket, fnm):
98138
try:
99139
self.conn.delete_object(Bucket=bucket, Key=fnm)
100140
except Exception:
101141
logging.exception(f"Fail rm {bucket}/{fnm}")
102142

143+
@use_prefix_path
144+
@use_default_bucket
103145
def get(self, bucket, fnm):
104146
for _ in range(1):
105147
try:
@@ -112,18 +154,20 @@ def get(self, bucket, fnm):
112154
time.sleep(1)
113155
return
114156

157+
@use_prefix_path
158+
@use_default_bucket
115159
def obj_exist(self, bucket, fnm):
116160
try:
117-
118161
if self.conn.head_object(Bucket=bucket, Key=fnm):
119162
return True
120163
except ClientError as e:
121164
if e.response['Error']['Code'] == '404':
122-
123165
return False
124166
else:
125167
raise
126168

169+
@use_prefix_path
170+
@use_default_bucket
127171
def get_presigned_url(self, bucket, fnm, expires):
128172
for _ in range(10):
129173
try:

0 commit comments

Comments
 (0)