2023-02-17 00:27:25 -05:00
|
|
|
import { S3 } from 'aws-sdk'
|
|
|
|
|
|
|
|
export default class AwsS3Api {
|
|
|
|
static async delete (configMap: IStringKeyMap): Promise<boolean> {
|
2023-02-17 02:42:49 -05:00
|
|
|
const { imgUrl, config: { accessKeyID, secretAccessKey, bucketName, region, endpoint, pathStyleAccess, bucketEndpoint, rejectUnauthorized } } = configMap
|
2023-02-17 00:27:25 -05:00
|
|
|
try {
|
2023-02-19 21:25:59 -05:00
|
|
|
const url = new URL(!/^https?:\/\//.test(imgUrl) ? `http://${imgUrl}` : imgUrl)
|
2023-02-17 00:27:25 -05:00
|
|
|
const fileKey = url.pathname
|
|
|
|
let endpointUrl
|
|
|
|
if (endpoint) {
|
2023-02-19 21:25:59 -05:00
|
|
|
if (!/^https?:\/\//.test(endpoint)) {
|
2023-02-17 02:42:49 -05:00
|
|
|
endpointUrl = `http://${endpoint}`
|
2023-02-17 00:27:25 -05:00
|
|
|
} else {
|
2023-02-17 02:42:49 -05:00
|
|
|
endpointUrl = endpoint
|
2023-02-17 00:27:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let sslEnabled = true
|
2023-02-17 02:42:49 -05:00
|
|
|
if (endpointUrl) {
|
|
|
|
sslEnabled = endpointUrl.startsWith('https')
|
|
|
|
}
|
|
|
|
const http = sslEnabled ? require('https') : require('http')
|
2023-02-17 00:27:25 -05:00
|
|
|
const client = new S3({
|
|
|
|
accessKeyId: accessKeyID,
|
|
|
|
secretAccessKey,
|
|
|
|
endpoint: endpointUrl,
|
|
|
|
s3ForcePathStyle: pathStyleAccess,
|
|
|
|
sslEnabled,
|
2023-02-17 02:42:49 -05:00
|
|
|
region,
|
|
|
|
s3BucketEndpoint: bucketEndpoint,
|
|
|
|
httpOptions: {
|
|
|
|
agent: new http.Agent({
|
2023-02-19 21:25:59 -05:00
|
|
|
rejectUnauthorized,
|
|
|
|
timeout: 30000
|
2023-02-17 02:42:49 -05:00
|
|
|
})
|
|
|
|
}
|
2023-02-17 00:27:25 -05:00
|
|
|
})
|
|
|
|
const result = await client.deleteObject({
|
|
|
|
Bucket: bucketName,
|
|
|
|
Key: fileKey.replace(/^\//, '')
|
|
|
|
}).promise()
|
|
|
|
return result.$response.httpResponse.statusCode === 204
|
|
|
|
} catch (error) {
|
2023-04-26 05:01:55 -04:00
|
|
|
console.log(error)
|
2023-02-17 00:27:25 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|