Skip to content

Commit

Permalink
feat(imgHosting): 支持传递SecurityToken
Browse files Browse the repository at this point in the history
  • Loading branch information
ZvonimirSun committed Dec 26, 2024
1 parent 9f82062 commit ad80f8b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/tools/imgHosting/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export interface S3Config {
[key: string]: string
}

export interface AliOssConfig extends S3Config {
securityToken: string
}

export interface Uploader<T = any> {
name: string
config: (options: Partial<T>) => Config[]
Expand Down
23 changes: 17 additions & 6 deletions src/tools/imgHosting/uploader/aliyun.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Config, ImageItem, S3Config, Uploader } from '../type'
import type { AliOssConfig, Config, ImageItem, Uploader } from '../type'
import { DeleteObjectsCommand, paginateListObjectsV2, PutObjectCommand, S3Client } from '@aws-sdk/client-s3'
import { v4 as uuidv4 } from 'uuid'

/**
* 获取配置
*/
function config(options: Partial<S3Config> = {}): Config[] {
function config(options: Partial<AliOssConfig> = {}): Config[] {
return [
{
name: 'accessKeyId',
Expand All @@ -23,6 +23,14 @@ function config(options: Partial<S3Config> = {}): Config[] {
label: '设定KeySecret',
hint: 'AccessKeySecret',
},
{
name: 'securityToken',
type: 'password',
default: options.securityToken || '',
required: false,
label: '设定SecurityToken',
hint: 'SecurityToken',
},
{
name: 'bucket',
type: 'input',
Expand Down Expand Up @@ -79,7 +87,7 @@ function config(options: Partial<S3Config> = {}): Config[] {
* @param file {File} 文件
* @return {Promise<object>}
*/
async function upload(options: S3Config, file: File): Promise<ImageItem> {
async function upload(options: AliOssConfig, file: File): Promise<ImageItem> {
const customUrl = options.customUrl
const path = options.path || ''
const s3Client = new S3Client({
Expand All @@ -88,6 +96,7 @@ async function upload(options: S3Config, file: File): Promise<ImageItem> {
credentials: {
accessKeyId: options.accessKeyId,
secretAccessKey: options.accessKeySecret,
sessionToken: options.securityToken,
},
})
const command = new PutObjectCommand({
Expand Down Expand Up @@ -121,7 +130,7 @@ async function upload(options: S3Config, file: File): Promise<ImageItem> {
}
}

async function list(config: S3Config) {
async function list(config: AliOssConfig) {
const customUrl = config.customUrl
const path = config.path || ''
const optionUrl = config.options || ''
Expand All @@ -132,6 +141,7 @@ async function list(config: S3Config) {
credentials: {
accessKeyId: config.accessKeyId,
secretAccessKey: config.accessKeySecret,
sessionToken: config.securityToken,
},
})
const paginator = paginateListObjectsV2(
Expand Down Expand Up @@ -162,14 +172,15 @@ async function list(config: S3Config) {
}) || []
}

async function remove(config: S3Config, imageItem: ImageItem) {
async function remove(config: AliOssConfig, imageItem: ImageItem) {
const path = config.path || ''
const s3Client = new S3Client({
region: config.area,
endpoint: `https://${config.area}.aliyuncs.com`,
credentials: {
accessKeyId: config.accessKeyId,
secretAccessKey: config.accessKeySecret,
sessionToken: config.securityToken,
},
})
const command = new DeleteObjectsCommand({
Expand All @@ -191,7 +202,7 @@ async function remove(config: S3Config, imageItem: ImageItem) {
}
}

export const aliyun: Uploader<S3Config> = {
export const aliyun: Uploader<AliOssConfig> = {
name: '阿里云OSS',
config,
upload,
Expand Down

0 comments on commit ad80f8b

Please sign in to comment.