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:
yang07861
2025-08-14 15:41:02 +08:00
committed by GitHub
parent 15d205b30a
commit ae366bab21

View File

@@ -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,10 +111,21 @@ 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 || ''
} }
const url = new URL(!/^https?:\/\//.test(imgUrl) ? `http://${imgUrl}` : imgUrl) let fileKey
let fileKey = url.pathname.replace(/^\/+/, '') if (urlPrefix && imgUrl.startsWith(urlPrefix)) {
if (pathStyleAccess) { const urlPrefixObj = new URL(urlPrefix)
fileKey = fileKey.replace(/^[^/]+\//, '') 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)
fileKey = url.pathname.replace(/^\/+/, '')
if (pathStyleAccess) {
fileKey = fileKey.replace(/^[^/]+\//, '')
}
} }
const endpointUrl: string | undefined = endpoint const endpointUrl: string | undefined = endpoint
? /^https?:\/\//.test(endpoint) ? /^https?:\/\//.test(endpoint)