Un bucket est un conteneur de stockage d'objets. Tous les objets doivent résider dans un bucket. Cette rubrique explique comment créer un bucket.
Notes
Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Pour accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour connaître la liste des régions et des points de terminaison pris en charge, consultez Régions et points de terminaison.
Les identifiants d'accès utilisés dans cette rubrique sont extraits des variables d'environnement. Pour savoir comment configurer ces identifiants, consultez Configuration des identifiants d'accès.
Cette rubrique montre comment créer une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, comme l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Configuration du client.
À partir du 13 octobre 2025 à 10 h 00 (UTC+8), OSS active progressivement par défaut la fonctionnalité Blocage de l'accès public pour les buckets créés via des appels d'API, à l'aide des SDK OSS ou d'ossutil, dans toutes les régions. Pour connaître la date d'entrée en vigueur de cette modification dans chaque région, consultez l'Annonce. Une fois cette fonctionnalité activée, vous ne pouvez pas accorder d'autorisations d'accès public, y compris les listes de contrôle d'accès (ACL) public-read ou public-read-write, ni les politiques de bucket autorisant l'accès public. Si votre activité nécessite un accès public, désactivez le blocage de l'accès public après la création du bucket.
Autorisations
Par défaut, un compte Alibaba Cloud dispose de toutes les autorisations. Les utilisateurs RAM ou les rôles RAM associés à un compte Alibaba Cloud ne disposent d'aucune autorisation par défaut. Le compte Alibaba Cloud ou l'administrateur du compte doit accorder les autorisations d'opération via des politiques RAM ou une politique de bucket.
|
API |
Action |
Description |
|
PutBucket |
|
Crée un bucket. |
|
|
Après la création d'un bucket, cette autorisation est requise pour modifier l'ACL du bucket. |
Exemple de code
Le code suivant montre comment créer un bucket nommé examplebucket.
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Exception {
// Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
String endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run this 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 bucket name.
String bucketName = "examplebucket";
// Specify the resource group ID. If you do not specify a resource group ID, the bucket is added to the default resource group.
//String rsId = "rg-aek27tc****";
// Specify the region where the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set Region to cn-hangzhou.
String region = "cn-hangzhou";
// Create an OSSClient instance.
// After an OSSClient instance is no longer used, call the shutdown method to release resources.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Create a bucket and enable the hierarchical namespace feature.
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName).withHnsStatus(HnsStatus.Enabled);
// If you want to specify the storage class, ACL, and data redundancy type when you create a bucket, see the following code.
// The following code provides an example of how to set the storage class to Standard.
createBucketRequest.setStorageClass(StorageClass.Standard);
// The default data redundancy type is LRS, which is specified as DataRedundancyType.LRS. If you want to set the data redundancy type to ZRS, set the value to DataRedundancyType.ZRS.
createBucketRequest.setDataRedundancyType(DataRedundancyType.ZRS);
// Set the ACL of the bucket to public-read. The default ACL is private.
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
// When you create a bucket in a region that supports resource groups, you can assign the bucket to a resource group.
//createBucketRequest.setResourceGroupId(rsId);
// Create the bucket.
ossClient.createBucket(createBucketRequest);
} 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 création d'un bucket, consultez l'exemple GitHub.
Pour plus d'informations sur l'opération API permettant de créer un bucket, consultez PutBucket.