Tous les produits
Search
Centre de documentation

Object Storage Service:Gérer les ACL des objets (OSS SDK for Java 1.0)

Dernière mise à jour :Aug 18, 2026

Outre les listes de contrôle d'accès (ACL) au niveau du bucket, Object Storage Service (OSS) propose des ACL au niveau des objets. Vous pouvez configurer l'ACL d'un objet lors de son chargement ou modifier celle d'un objet déjà chargé.

Notes d'utilisation

  • Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Pour accéder à OSS depuis d'autres services Alibaba Cloud dans la même région, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et points de terminaison pris en charge, consultez Régions et points de terminaison.

  • Les identifiants d'accès sont obtenus à partir des variables d'environnement. Pour plus d'informations, consultez Configurer les informations d'identification d'accès.

  • Cette rubrique illustre la création d'une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Configuration du client.

  • Pour configurer l'ACL d'un objet, vous devez disposer de l'autorisation oss:PutObjectAcl. Pour interroger les ACL des objets, vous devez disposer de l'autorisation oss:GetObjectAcl. Pour plus d'informations, consultez Accorder une politique personnalisée.

Types d'ACL

Le tableau suivant décrit les ACL configurables pour un objet.

Remarque

L'ACL d'un objet est prioritaire sur celle du bucket dans lequel il est stocké. Par exemple, si l'ACL d'un objet situé dans un bucket privé est définie sur public-read, tous les utilisateurs, y compris les utilisateurs anonymes, peuvent lire l'objet.

Type d'ACL

Description

Valeur

Hérité du bucket

L'ACL de l'objet est identique à celle du bucket dans lequel il est stocké. Il s'agit de l'ACL par défaut d'un objet.

CannedAccessControlList.Default

Privé

Seul le propriétaire de l'objet peut le lire et l'écrire. Les autres utilisateurs ne peuvent pas y accéder.

CannedAccessControlList.Private

Public-read

Seul le propriétaire de l'objet peut l'écrire. Les autres utilisateurs, y compris les utilisateurs anonymes, peuvent uniquement le lire.

Avertissement

Cela peut entraîner un accès non autorisé et des coûts imprévus. Utilisez cette ACL avec prudence.

CannedAccessControlList.PublicRead

Public-read-write

Tous les utilisateurs, y compris les utilisateurs anonymes, peuvent lire et écrire l'objet.

Avertissement

Cette ACL permet à tous les utilisateurs d'accéder à l'objet et d'y écrire des données via Internet, ce qui peut entraîner un accès non autorisé, des coûts imprévus ou le chargement de contenu interdit. Ne définissez l'ACL de l'objet sur public-read-write que si cela est nécessaire.

CannedAccessControlList.PublicReadWrite

Configurer l'ACL d'un objet

L'exemple de code suivant montre comment configurer l'ACL d'un objet :

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.CannedAccessControlList;

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: testfolder/exampleobject.txt. 
        String objectName = "testfolder/exampleobject.txt";
        // 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 cn-hangzhou.
        String region = "cn-hangzhou";

        // Create an OSSClient instance. 
        // Call the shutdown method to release associated resources when the OSSClient is no longer in use.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);        
        OSS ossClient = OSSClientBuilder.create()
        .endpoint(endpoint)
        .credentialsProvider(credentialsProvider)
        .clientConfiguration(clientBuilderConfiguration)
        .region(region)               
        .build();

        try {
            // Set the ACL of the object to public read. 
            ossClient.setObjectAcl(bucketName, objectName, CannedAccessControlList.PublicRead);
        } 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();
            }
        }
    }
}            

Interroger l'ACL d'un objet

L'exemple de code suivant montre comment interroger l'ACL d'un objet :

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.ObjectAcl;

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: testfolder/exampleobject.txt. 
        String objectName = "testfolder/exampleobject.txt";
        // 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 cn-hangzhou.
        String region = "cn-hangzhou";

        // Create an OSSClient instance. 
        // Call the shutdown method to release associated resources when the OSSClient is no longer in use.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);        
        OSS ossClient = OSSClientBuilder.create()
        .endpoint(endpoint)
        .credentialsProvider(credentialsProvider)
        .clientConfiguration(clientBuilderConfiguration)
        .region(region)               
        .build();

        try {
            // Query the ACL of the object. 
            ObjectAcl objectAcl = ossClient.getObjectAcl(bucketName, objectName);
            System.out.println(objectAcl.getPermission().toString());
        } 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();
            }
        }
    }
}            

Références

  • Pour obtenir l'exemple de code complet relatif à la gestion des ACL des objets, visitez GitHub.

  • Pour plus d'informations sur l'opération API permettant de configurer l'ACL d'un objet, consultez PutObjectACL.

  • Pour plus d'informations sur l'opération API permettant d'interroger l'ACL d'un objet, consultez GetObjectACL.