Tencent/WeKnora · error

failed to get file from S3: %w

Error message

failed to get file from S3: %w

What it means

GetFile wraps any error returned by the AWS S3 GetObject call with 'failed to get file from S3'. It means the object could not be read from the bucket — either the key does not exist, credentials/permissions are insufficient, or the S3 call failed at the network level. The underlying S3 error is preserved via %w for errors.Is/As inspection.

Source

Thrown at internal/application/service/file/s3.go:268

		return "", fmt.Errorf("failed to upload file to S3: %w", err)
	}

	return fmt.Sprintf("s3://%s/%s", s.bucketName, objectName), nil
}

// GetFile gets a file from S3
func (s *s3FileService) GetFile(ctx context.Context, filePath string) (io.ReadCloser, error) {
	objectName, err := s.parseS3FilePath(filePath)
	if err != nil {
		return nil, err
	}

	resp, err := s.client.GetObject(ctx, &s3.GetObjectInput{
		Bucket: aws.String(s.bucketName),
		Key:    aws.String(objectName),
	})
	if err != nil {
		return nil, fmt.Errorf("failed to get file from S3: %w", err)
	}

	return resp.Body, nil
}

// DeleteFile deletes a file
func (s *s3FileService) DeleteFile(ctx context.Context, filePath string) error {
	objectName, err := s.parseS3FilePath(filePath)
	if err != nil {
		return err
	}

	_, err = s.client.DeleteObject(ctx, &s3.DeleteObjectInput{
		Bucket: aws.String(s.bucketName),
		Key:    aws.String(objectName),
	})
	if err != nil {
		return fmt.Errorf("failed to delete file: %w", err)

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Verify the object exists (aws s3 ls s3://bucket/objectName) and that the objectName passed to GetFile matches the path returned by SaveFile/SaveBytes
  2. Check AWS credentials and IAM policy grant s3:GetObject on bucket/objectName
  3. Unwrap the error with errors.As on smithy APIError to distinguish NoSuchKey vs AccessDenied vs network errors
  4. Confirm bucketName and region/endpoint configuration are correct for the environment

Example fix

// before
body, err := fileSvc.GetFile(ctx, path)
// after
body, err := fileSvc.GetFile(ctx, path)
if err != nil {
    var apiErr smithy.APIError
    if errors.As(err, &apiErr) && apiErr.ErrorCode() == "NotFound" {
        return fmt.Errorf("file %s not found: %w", path, err)
    }
    return err
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Go: check path format before calling
if !strings.HasPrefix(path, "s3://") { return errors.New("not an s3 path") }

Type guard

func isS3Path(p string) bool { return strings.HasPrefix(p, "s3://") }

Try / catch

body, err := svc.GetFile(ctx, path)
if err != nil {
    var apiErr smithy.APIError
    if errors.As(err, &apiErr) && apiErr.ErrorCode() == "NotFound" {
        return ErrFileNotFound
    }
    return err
}

Prevention

When it happens

Trigger: Calling GetFile with an objectName/key that does not exist in the bucket (NoSuchKey), expired or misconfigured credentials, missing s3:GetObject permission on the bucket/key, or network/DNS failures reaching the S3 endpoint.

Common situations: File was deleted or never uploaded under the constructed path (pathPrefix/tenantID/... changed between versions); wrong bucketName in config; IAM policy lacks GetObject; SDK v2 endpoint misconfiguration; VPC without outbound access to S3.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/bee125325c8c236e. Report an issue: GitHub.