mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-05-07 05:42:40 +08:00
fix(s3): Correctly handle file deletion with custom URL (#358)
* fix(s3): Correctly handle file deletion with custom URL When a custom URL is configured for an S3-compatible provider, the application was failing to delete files from the remote bucket. This was because the deletion logic incorrectly used the full pathname of the custom URL as the S3 object key, instead of the object's actual key within the bucket. This commit modifies the `removeFileFromS3InMain` function to be aware of the `customUrl` setting. If a custom URL is in use, the logic now correctly strips the custom URL's prefix from the image URL to derive the proper S3 object key before sending the `DeleteObject` command. * feat(debug): Add detailed logging to S3 deletion This commit adds extensive console logging to the `removeFileFromS3InMain` function in `src/main/utils/deleteFunc.ts`. The purpose of this change is to debug an issue where file deletion fails with a custom URL. These logs will capture the state of key variables (configMap, imgUrl, customUrl, fileKey) and any errors returned by the S3 client during the deletion process. This is a temporary debugging measure. * fix(s3): Correctly handle file deletion using urlPrefix This commit fixes a bug where file deletion from an S3-compatible provider would fail if a custom URL was configured. The root cause was that the logic was looking for a `customUrl` property in the config, when the correct property name is `urlPrefix`. The `removeFileFromS3InMain` function has been updated to use the correct `urlPrefix` property. This ensures that when a custom URL is present, the S3 object key is derived correctly by stripping the prefix, allowing the deletion to succeed. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
@@ -93,7 +93,16 @@ export async function removeFileFromS3InMain (configMap: IStringKeyMap, dogeMode
|
|||||||
const {
|
const {
|
||||||
url: rawUrl,
|
url: rawUrl,
|
||||||
type,
|
type,
|
||||||
config: { accessKeyID, secretAccessKey, bucketName, endpoint, pathStyleAccess, rejectUnauthorized, proxy }
|
config: {
|
||||||
|
accessKeyID,
|
||||||
|
secretAccessKey,
|
||||||
|
bucketName,
|
||||||
|
endpoint,
|
||||||
|
pathStyleAccess,
|
||||||
|
rejectUnauthorized,
|
||||||
|
proxy,
|
||||||
|
urlPrefix
|
||||||
|
}
|
||||||
} = configMap
|
} = configMap
|
||||||
let {
|
let {
|
||||||
imgUrl,
|
imgUrl,
|
||||||
@@ -102,11 +111,22 @@ export async function removeFileFromS3InMain (configMap: IStringKeyMap, dogeMode
|
|||||||
if (type === 'aws-s3' || type === 'aws-s3-plist') {
|
if (type === 'aws-s3' || type === 'aws-s3-plist') {
|
||||||
imgUrl = rawUrl || imgUrl || ''
|
imgUrl = rawUrl || imgUrl || ''
|
||||||
}
|
}
|
||||||
|
let fileKey
|
||||||
|
if (urlPrefix && imgUrl.startsWith(urlPrefix)) {
|
||||||
|
const urlPrefixObj = new URL(urlPrefix)
|
||||||
|
const imgUrlObj = new URL(imgUrl)
|
||||||
|
if (imgUrlObj.pathname.startsWith(urlPrefixObj.pathname)) {
|
||||||
|
fileKey = imgUrlObj.pathname.substring(urlPrefixObj.pathname.length).replace(/^\/+/, '')
|
||||||
|
} else {
|
||||||
|
fileKey = imgUrlObj.pathname.replace(/^\/+/, '')
|
||||||
|
}
|
||||||
|
} else {
|
||||||
const url = new URL(!/^https?:\/\//.test(imgUrl) ? `http://${imgUrl}` : imgUrl)
|
const url = new URL(!/^https?:\/\//.test(imgUrl) ? `http://${imgUrl}` : imgUrl)
|
||||||
let fileKey = url.pathname.replace(/^\/+/, '')
|
fileKey = url.pathname.replace(/^\/+/, '')
|
||||||
if (pathStyleAccess) {
|
if (pathStyleAccess) {
|
||||||
fileKey = fileKey.replace(/^[^/]+\//, '')
|
fileKey = fileKey.replace(/^[^/]+\//, '')
|
||||||
}
|
}
|
||||||
|
}
|
||||||
const endpointUrl: string | undefined = endpoint
|
const endpointUrl: string | undefined = endpoint
|
||||||
? /^https?:\/\//.test(endpoint)
|
? /^https?:\/\//.test(endpoint)
|
||||||
? endpoint
|
? endpoint
|
||||||
|
|||||||
Reference in New Issue
Block a user