All Products
Search
Document Center

Object Storage Service:Obtain request IDs

Last Updated:Sep 27, 2023

Object Storage Service (OSS) assigns a unique ID for each request received by OSS. You can use the ID of a request to query the information about the request in various logs. When you contact technical support to help troubleshoot errors that occurred in OSS, you must provide the IDs of the failed requests. Technical support can use these request IDs to locate and resolve your problems. This topic describes how to obtain the ID of a request.

Obtain request IDs by using OSS SDKs

The following sample code provides examples on how to obtain request IDs by using OSS SDKs for common programming languages.

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import java.io.File;

public class Demo {

    public static void main(String[] args) throws Exception {
        // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the name of the bucket. Example: examplebucket. 
        String bucketName = "examplebucket";
        // Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
        String objectName = "exampledir/exampleobject.txt";
        // Specify the full path of the local file that you want to upload. Example: D:\\localpath\\examplefile.txt. 
        // By default, if the path of the local file is not specified, the local file is uploaded from the path of the project to which the sample program belongs. 
        String filePath= "D:\\localpath\\examplefile.txt";

        // Create an OSSClient instance. 
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            // Create a PutObjectRequest object. 
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new File(filePath));
            // The following sample code provides an example on how to specify the storage class and ACL of an object when you upload the object: 
            // ObjectMetadata metadata = new ObjectMetadata();
            // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
            // metadata.setObjectAcl(CannedAccessControlList.Private);
            // putObjectRequest.setMetadata(metadata);
            
            // Upload the local file. 
            PutObjectResult result = ossClient.putObject(putObjectRequest);           
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\OssClient;
use OSS\Core\OssException;
use OSS\Model\StyleConfig;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
$endpoint = "yourEndpoint";
// Specify the name of the bucket. 
$bucket= "examplebucket";
try{
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    $result = $ossClient->getObject($bucket, "demo.txt");
} catch(OssException $e) {
    printf($e->getMessage() . "\n");
    printf($e->getRequestId() . "\n");
}
const OSS = require('ali-oss')
constpath=require("path")

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the name of the bucket. 
  bucket: 'examplebucket',
});

const headers = {
  // Specify the storage class of the object. 
  'x-oss-storage-class': 'Standard',
  // Specify the ACL of the object. 
  'x-oss-object-acl': 'private',
  // When you access an object by using the URL of the object, specify that the object is downloaded as an attachment. The name of the downloaded object is example.jpg. 
  // 'Content-Disposition': 'attachment; filename="example.jpg"'
  // Specify tags for the object. You can specify multiple tags at a time. 
  'x-oss-tagging': 'Tag1=1&Tag2=2',
  // Specify whether the PutObject operation overwrites an object that has the same name. In this example, the x-oss-forbid-overwrite parameter is set to true, which specifies that an existing object that has the same name cannot be overwritten by the uploaded object. 
  'x-oss-forbid-overwrite': 'true',
};

async function put () {
  try {
    // Specify the full paths of the object and the local file. Do not include the bucket name in the full path of the object. 
    // If you do not specify the path of the local file, the local file is uploaded from the path of the project to which the sample program belongs. 
    const result = await client.put('exampleobject.txt', path.normalize('D:\\localpath\\examplefile.txt')
    // Specify headers.
    //,{headers}
    );
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

put();
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

try:
    // Specify the name of the downloaded object. Example: exampleobject.txt. 
    stream = bucket.get_object('exampleobject.txt')
except oss2.exceptions.NoSuchKey as e:
    print('status={0}, request_id={1}'.format(e.status, e.request_id))            
package main

import (
	"fmt"
	"net/http"
	"os"
	"strings"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

// The function for error handling. 
func HandleError(err error) {
	fmt.Println("Error:", err)
	os.Exit(-1)
}

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the actual endpoint. 
	// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
	client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		HandleError(err)
	}
	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	// Specify the name of the object. Example: exampleobject.txt. 
	objectName := "exampleobject.txt"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		HandleError(err)
	}
	var responseHeader http.Header
	// Upload a string. 
	err = bucket.PutObject(objectName, strings.NewReader("Hello OSS"), oss.GetResponseHeader(&responseHeader))
	if err != nil {
		if _, ok := err.(oss.ServiceError); ok {
			// err is of the oss.ServiceError type. 
			fmt.Println("Error Message:", err.(oss.ServiceError).Message)
			fmt.Println("Error Code:", err.(oss.ServiceError).Code)
			fmt.Println("Request ID:", err.(oss.ServiceError).RequestID)
			fmt.Println("Host ID:", err.(oss.ServiceError).HostID)
		} else {
			// err is not of the oss.ServiceError type. 
			fmt.Println("Error:", err)
			os.Exit(-1)
		}
	}

	requestId := oss.GetRequestId(responseHeader)
	fmt.Println("Request ID:", requestId)
}

Obtain request IDs by using the browser developer tools if you directly access objects from a browser

The following procedure shows how to obtain request IDs if you directly access OSS objects from Google Chrome on Windows:

  1. Open your browser and press F12.

  2. On the developer tools page, click Network.

  3. Click the Name tab and select the desired object.

  4. Click the Headers tab. In the Response Headers section, obtain the ID of the request from x-oss-request-id.

浏览器获取

Obtain request IDs from real-time logs

To obtain request IDs, you can query real-time logs that record the operations performed on buckets and objects in the OSS console.

  1. Log on to the OSS console.

  2. In the left-side navigation pane, click Buckets. On the Buckets page, click the name of the bucket.

  3. Choose Logging > Real-time Log Query.

  4. Press Ctrl+F and enter request_id to search for the ID of the request.实时日志

Obtain request IDs by using ossutil

If you run a command in ossutil, you can obtain the request ID from the returned log.

工具获取

Obtain request IDs by using the CLI on Linux

The following procedure shows how to obtain request IDs by using the CLI on Linux:

  1. Obtain the object URL.

    1. Log on to the OSS console.

    2. In the left-side navigation pane, click Buckets. On the Buckets page, find and click the desired bucket.

    3. In the left-side navigation tree, choose Object Management > Objects.

    4. Find the desired object and click View Details in the Actions column. Then, click Copy Object URL.

  2. Run the following command to obtain the request ID in the HTTP response header:

    curl -voa "[$URL]"

    Set [$URL] to the URL that you copied.

    The following figure shows the returned result.requestid

Obtain request IDs by using the developer tools when you upload objects in the OSS console

The following procedure shows how to use the developer tools of Google Chrome to obtain the ID of a request that you sent to upload an object:

  1. Open the developer tools of your browser.

    1. Open your browser and press F12.

    2. On the developer tools page, click Network.

  2. Upload an object.

    1. Log on to the OSS console. In the left-side navigation pane, click Buckets. On the Buckets page, click the name of the bucket to which you want to upload an object.

    2. In the left-side navigation tree, choose Object Management > Objects.

    3. Upload the object. For more information, see Upload objects.

  3. Use browser developer tools to obtain the ID of the upload request.

    1. On the developer tools page of your browser, click the Name tab and select the object that you uploaded in Step 2.

    2. Click the Headers tab. In the Response Headers section, obtain the ID of the request from x-oss-request-id. log