Creating a Bucket (SDK for Node.js)
Function
OBS buckets are containers for storing objects you upload to OBS. This API creates a bucket.
When creating a bucket, you can also configure parameters such as the storage class, region, and access control list (ACL) as needed.
Restrictions
- To create a bucket, you must have the obs:bucket:CreateBucket permission.
- A maximum of 100 buckets (regardless of regions) can be created for an account. There is no limit on the number and size of objects in a bucket.
- A bucket name must be unique in OBS. If you repeatedly create buckets with the same name in the same region, an HTTP status code 200 will be returned. In other cases, creating a bucket with the same name as an existing bucket will have an HTTP status code 409 returned, indicating that such a bucket already exists.
- The name of a deleted bucket can be reused for another bucket or a parallel file system at least 30 minutes after the deletion.
Method
ObsClient.createBucket(params)
Request Parameters
|
Parameter |
Type |
Mandatory (Yes/No) |
Description |
|---|---|---|---|
|
Bucket |
string |
Yes |
Explanation: Bucket name. Restrictions:
Value range: The value can contain 3 to 63 characters. Default value: None |
|
Location |
string |
Yes if the OBS service region is not the default region |
Explanation: Region where a bucket is located. |
|
ACL |
No |
Explanation: ACL that can be pre-defined when a bucket is created. Restrictions: None Value range: For details about the ACL options, see AclType. Default value: private |
|
|
StorageClass |
No |
Explanation: Bucket storage class that can be specified at bucket creation. Restrictions: None Value range: See StorageClassType. Default value: STANDARD |
|
|
GrantRead |
string |
No |
Explanation: ID (domain_id) of an account the READ permission is granted to. The account with the READ permission can list objects, multipart uploads, and object versions in the bucket you are creating, as well as obtain bucket metadata. Restrictions: None Value range: To obtain the account ID, see How Do I Get My Account ID and User ID? (Node.js SDK). Default value: None |
|
GrantWrite |
string |
No |
Explanation: ID (domain_id) of an account the WRITE permission is granted to. The account with the WRITE permission can create, delete, and overwrite objects in the bucket you are creating; initiate or abort multipart uploads; and upload, copy, and assemble parts. Restrictions: None Value range: To obtain the account ID, see How Do I Get My Account ID and User ID? (Node.js SDK). Default value: None |
|
GrantReadACP |
string |
No |
Explanation: ID (domain_id) of an account the READ_ACP permission is granted to. The account with the READ_ACP permission can read the ACL of the bucket you are creating. Restrictions: None Value range: To obtain the account ID, see How Do I Get My Account ID and User ID? (Node.js SDK). Default value: None |
|
GrantWriteACP |
string |
No |
Explanation: ID (domain_id) of an account the WRITE_ACP permission is granted to. The account with the WRITE_ACP permission can modify the ACL of the bucket you are creating. Restrictions: None Value range: To obtain the account ID, see How Do I Get My Account ID and User ID? (Node.js SDK). Default value: None |
|
GrantFullControl |
string |
No |
Explanation: ID (domain_id) of an account the FULL_CONTROL permission is granted to. The account with the FULL_CONTROL permission can perform any operation on the bucket you are creating. Restrictions: None Value range: To obtain the account ID, see How Do I Get My Account ID and User ID? (Node.js SDK). Default value: None |
|
GrantReadDelivered |
string |
No |
Explanation: ID (domain_id) of an account the READ permission is granted to. By default, this READ permission applies to all objects in the bucket. Restrictions: None Value range: To obtain the account ID, see How Do I Get My Account ID and User ID? (Node.js SDK). Default value: None |
|
GrantFullControlDelivered |
string |
No |
Explanation: ID (domain_id) of an account the FULL_CONTROL permission is granted to. The account with the FULL_CONTROL permission has full control over the bucket you are creating. By default, the FULL_CONTROL permission applies to all objects in the bucket. Restrictions: None Value range: To obtain the account ID, see How Do I Get My Account ID and User ID? (Node.js SDK). Default value: None |
|
MultiEnterprise |
string |
No |
Explanation: Enterprise project ID that can be specified during bucket creation. If you have enabled EPS, you can obtain the project ID from the EPS console. Restrictions: The value of epid is a UUID. epid is not required if you have not enabled EPS yet. Example: 9892d768-2d13-450f-aac7-ed0e44c2585f Default value: None |
|
Constant |
Default Value |
Description |
|---|---|---|
|
ObsClient.enums.AclPrivate |
private |
Private read and write A bucket or object can only be accessed by its owner. |
|
ObsClient.enums.AclPublicRead |
public-read |
Public read and private write If this permission is granted on a bucket, anyone can read the object list, multipart uploads, metadata, and object versions in the bucket. If this permission is granted on an object, everyone can obtain the content and metadata of the object. |
|
ObsClient.enums.AclPublicReadWrite |
public-read-write |
Public read and write If this permission is granted on a bucket, anyone can read the object list, multipart tasks, metadata, and object versions in the bucket and can upload or delete objects, initiate multipart upload tasks, upload parts, assemble parts, copy parts, and abort multipart upload tasks. If this permission is granted on an object, everyone can obtain the content and metadata of the object. |
|
ObsClient.enums.AclPublicReadDelivered |
public-read-delivered |
Public read on a bucket as well as the objects in the bucket. If this permission is granted on a bucket, anyone can read the object list, multipart tasks, metadata, and object versions and read the content and metadata of objects in the bucket.
NOTE:
AclPublicReadDelivered does not apply to objects. |
|
ObsClient.enums.AclPublicReadWriteDelivered |
public-read-write-delivered |
Public read and write on a bucket as well as the objects in the bucket. If this permission is granted on a bucket, anyone can read the object list, multipart uploads, metadata, and object versions in the bucket and can upload or delete objects, initiate multipart upload tasks, upload parts, assemble parts, copy parts, and abort multipart uploads. They can also read the content and metadata of objects in the bucket.
NOTE:
AclPublicReadWriteDelivered does not apply to objects. |
|
ObsClient.enums.AclBucketOwnerFullControl |
bucket-owner-full-control |
If this permission is granted on an object, only the bucket and object owners have the full control over the object. By default, if you upload an object to a bucket of any other user, the bucket owner does not have the permissions on your object. After you grant this policy to the bucket owner, the bucket owner can have full control over your object. For example, if user A uploads object x to user B's bucket, user B does not have the control over object x. If user A sets the bucket-owner-full-control policy for object x, user B then has the control over object x. |
|
Constant |
Default Value |
Description |
|---|---|---|
|
ObsClient.enums.StorageClassStandard |
STANDARD |
Standard storage class. Features low access latency and high throughput and is used for storing massive, frequently accessed (multiple times a month) or small objects (< 1 MB) requiring quick response. |
|
ObsClient.enums.StorageClassWarm |
WARM |
Warm storage class. Used for storing data that is semi-frequently accessed (fewer than 12 times a year) but becomes instantly available when needed. |
|
ObsClient.enums.StorageClassCold |
COLD |
Cold storage class. Used for storing rarely accessed (once a year) data. |
Responses
|
Type |
Description |
|---|---|
|
NOTE:
This API returns a Promise response, which requires the Promise or async/await syntax. |
Explanation: Returned results. For details, see Table 5. |
|
Parameter |
Type |
Description |
|---|---|---|
|
CommonMsg |
Explanation: Common information generated after an API call is complete, including the HTTP status code and error code. For details, see Table 6. |
|
|
InterfaceResult |
Explanation: Results outputted for a successful call. For details, see Table 7. Restrictions: This parameter is not included if the value of Status is greater than 300. |
|
Parameter |
Type |
Description |
|
Status |
number |
Explanation: HTTP status code returned by the OBS server. Value range: A status code is a group of digits indicating the status of a response. It ranges from 2xx (indicating successes) to 4xx or 5xx (indicating errors). |
|
Code |
string |
Explanation: Error code returned by the OBS server. |
|
Message |
string |
Explanation: Error description returned by the OBS server. |
|
HostId |
string |
Explanation: Request server ID returned by the OBS server. |
|
RequestId |
string |
Explanation: Request ID returned by the OBS server. |
|
Id2 |
string |
Explanation: Request ID2 returned by the OBS server. |
|
Indicator |
string |
Explanation: Error code details returned by the OBS server. |
Code Examples
This example creates a bucket named examplebucket. For bucket details, see the code comments below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
// Import the OBS library. // Use npm to install the client. const ObsClient = require("esdk-obs-nodejs"); // Use the source code to install the client. // var ObsClient = require('./lib/obs'); // Create an ObsClient instance. const obsClient = new ObsClient({ // Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage. // Obtain an AK/SK pair on the management console. access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. // security_token: process.env.SECURITY_TOKEN, // Enter the endpoint of the region where the bucket is located. server: "https://your-endpoint" }); async function createBucket() { try { const params = { // Specify the bucket name. Bucket: "examplebucket", // Specify the region where the bucket is to be created. The region must be the same as that in the passed endpoint. // Location: "region", // Specify the bucket ACL. The following uses obs.AclPrivate as an example. ACL: obsClient.enums.AclPrivate, // Specify the storage class of the bucket. obs.StorageClassWarm () is used as an example. If this parameter is not specified, the bucket is created with the Standard storage class. StorageClass: obsClient.enums.StorageClassWarm, }; // Create a bucket. const result = await obsClient.createBucket(params); if (result.CommonMsg.Status <= 300) { console.log("Create bucket(%s) successful!", params.Bucket); console.log("RequestId: %s", result.CommonMsg.RequestId); return; }; console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response."); console.log("Status: %d", result.CommonMsg.Status); console.log("Code: %s", result.CommonMsg.Code); console.log("Message: %s", result.CommonMsg.Message); console.log("RequestId: %s", result.CommonMsg.RequestId); } catch (error) { console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network."); console.log(error); }; }; createBucket(); |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot