Todos los productos
Search
Centro de documentos

Object Storage Service:Bloqueo de acceso público global con OSS SDK for PHP 2.0

Última actualización: Sep 22, 2026

En este tema se describe cómo habilitar el bloqueo de acceso público global para OSS, obtener su información de configuración y eliminar dicha información mediante OSS SDK for PHP 2.0.

Notas de uso

  • El código de ejemplo de este tema utiliza el identificador de región cn-hangzhou, que corresponde a la región China (Hangzhou). De forma predeterminada, se usa un endpoint público para acceder a los recursos de un bucket. Si necesita acceder a los recursos del bucket desde otros servicios de Alibaba Cloud ubicados en la misma región que el bucket, utilice un endpoint interno. Para más información sobre las regiones y los endpoints compatibles con OSS, consulte Regions and endpoints.

Código de ejemplo

Habilitar el bloqueo de acceso público global para OSS

A continuación se muestra un ejemplo de código para habilitar el bloqueo de acceso público global.

<?php

// Import the autoloader file to load dependency libraries.
require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define the description of command line arguments.
$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region is a required argument. It specifies the region where the bucket is located.
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is an optional argument. It specifies the domain name that other services can use to access OSS.
];

// Generate a list of long options to parse command line arguments.
$longopts = \array_map(function ($key) {
    return "$key:"; // A colon (:) is added after each parameter to indicate that a value is required.
}, array_keys($optsdesc));

// Parse command line arguments.
$options = getopt("", $longopts);

// Check whether required arguments are missing.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help"; // Prompt the user that a required argument is missing.
        exit(1);
    }
}

// Obtain the values of command line arguments.
$region = $options["region"]; // The region where the bucket is located.

// Use environment variables to load credential information, including AccessKeyId and AccessKeySecret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();

// Set the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);

// Set the region.
$cfg->setRegion($region);

// If an endpoint is provided, set the endpoint.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

// Create an OSS client instance.
$client = new Oss\Client($cfg);

// Build a PutPublicAccessBlock request to set the Block Public Access configuration.
$request = new Oss\Models\PutPublicAccessBlockRequest(
    publicAccessBlockConfiguration: new Oss\Models\PublicAccessBlockConfiguration(blockPublicAccess: true) // Configure to block public access.
);

// Call the putPublicAccessBlock method to set the Block Public Access configuration and accept the returned result.
$result = $client->putPublicAccessBlock($request);

// Print the returned result, including the status code and request ID.
printf(
    'status code:' . $result->statusCode . PHP_EOL .
    'request id:' . $result->requestId
);

Obtener la información de configuración del bloqueo de acceso público global para OSS

A continuación se muestra un ejemplo de código para obtener la información de configuración del bloqueo de acceso público global.

<?php

// Import the autoloader file to load dependency libraries.
require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define the description of command line arguments.
$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region is a required argument. It specifies the region where the bucket is located.
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is an optional argument. It specifies the domain name that other services can use to access OSS.
];

// Generate a list of long options to parse command line arguments.
$longopts = \array_map(function ($key) {
    return "$key:"; // A colon (:) is added after each parameter to indicate that a value is required.
}, array_keys($optsdesc));

// Parse command line arguments.
$options = getopt("", $longopts);

// Check whether required arguments are missing.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help"; // Prompt the user that a required argument is missing.
        exit(1);
    }
}

// Obtain the values of command line arguments.
$region = $options["region"]; // The region where the bucket is located.

// Use environment variables to load credential information, including AccessKeyId and AccessKeySecret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();

// Set the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);

// Set the region.
$cfg->setRegion($region);

// If an endpoint is provided, set the endpoint.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

// Create an OSS client instance.
$client = new Oss\Client($cfg);

// Build a GetPublicAccessBlock request to obtain the Block Public Access configuration.
$request = new Oss\Models\GetPublicAccessBlockRequest();

// Call the getPublicAccessBlock method to obtain the Block Public Access configuration and accept the returned result.
$result = $client->getPublicAccessBlock($request);

// Print the returned result, including the status code, request ID, and Block Public Access configuration.
printf(
    'status code:' . $result->statusCode . PHP_EOL .
    'request id:' . $result->requestId . PHP_EOL .
    'public access block config:' . var_export($result->publicAccessBlockConfiguration, true)
);

Eliminar la información de configuración del bloqueo de acceso público global para OSS

A continuación se muestra un ejemplo de código para eliminar la información de configuración del bloqueo de acceso público global.

<?php

// Import the autoloader file to load dependency libraries.
require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define the description of command line arguments.
$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region is a required argument. It specifies the region where the bucket is located.
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is an optional argument. It specifies the domain name that other services can use to access OSS.
];

// Generate a list of long options to parse command line arguments.
$longopts = \array_map(function ($key) {
    return "$key:"; // A colon (:) is added after each parameter to indicate that a value is required.
}, array_keys($optsdesc));

// Parse command line arguments.
$options = getopt("", $longopts);

// Check whether required arguments are missing.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help"; // Prompt the user that a required argument is missing.
        exit(1);
    }
}

// Obtain the values of command line arguments.
$region = $options["region"]; // The region where the bucket is located.

// Use environment variables to load credential information, including AccessKeyId and AccessKeySecret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();

// Set the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);

// Set the region.
$cfg->setRegion($region);

// If an endpoint is provided, set the endpoint.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

// Create an OSS client instance.
$client = new Oss\Client($cfg);

// Build a DeletePublicAccessBlock request to delete the Block Public Access configuration.
$request = new Oss\Models\DeletePublicAccessBlockRequest();

// Call the deletePublicAccessBlock method to delete the Block Public Access configuration and accept the returned result.
$result = $client->deletePublicAccessBlock($request);

// Print the returned result, including the status code and request ID.
printf(
    'status code:' . $result->statusCode . PHP_EOL .
    'request id:' . $result->requestId
);

Referencias

  • Para consultar el código de ejemplo completo que permite gestionar el bloqueo de acceso público global para OSS, visite GitHub.

  • Para más información sobre la operación de API utilizada para habilitar el bloqueo de acceso público global para OSS, consulte PutPublicAccessBlock.

  • Para más información sobre la operación de API utilizada para obtener la información de configuración del bloqueo de acceso público global, consulte GetPublicAccessBlock.

  • Para más información sobre la operación de API utilizada para eliminar la información de configuración del bloqueo de acceso público global, consulte DeletePublicAccessBlock.