{"record":{"id":"856d64907d53f3cb","repo":"Tencent/WeKnora","slug":"failed-to-copy-file-in-oss-w","errorCode":null,"errorMessage":"failed to copy file in OSS: %w","messagePattern":"failed to copy file in OSS: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/application/service/file/oss.go","lineNumber":274,"sourceCode":"\tsrcBucket, srcKey, err := parseOssFilePath(srcPath)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"oss copy rejected source %q: %w\", srcPath, ErrCrossBackendCopy)\n\t}\n\tif err := utils.SafeObjectKey(srcKey); err != nil {\n\t\treturn \"\", fmt.Errorf(\"invalid source path: %w\", err)\n\t}\n\n\text := filepath.Ext(srcPath)\n\tdestKey := fmt.Sprintf(\"%s%d/%s/%s%s\", s.pathPrefix, tenantID, knowledgeID, uuid.New().String(), ext)\n\n\t_, err = s.client.CopyObject(ctx, &oss.CopyObjectRequest{\n\t\tBucket:       oss.Ptr(s.bucketName),\n\t\tKey:          oss.Ptr(destKey),\n\t\tSourceBucket: oss.Ptr(srcBucket),\n\t\tSourceKey:    oss.Ptr(srcKey),\n\t})\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to copy file in OSS: %w\", err)\n\t}\n\n\tnewPath := fmt.Sprintf(\"oss://%s/%s\", s.bucketName, destKey)\n\tlogger.Infof(ctx, \"Copied OSS object %s to %s\", srcPath, newPath)\n\treturn newPath, nil\n}\n\n// GetFile retrieves a file from OSS by its path.\nfunc (s *ossFileService) GetFile(ctx context.Context, filePath string) (io.ReadCloser, error) {\n\tbucketName, objectName, err := parseOssFilePath(filePath)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tif err := utils.SafeObjectKey(objectName); err != nil {\n\t\treturn nil, fmt.Errorf(\"invalid file path: %w\", err)\n\t}\n\n\tvar client *oss.Client","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/application/service/file/oss.go#L256-L292","documentation":"This error is returned by ossFileService.CopyFile when the client.CopyObject (server-side copy) call to Alibaba Cloud OSS fails. The parse and key-validation steps already succeeded, so this indicates an OSS-level problem: permissions, object absence, size limits, or network. The SDK error is wrapped and preserved for inspection.","triggerScenarios":"Calling CopyFile with a valid oss:// source where the CopyObject API fails: source object does not exist (NoSuchKey), missing oss:GetObject on source or oss:PutObject on destination, source object larger than the 5 GB CopyObject limit, cross-region/cross-storage-class copy restrictions, or network errors.","commonSituations":"Copying a knowledge file whose row was deleted from OSS but still referenced in the DB; RAM policies granting destination write but not source read; very large attachments exceeding server-side copy limits (require multipart copy); buckets in different regions.","solutions":["Unwrap the error and check for NoSuchKey — the source object may be gone; verify it exists with a HeadObject first.","Verify RAM permissions: oss:GetObject on the source bucket/key and oss:PutObject on the destination.","For sources >5 GB, use multipart (UploadPartCopy) instead of CopyObject.","Confirm source and destination buckets are compatible (region/storage class) for direct copy.","Retry on transient 5xx/network errors with backoff."],"exampleFix":"// before\n_, err = s.client.CopyObject(ctx, &oss.CopyObjectRequest{...})\nif err != nil {\n    return \"\", fmt.Errorf(\"failed to copy file in OSS: %w\", err)\n}\n// after\nif err != nil {\n    var respErr *oss.ServiceError\n    if errors.As(err, &respErr) && respErr.Code == \"NoSuchKey\" {\n        return \"\", fmt.Errorf(\"source object missing: %w\", err)\n    }\n    return \"\", fmt.Errorf(\"failed to copy file in OSS: %w\", err)\n}","handlingStrategy":"retry","validationCode":"// pre-flight before CopyFile\nsrcBucket, srcKey, err := parseOssFilePath(srcPath)\nif err != nil { return ErrCrossBackendCopy }\nif err := utils.SafeObjectKey(srcKey); err != nil { return err }\n_, err = client.HeadObject(ctx, &oss.HeadObjectRequest{Bucket: oss.Ptr(srcBucket), Key: oss.Ptr(srcKey)})\n// err != nil here means the source object is missing -> skip copy","typeGuard":null,"tryCatchPattern":"newPath, err := svc.CopyFile(ctx, srcPath, tenantID, knowledgeID)\nvar svcErr *oss.ServiceError\nif errors.As(err, &svcErr) {\n    switch svcErr.Code {\n    case \"NoSuchKey\": // source gone -> treat as not found\n    case \"AccessDenied\": // fix IAM on source/destination\n    default: if isTransient(svcErr.Code) { /* retry */ }\n    }\n}","preventionTips":["HeadObject the source before copying to catch missing objects early.","Grant oss:GetObject on source and oss:PutObject on destination in RAM policy.","Use multipart copy (UploadPartCopy) for objects approaching 5 GB.","Keep source and destination in compatible regions/storage classes.","Retry transient 5xx/network errors with backoff."],"tags":["oss","copy","copyobject","alibaba-cloud","permissions"],"backgroundTag":"oss-copy-failed","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}