Feature: migrate from aws-sdk v2 to v3

This commit is contained in:
萌萌哒赫萝
2023-08-08 20:34:19 -07:00
parent 76e588b2ef
commit 13852a5c1b
10 changed files with 557 additions and 573 deletions
+6 -37
View File
@@ -1,44 +1,13 @@
import { S3 } from 'aws-sdk'
import { ipcRenderer } from 'electron'
import { getRawData } from '~/renderer/utils/common'
export default class AwsS3Api {
static async delete (configMap: IStringKeyMap): Promise<boolean> {
const { imgUrl, config: { accessKeyID, secretAccessKey, bucketName, region, endpoint, pathStyleAccess, bucketEndpoint, rejectUnauthorized } } = configMap
try {
const url = new URL(!/^https?:\/\//.test(imgUrl) ? `http://${imgUrl}` : imgUrl)
const fileKey = url.pathname
let endpointUrl
if (endpoint) {
if (!/^https?:\/\//.test(endpoint)) {
endpointUrl = `http://${endpoint}`
} else {
endpointUrl = endpoint
}
}
let sslEnabled = true
if (endpointUrl) {
sslEnabled = endpointUrl.startsWith('https')
}
const http = sslEnabled ? require('https') : require('http')
const client = new S3({
accessKeyId: accessKeyID,
secretAccessKey,
endpoint: endpointUrl,
s3ForcePathStyle: pathStyleAccess,
sslEnabled,
region,
s3BucketEndpoint: bucketEndpoint,
httpOptions: {
agent: new http.Agent({
rejectUnauthorized,
timeout: 30000
})
}
})
const result = await client.deleteObject({
Bucket: bucketName,
Key: fileKey.replace(/^\/+/, '')
}).promise()
return result.$response.httpResponse.statusCode === 204
const deleteResult = await ipcRenderer.invoke('delete-aws-s3-file',
getRawData(configMap)
)
return deleteResult
} catch (error) {
console.log(error)
return false