Commit a842861c by Haohao Jiang

fix: rename upload method name

parent 8781869c
BOBCAT_HOST=xxxxxxxxx
BOBCAT_USER_COOKIE=xxxxxxxxx
DESIGN_TEAM_CHANNEL_ID=xxxxxxxxx
GOOGLE_SERVICE_ACCOUNT_KEY_FILE=xxxxxxxxx
GOOGLE_SHEET_AI_ID=xxxxxxxxx
GOOGLE_SHEET_ID=xxxxxxxxx
GSHEET_AI_DELAY_MS=xxxxxxxxx
SLACK_WEBHOOK=xxxxxxxxx
# AWS S3 Configuration
AWS_REGION=ap-northeast-1
AWS_ACCESS_KEY_ID=your_access_key_id
AWS_SECRET_ACCESS_KEY=your_secret_key
AWS_S3_BUCKET_NAME=your_bucket_name
\ No newline at end of file
...@@ -31,9 +31,9 @@ AWS_S3_BUCKET_NAME=your_bucket_name ...@@ -31,9 +31,9 @@ AWS_S3_BUCKET_NAME=your_bucket_name
### 使用示例 ### 使用示例
```typescript ```typescript
import { uploadFileFromEnv } from './src/clients/s3'; import { uploadToS3 } from './src/clients/s3';
const key = await uploadFileFromEnv( const key = await uploadToS3(
'/path/to/local/file.pdf', '/path/to/local/file.pdf',
'uploads/file.pdf', 'uploads/file.pdf',
{ contentType: 'application/pdf', acl: 'private' } { contentType: 'application/pdf', acl: 'private' }
......
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
* *
* 使用示例: * 使用示例:
* ```typescript * ```typescript
* import { uploadFileFromEnv } from './clients/s3'; * import { uploadToS3 } from './clients/s3';
* *
* // 上传文件(自动读取环境变量) * // 上传文件(自动读取环境变量)
* const key = await uploadFileFromEnv( * const key = await uploadToS3(
* '/path/to/file.pdf', * '/path/to/file.pdf',
* 'uploads/file.pdf', * 'uploads/file.pdf',
* { contentType: 'application/pdf', acl: 'private' } * { contentType: 'application/pdf', acl: 'private' }
...@@ -15,8 +15,7 @@ ...@@ -15,8 +15,7 @@
*/ */
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3'; import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import { createReadStream, statSync } from 'fs'; import { readFileSync } from 'fs';
import { basename } from 'path';
/** /**
* S3 Configuration * S3 Configuration
...@@ -64,14 +63,12 @@ export async function uploadFile( ...@@ -64,14 +63,12 @@ export async function uploadFile(
options?: UploadOptions options?: UploadOptions
): Promise<string> { ): Promise<string> {
const client = createS3Client(config); const client = createS3Client(config);
const fileStream = createReadStream(filePath); const fileContent = readFileSync(filePath);
const fileStat = statSync(filePath);
const uploadParams: any = { const uploadParams: any = {
Bucket: config.bucketName, Bucket: config.bucketName,
Key: key, Key: key,
Body: fileStream, Body: fileContent,
ContentLength: fileStat.size,
}; };
if (options?.contentType) { if (options?.contentType) {
...@@ -99,7 +96,7 @@ export async function uploadFile( ...@@ -99,7 +96,7 @@ export async function uploadFile(
* @param options Upload options * @param options Upload options
* @returns The S3 object key * @returns The S3 object key
*/ */
export async function uploadFileFromEnv( export async function uploadToS3(
filePath: string, filePath: string,
key: string, key: string,
options?: UploadOptions options?: UploadOptions
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment