Before you can upload objects to Object Storage Service (OSS), you must create a bucket. You can configure a variety of attributes for a bucket, including the region, access control list (ACL), and storage class. You can create buckets of different storage classes to store your data.
Usage notes
- When you create a bucket, you are charged only for the storage of objects in the bucket and the traffic generated when the objects are accessed. For more information, see Overview.
- The capacity of the bucket is scalable. You do not need to purchase the capacity before you use the bucket.
Limits
- You can use an Alibaba Cloud account to create up to 100 buckets in the same region.
- A bucket name must be globally unique within OSS. For more information about the naming conventions of buckets, see Bucket naming conventions.
- After a bucket is created, you cannot modify its name, region, storage class or redundancy type.
- OSS does not impose limits on the capacity of a bucket.
Use the OSS console
Use ossbrowser
ossbrowser allows you to perform bucket management operations that you can perform in the OSS console. You can follow the on-screen instructions in ossbrowser to create a bucket. For more information about how to use ossbrowser, see Use ossbrowser.
Use OSS SDKs
The following code provides examples on how to create a bucket by using OSS SDKs for common programming languages. For more information about how to create a bucket by using OSS SDKs for other programming languages, see Overview.
<?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;
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
$accessKeyId = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
// In this example, the endpoint of the China (Hangzhou) region is used. Specify the endpoint based on your business requirements.
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Specify the name of the bucket. Example: examplebucket.
$bucket= "examplebucket;
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
// Set the storage class of the bucket to Infrequent Access (IA). The default value is Standard.
$options = array(
OssClient::OSS_STORAGE => OssClient::OSS_STORAGE_IA
);
// Set the access control list (ACL) of the bucket to public-read. The default value is private.
$ossClient->createBucket($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
const OSS = require('ali-oss');
const client = new OSS({
// Set yourregion to the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourregion to oss-cn-hangzhou.
region: 'yourregion',
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. 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'
});
// Create the bucket.
async function putBucket() {
try {
const options = {
storageClass: 'Standard', // By default, the storage class of a bucket is Standard. To set the storage class of the bucket to Archive, set storageClass to Archive.
acl: 'private', // By default, the ACL of a bucket is private. To set the ACL of the bucket to public read, set acl to public-read.
dataRedundancyType: 'LRS' // By default, the data redundancy type of a bucket is locally redundant storage (LRS). To set the data redundancy type of the bucket to zone-redundant storage (ZRS), set dataRedundancyType to ZRS.
}
// Specify the bucket name.
const result = await client.putBucket('examplebucket', options);
console.log(result);
} catch (err) {
console.log(err);
}
}
putBucket();
# -*- coding: utf-8 -*-
import oss2
# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint 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')
# Create the bucket.
# The following code provides an example on how to specify the storage class, access control list (ACL), and redundancy type when you create a bucket.
# In this example, the storage class is Standard, the ACL is private, and the redundancy type is zone-redundant storage (ZRS).
# bucketConfig = oss2.models.BucketCreateConfig(oss2.BUCKET_STORAGE_CLASS_STANDARD, oss2.BUCKET_DATA_REDUNDANCY_TYPE_ZRS)
# bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE, bucketConfig)
bucket.create_bucket()
using Aliyun.OSS;
// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
var accessKeyId = "yourAccessKeyId";
var accessKeySecret = "yourAccessKeySecret";
// Specify the bucket name.
var bucketName = "examplebucket";
// Initialize an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
// Create the bucket.
public void CreateBucket(string bucketName)
{
try
{
var request = new CreateBucketRequest(bucketName);
// Set the access control list (ACL) of the bucket to public-read (PublicRead). The default value is private.
request.ACL = CannedAccessControlList.PublicRead;
// Set the redundancy type of the bucket to zone-redundant storage (ZRS).
request.DataRedundancyType = DataRedundancyType.ZRS;
client.CreateBucket(request);
Console.WriteLine("Create bucket succeeded");
}
catch (Exception ex)
{
Console.WriteLine("Create bucket failed. {0}", ex.Message);
}
}
// Construct a request to create a bucket.
// Specify the bucket name. For more information about the naming conventions for buckets, see Bucket naming conventions.
CreateBucketRequest createBucketRequest = new CreateBucketRequest("examplebucket");
// Specify the region in which the bucket is located. In this example, the bucket is located in the China (Hangzhou) region. For more information about supported regions, see Regions and endpoints.
CreateBucketRequest.setLocationConstraint("oss-cn-hangzhou");
// Specify the access control list (ACL) of the bucket. For more information about ACLs, see Configure the ACL feature for a bucket.
// CreateBucketRequest.setBucketACL(CannedAccessControlList.Private);
// Specify the storage class of the bucket. For more information about storage classes, see Overview.
// CreateBucketRequest.setBucketStorageClass(StorageClass.Standard);
// Create the bucket in asynchronous mode.
OSSAsyncTask createTask = oss.asyncCreateBucket(createBucketRequest, new OSSCompletedCallback<CreateBucketRequest, CreateBucketResult>() {
@Override
public void onSuccess(CreateBucketRequest request, CreateBucketResult result) {
Log.d("asyncCreateBucket", "Success");
}
@Override
public void onFailure(CreateBucketRequest request, ClientException clientException, ServiceException serviceException) {
// Handle request exceptions.
if (clientException != null) {
// Handle client exceptions, such as network exceptions.
clientException.printStackTrace();
}
if (serviceException != null) {
// Handle service exceptions.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements.
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create a bucket named examplebucket of which the storage class is IA (oss.StorageIA), the access control list (ACL) is public-read (oss.ACLPublicRead), and the redundancy type is zone-redundant storage (oss. Redundant ZRS).
err = client.CreateBucket("examplebucket", oss.StorageClass(oss.StorageIA), oss.ACL(oss.ACLPublicRead), oss.RedundancyType(oss.RedundancyZRS))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
id<OSSCredentialProvider> credentialProvider = [[OSSAuthCredentialProvider alloc] initWithAuthServerUrl:@"<StsServer>"];
OSSClientConfiguration *cfg = [[OSSClientConfiguration alloc] init];
cfg.maxRetryCount = 3;
cfg.timeoutIntervalForRequest = 15;
// If an OSSClient is released, all tasks in the session are canceled and the session becomes invalid. Make sure that the OSSClient is not released before the request is complete.
// Set Endpoint to the endpoint of the region in which 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.
OSSClient *client = [[OSSClient alloc] initWithEndpoint:@"Endpoint" credentialProvider:credentialProvider clientConfiguration:cfg];
OSSCreateBucketRequest * create = [OSSCreateBucketRequest new];
// Set the bucket name to examplebucket.
create.bucketName = @"examplebucket";
// Set the access control list (ACL) of the bucket to private.
create.xOssACL = @"private";
// Set the storage class of the bucket to Infrequent Access (IA).
create.storageClass = OSSBucketStorageClassIA;
OSSTask * createTask = [client createBucket:create];
[createTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
NSLog(@"create bucket success!");
} else {
NSLog(@"create bucket failed, error: %@", task.error);
}
return nil;
}];
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the account information. */
/* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. */
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
/* Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
/*Specify the name, storage class, and access control list (ACL) of the bucket. */
CreateBucketRequest request(BucketName, StorageClass::IA, CannedAccessControlList::PublicReadWrite);
/* Set the redundancy type of the bucket to zone-redundant storage (ZRS). */
//request.setDataRedundancyType(DataRedundancyType::ZRS);
/* Create the bucket. */
auto outcome = client.CreateBucket(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "CreateBucket fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
}
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
const char *bucket_name = "<yourBucketName>";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* Use a char* string to initialize the aos_string_t data type. */
aos_str_set(&options->config->endpoint, endpoint);
aos_str_set(&options->config->access_key_id, access_key_id);
aos_str_set(&options->config->access_key_secret, access_key_secret);
/* Specify whether to use CNAME. 0 indicates that the CNAME is not used. */
options->config->is_cname = 0;
/* Configure network parameters, such as timeout. */
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* Call aos_http_io_initialize in main() to initialize global resources such as the network and memory. */
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
}
/* Create a memory pool to manage memory. The implementation code is included in the APR library. aos_pool_t is equivalent to apr_pool_t. */
aos_pool_t *pool;
/* Create a new memory pool. The second parameter is NULL. This value indicates that the pool does not inherit any other memory pool. */
aos_pool_create(&pool, NULL);
/* Create and initialize options. This parameter includes global configuration information such as endpoint, access_key_id, access_key_secret, is_cname, and curl. */
oss_request_options_t *oss_client_options;
/* Allocate a memory chunk in the memory pool to options. */
oss_client_options = oss_request_options_create(pool);
/* Initialize oss_client_options. */
init_options(oss_client_options);
/* Initialize parameters. */
aos_string_t bucket;
oss_acl_e oss_acl = OSS_ACL_PRIVATE;
aos_table_t *resp_headers = NULL;
aos_status_t *resp_status = NULL;
/* Assign char* data to the aos_string_t bucket. */ */
aos_str_set(&bucket, bucket_name);
// Create the bucket. */
resp_status = oss_create_bucket(oss_client_options, &bucket, oss_acl, &resp_headers);
/* Determine whether the bucket is created. */ */
if (aos_status_is_ok(resp_status)) {
printf("create bucket succeeded\n");
} else {
printf("create bucket failed\n");
}
/* Release the memory pool. The memory allocated to various resources used for the request is released. */
aos_pool_destroy(pool);
/* Release the allocated global resources. */
aos_http_io_deinitialize();
return 0;
}
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
endpoint: 'endpoint',
access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
client.create_bucket('my-bucket')
Use ossutil
For more information about how to create a bucket by using ossutil, see mb (create buckets).
Use the RESTful API
If your program requires more custom options to create buckets, you can call RESTful API operations. In this case, you must manually write code to calculate the signature. For more information, see PutBucket.
References
- Object management
- Upload objects
After you create a bucket, you can upload objects to the bucket. For more information about how to upload objects, see Simple upload.
- Download objects
After you upload objects to a bucket, you can download the objects to the default download path of your browser or the specified local path. For more information about how to download objects, see Simple download.
- Share objects
You can share the URLs of uploaded objects with third parties for downloads or previews. For more information about how to share objects, see Share objects.
- Upload objects
- Access control
By default, the ACL of OSS resources is private to ensure data security. Only the owners of the resources and authorized users can access these resources. OSS allows you to configure a variety of policies to grant third-party users specific permissions to access or use your OSS resources. For more information about access control, see Overview.