Use the OSS documentation in Cursor

更新时间:
复制 MD 格式

Set the OSS documentation as an AI knowledge source in Cursor. The Cursor Agent can then use the latest official documentation to automatically generate standard SDK call code and ossutil usage examples. It can also fix API call issues and provide accurate configuration recommendations.

Configure the Cursor documentation data source

You can configure Cursor's @Docs data source to easily provide this document to Cursor.

Steps:

  1. Open Cursor settings

    • In the menu bar, click Cursor > Settings.

  2. Navigate to the Docs configuration

    • In the navigation pane on the left, click Indexing & Docs.

    • The document configuration page opens.

  3. Add the OSS documentation source

    • Click Add Docs. In the Docs panel, click Add Doc.

    • Enter the URL for the official OSS documentation: https://help.aliyun.com/zh/oss/. Enter a name that is easy to recognize, such as "Aliyun OSS Docs".

      In the displayed configuration form, enter the following information:

      • NAME: Aliyun OSS Docs

      • PREFIX: https://help.aliyun.com/zh/oss/

      • ENTRYPOINT: https://help.aliyun.com/zh/oss/

      After completing the form, click Confirm.

    • Confirm that the documentation source is added to the list and that its status is Indexed. After the documentation source is added, the document list displays the index status (for example, Indexed 2556 pages). Click the View indexed pages icon (book icon) in the Actions column. In the panel that appears, you can view the list of indexed page titles to confirm that the documentation has been successfully crawled and indexed.

Use the OSS documentation for Q&A

After you complete the configuration, you can query the OSS documentation data source in Cursor.

Method:

  1. Use @Docs in the chat window

    • Open the Cursor AI chat window.

    • Enter @Aliyun OSS Docs followed by your question.

    • For example: @Aliyun OSS Docs How do I use the OSS Java SDK to create a client and configure access credentials?

  2. Obtain precise answers

    • The AI provides accurate answers based on the OSS documentation. These answers include code examples, configuration steps, and other details.

      Maven dependency configuration:

      <dependency>
          <groupId>com.aliyun.oss</groupId>
          <artifactId>aliyun-sdk-oss</artifactId>
          <version>3.17.4</version>
      </dependency>

      Complete Java code example for creating an OSSClient with environment variable credentials (recommended):

      import com.aliyun.oss.ClientBuilderConfiguration;
      import com.aliyun.oss.OSS;
      import com.aliyun.oss.OSSClientBuilder;
      import com.aliyun.oss.common.auth.CredentialsProviderFactory;
      import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
      import com.aliyun.oss.common.comm.SignVersion;
      
      public class OSSClientExample {
          public static void main(String[] args) throws Exception {
              // Obtain credentials from environment variables
              EnvironmentVariableCredentialsProvider credentialsProvider =
                  CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
      
              // Configure the client
              ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
              // Use V4 signature algorithm
              clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
      
              // Create an OSSClient instance
              OSS ossClient = OSSClientBuilder.create()
                      .endpoint("your-endpoint") // For example: https://oss-cn-hangzhou.aliyuncs.com
                      .credentialsProvider(credentialsProvider)
                      .clientConfiguration(clientBuilderConfiguration)
                      .region("your-region") // For example: cn-hangzhou
                      .build();
      
              // Use ossClient to perform operations...
      
              // Shut down the OSSClient instance when it is no longer needed
              ossClient.shutdown();
          }
      }

      Set the following environment variables: OSS_ACCESS_KEY_ID (your AccessKey ID) and OSS_ACCESS_KEY_SECRET (your AccessKey secret).

  3. Optimize query results

    • Be specific: Avoid broad questions. Ask about a specific feature or scenario.

    • Include keywords: Include OSS-related keywords in your question, such as "bucket", "object", and "permission".

    • Ask in steps: For complex operations, ask questions in multiple steps.

      # Good query examples
      @Docs How do I use the Java SDK to upload a file to OSS?
      @Docs How do I configure cross-origin resource sharing (CORS) for OSS?
      @Docs How do I set permissions using a Resource Access Management (RAM) policy in OSS?
      @Docs I need to upload a file to OSS in my Node.js project. Provide a complete code example.
      @Docs Provide best practices for multipart uploads of large files to OSS.
      @Docs Provide detailed troubleshooting steps for OSS CDN cache configuration issues.
      
      # Queries to avoid
      @Docs How do I use OSS? (Too broad)
      @Docs Upload file (Lacks context)

Use the OSS documentation to resolve errors

  1. If you encounter an error when you use the Alibaba Cloud OSS SDK to upload or download files, or perform other operations, you can use the documentation data source to automatically fix the error.

    import alibabacloud_oss_v2 as oss
    
    def main():
        # Load credentials from environment variables for authentication
        credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
    
        # Load the default SDK configuration and set the credentials provider
        cfg = oss.config.load_default()
        cfg.credentials_provider = credentials_provider
    
        # Set the region
        cfg.region = "cn-hangzhou"
    
        # Create an OSS client with the configured settings
        client = oss.Client(cfg)
    
        # Upload an object directly from a local file
        # Specify the bucket name, object name, and local file path
        with open('your-test-file.md', 'r') as f:
            result = client.put_object(
                oss.PutObjectRequest(
                    bucket="xxx",  # Bucket name
                    key="xxx",        # Object name
                    body=f.read()     # Read file content
                )
            )
    
        # Print the result: status code, request ID, content MD5, ETag, CRC64 checksum, version ID, server response time
        print(f'status code: {result.status_code},'
              f' request id: {result.request_id},'
              f' content md5: {result.content_md5},'
              f' etag: {result.etag},'
              f' hash crc64: {result.hash_crc64},'
              f' version id: {result.version_id},'
              f' server time: {result.headers.get("x-oss-server-time")},'
        )
    
    if __name__ == "__main__":
        main()
    File "/Users/xxx/xxx/py_proj/myenv/lib/python3.12/site-packages/alibabacloud_oss_v2/client.py", line 275, in put_ob...
        return operations.put_object(self._client, request, **kwargs)
    File "/Users/xxx/xxx/py_proj/myenv/lib/python3.12/site-packages/alibabacloud_oss_v2/operations/object_basic.py", line 44, in put_object
        op_output = client.invoke_operation(op_input, **kwargs)
    File "/Users/xxx/xxx/py_proj/myenv/lib/python3.12/site-packages/alibabacloud_oss_v2/_client.py", line 327, in invoke_operation
        raise exceptions.OperationError(
    alibabacloud_oss_v2.exceptions.OperationError: operation error PutObject: crc is inconsistent, client 12522791075485846984, server 16472007160000755729.
  2. Open the Cursor Agent chat window. Enter `@Aliyun OSS Docs` followed by a description of your specific problem. The AI uses the latest official OSS documentation to provide a precise troubleshooting procedure and solution, which helps you optimize your code and resolve the error.

    In the IDE's built-in AI assistant dialog, enter @Aliyun OSS Docs Please help me troubleshoot the error. The chat context automatically associates the current code file put_object_with_file.py (Lines 173-174) and the knowledge base Aliyun OSS Docs. The AI assistant analyzes the error based on the documentation and code context and provides a fix suggestion.

    For example, when a CRC error alibabacloud_oss_v2.exceptions.OperationError: operation error PutObject: crc is inconsistent occurs during file upload, the AI assistant automatically analyzes the error type (CRC64 data integrity check failure, where the client and server CRC64 values are inconsistent), the affected scope (PutObject, AppendObject, UploadPart), and identifies the root cause as reading the file in text mode 'r'. The solution is to change open('your-test-file.md', 'r') to open('your-test-file.md', 'rb') to read the file in binary mode. A one-click fix button is also provided.

    The AI assistant provides two solutions for the OSS SDK file upload error:

    1. Temporary solution: Create put_object_with_file_disable_crc.py to disable CRC64 verification (⚠️ Loses data integrity protection, ⚠️ Only for trusted environments, ⚠️ Not recommended for production)

    CRC64 verification mechanism: The SDK enables CRC64 verification by default. The client and server each compute the checksum and compare them. Opening a file in text mode may cause line ending conversion, leading to checksum inconsistencies. Always use binary mode to read files.

Use the OSS documentation to generate ossutil commands

You can use Cursor's AI capabilities to describe an operation in natural language. Using the official OSS documentation as a data source, Cursor automatically generates and runs command-line operations for tasks such as uploads, downloads, and canonical name (CNAME) configurations. The AI references the documentation and your current file directory to generate commands that you can run directly.

Method:

  1. Use @Docs in the chat window

    • Open the Cursor AI chat window. Enter @Aliyun OSS Docs followed by a description of the operation.

    • For example: @Aliyun OSS Docs Use ossutil to upload the cname-configuration.xml file to the bucket named examplebucket

  • Generate and run the command based on the documentation

    • The AI analyzes whether the cname-configuration.xml file exists in the current directory.

      Let me first check the files in the current directory:
      
      Listed current directory
      
      I can see the cname-configuration.xml file. Let me view the file content to understand its structure:
      
      Read cname-configuration.xml
    • Then, based on the key elements in your description, such as "upload", the filename, and the bucket name, the AI generates the corresponding ossutil command from the official OSS documentation. If you have configured your access credentials, Cursor can also run the command for you.

      For example, run the following ossutil command to upload the cname-configuration.xml file to the specified bucket:

      ossutil cp cname-configuration.xml oss://xxx/
      
      axxxxi@JGWL9Q2JP7 ossutil-2.1.1-mac-arm64 % ossutil cp cname-configuration.xml oss://xxx/
      Success: Total 1 file, size 3882 B, Upload done:(1 objects, 3882 B), avg 14.731 KiB/s
      
      0.260099(s) elapsed
      axxxxi@JGWL9Q2JP7 ossutil-2.1.1-mac-arm64 %