Create a bucket (Swift SDK)

Updated at:

Create a bucket in Object Storage Service (OSS) by using the Alibaba Cloud OSS Swift SDK. A bucket is a container for objects, and all files you upload are stored in buckets.

Considerations

  • Starting at 10:00:00 (UTC+8) on October 13, 2025, OSS gradually enables Block public access by default for buckets that are created by calling API operations or by using OSS SDKs or ossutil in all regions. For the time when the change takes effect in each region, see Announcement. After this feature is enabled, you cannot grant public access permissions, including the public-read or public-read-write ACL and bucket policies that allow public access. If your business requires public access, you can disable block public access after the bucket is created.

  • Region and endpoint — The sample code in this topic uses the China (Hangzhou) region ID cn-hangzhou as an example and uses the public endpoint by default. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint. For the mapping between regions and endpoints, seeRegions and endpoints.

  • Anywhere mode — This topic uses a region-specific bucket as an example. To create a bucket in Anywhere mode by using the following code, replace the endpoint with https://oss-rg-china-mainland.aliyuncs.com. A bucket in Anywhere mode supports only the Standard storage class and only locally redundant storage (LRS) for disaster recovery, and does not support resource group configuration. For more information about buckets in Anywhere mode, seeRegions and buckets.

Prerequisites

  • Determine the region and endpoint for your bucket.

  • You must have the oss:PutBucket permission. For more information, see the Permissions section.

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables must be configured.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM policies or Bucket Policy.

API

Action

Description

PutBucket

oss:PutBucket

Creates a bucket.

oss:PutBucketAcl

After creating a bucket, this permission is required to modify the bucket ACL.

Sample code

The following code creates a bucket.

import AlibabaCloudOSS
import Foundation

@main
struct Main {
    static func main() async {

        do {

            // Specify the region in which the bucket is located. For example, set Region to cn-hangzhou for the China (Hangzhou) region.
            let region = "cn-hangzhou"
            // Specify the name of the bucket.
            let bucket = "yourBucketName"
            // Optional. Specify the domain name that is used to access OSS. For example, set Endpoint to https://oss-cn-hangzhou.aliyuncs.com for the China (Hangzhou) region.
            let endpoint: String? = nil

            // 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.
            let credentialsProvider = EnvironmentCredentialsProvider()

            // Configure the OSS client parameters.
            let config = Configuration.default()
                .withRegion(region) // Set the region.
                .withCredentialsProvider(credentialsProvider) // Set the credentials.

            // Set the endpoint.
            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }

            // Create an OSS client instance.
            let client = Client(config)

            // Create the bucket.
            let result = try await client.putBucket(
                PutBucketRequest(
                    bucket: bucket
                )
            )

            // Print the result.
            print("result:\n\(result)")

        } catch {
            // Catch and handle exceptions.
            print("error:\n\(error)")
        }
    }
}

References

  • For the complete sample code for creating a bucket, see the sample on GitHub.

  • For the API operation that creates a bucket, see PutBucket.