/** * Strip a bucket-name prefix from an OSS endpoint URL. * * Users sometimes paste the bucket-specific URL * (e.g. `https://my-bucket.oss-cn-hangzhou.aliyuncs.com`) instead of the * bare regional endpoint. The upstream SDK prepends the bucket name itself * when constructing request URLs, so leaving the prefix in place produces a * doubled hostname like `https://my-bucket.my-bucket.oss-cn-hangzhou.…`. * * Returns the cleaned endpoint, or the original string when no stripping * is needed (including malformed URLs — those are left for schema * validation to reject). */ export function stripBucketPrefix( ossEndpoint: string, ossBucketName: string, ): string { try { const url = new URL(ossEndpoint); if (url.hostname.startsWith(`${ossBucketName}.`)) { url.hostname = url.hostname.slice(ossBucketName.length + 1); return url.toString().replace(/\/$/, ""); } } catch { // invalid URL — return as-is } return ossEndpoint; }