All Products
Search
Document Center

Object Storage Service:Get the region of a bucket (C SDK)

Last Updated:Mar 20, 2026

Call oss_get_bucket_location to retrieve the region where a bucket resides.

Prerequisites

Before you begin, ensure that you have:

Note: This example uses the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For a full list of regions and endpoints, see Regions and endpoints.

Get the bucket region

The following example calls oss_get_bucket_location and prints the region string from oss_location.data.

#include "oss_api.h"
#include "aos_http_io.h"

/* Replace with your bucket's endpoint. Example: https://oss-cn-hangzhou.aliyuncs.com */
const char *endpoint = "yourEndpoint";

/* Replace with your bucket name. */
const char *bucket_name = "yourBucket";

/* Replace with the region where your bucket resides. Example: cn-hangzhou */
const char *region = "yourRegion";

void init_options(oss_request_options_t *options)
{
    options->config = oss_config_create(options->pool);

    /* Initialize endpoint, credentials, and region. */
    aos_str_set(&options->config->endpoint, endpoint);

    /* Read credentials from environment variables.
       Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example. */
    aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
    aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));

    aos_str_set(&options->config->region, region);
    options->config->signature_version = 4;

    /* Set is_cname to 0 to use the OSS endpoint directly (not a CNAME). */
    options->config->is_cname = 0;

    /* Configure network settings such as timeout. */
    options->ctl = aos_http_controller_create(options->pool, 0);
}

int main(int argc, char *argv[])
{
    /* Initialize global resources (network, memory) at program entry. */
    if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
        exit(1);
    }

    /* Create a memory pool for this request. */
    aos_pool_t *pool;
    aos_pool_create(&pool, NULL);

    /* Create and initialize client options. */
    oss_request_options_t *oss_client_options;
    oss_client_options = oss_request_options_create(pool);
    init_options(oss_client_options);

    /* Initialize request variables. */
    aos_string_t bucket;
    aos_string_t oss_location;
    aos_table_t *resp_headers = NULL;
    aos_status_t *resp_status = NULL;

    aos_str_set(&bucket, bucket_name);

    /* Call GetBucketLocation. On success, oss_location.data contains the region string. */
    resp_status = oss_get_bucket_location(oss_client_options, &bucket, &oss_location, &resp_headers);
    if (aos_status_is_ok(resp_status)) {
        printf("get bucket location succeeded: %s\n", oss_location.data);
    } else {
        printf("get bucket location failed\n");
    }

    /* Release the memory pool and global resources. */
    aos_pool_destroy(pool);
    aos_http_io_deinitialize();
    return 0;
}

API reference

GetBucketLocation