Skip to content

Commit 53a0b47

Browse files
authored
🐛 fix: make S3 upload ACL setting optional (#3464)
* feat: make setting ACL optional * docs: add `S3_SET_ACL` description
1 parent cf90dba commit 53a0b47

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

docs/self-hosting/server-database.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ S3_ENDPOINT=https://0b33a03b5c993fd2f453379dc36558e5.r2.cloudflarestorage.com
144144

145145
The name and region of the bucket. `S3_BUCKET` is mandatory for specifying the bucket's name. `S3_REGION` is optional for specifying the bucket's region, generally not required to add, but some service providers may need to configure it.
146146

147+
### `S3_SET_ACL`
148+
Whether to set the ACL to `public-read` when uploading files. This option is enabled by default. If the service provider does not support setting individual ACLs for files (i.e., all files inherit the bucket's ACL), enabling this option may result in a request error. You can disable it by setting `S3_SET_ACL` to `0`.
149+
147150
### `NEXT_PUBLIC_S3_DOMAIN`
148151

149152
The public access domain of the bucket, used to access files in the bucket. This address needs to be **internet-readable**. The reason is that when OpenAI's GPT-4o and other visual models recognize images, OpenAI will try to download the image link on their servers. Therefore, this link must be publicly accessible. If it is a private link, OpenAI will not be able to access the image and will not be able to recognize the image content properly.

docs/self-hosting/server-database.zh-CN.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ S3_ENDPOINT=https://0b33a03b5c993fd2f453379dc36558e5.r2.cloudflarestorage.com
150150

151151
存储桶的名称和区域,`S3_BUCKET` 是必须的,用于指定存储桶的名称。 `S3_REGION` 是可选的,用于指定存储桶的区域,一般来说不需要添加,但某些服务商则需要配置。
152152

153+
### `S3_SET_ACL`
154+
是否在上传文件时设置 ACL 为 `public-read`。该选项默认启用。如果服务商不支持为文件设置单独的 ACL(即所有文件继承存储桶的 ACL),启用此选项可能会导致请求错误,将 `S3_SET_ACL` 设置为 `0` 即可关闭。
155+
153156
### `NEXT_PUBLIC_S3_DOMAIN`
154157

155158
存储桶对外的访问域名,用于访问存储桶中的文件,这个地址需要**允许互联网可读**。 原因是 OpenAI 的 gpt-4o 等视觉模型识别图片时,OpenAI 会尝试在他们的服务器中下载这个图片链接,因此这个链接必须是公开可访问的,如果是私有的链接,OpenAI 将无法访问到这个图片,进而无法正常识别到图片内容。

src/config/file.ts

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const getFileConfig = () => {
1818
S3_ENDPOINT: process.env.S3_ENDPOINT,
1919
S3_REGION: process.env.S3_REGION,
2020
S3_SECRET_ACCESS_KEY: process.env.S3_SECRET_ACCESS_KEY,
21+
S3_SET_ACL: process.env.S3_SET_ACL !== '0',
2122
},
2223
server: {
2324
// S3
@@ -27,6 +28,7 @@ export const getFileConfig = () => {
2728

2829
S3_REGION: z.string().optional(),
2930
S3_SECRET_ACCESS_KEY: z.string().optional(),
31+
S3_SET_ACL: z.boolean(),
3032
},
3133
});
3234
};

src/server/modules/S3/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ export class S3 {
2626

2727
private readonly bucket: string;
2828

29+
private readonly setAcl: boolean;
30+
2931
constructor() {
3032
if (!fileEnv.S3_ACCESS_KEY_ID || !fileEnv.S3_SECRET_ACCESS_KEY || !fileEnv.S3_BUCKET)
3133
throw new Error('S3 environment variables are not set completely, please check your env');
3234

3335
this.bucket = fileEnv.S3_BUCKET;
36+
this.setAcl = fileEnv.S3_SET_ACL;
3437

3538
this.client = new S3Client({
3639
credentials: {
@@ -68,7 +71,7 @@ export class S3 {
6871

6972
public async createPreSignedUrl(key: string): Promise<string> {
7073
const command = new PutObjectCommand({
71-
ACL: 'public-read',
74+
ACL: this.setAcl ? 'public-read' : undefined,
7275
Bucket: this.bucket,
7376
Key: key,
7477
});

0 commit comments

Comments
 (0)