Manage versioning (Swift SDK)

Updated at:

Versioning applies to all objects in a bucket. After versioning is enabled, you can restore an object to any previous version if it is accidentally overwritten or deleted.

Notes

  • The sample code in this topic uses the China (Hangzhou) region ID cn-hangzhou and a public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the mappings between regions and endpoints, see Regions and endpoints.

  • To set the versioning state of a bucket, you must have the oss:PutBucketVersioning permission. To retrieve the versioning state of a bucket, you must have the oss:GetBucketVersioning permission. For more information, see Grant custom permissions to a RAM user.

Sample code

Set the versioning state of a bucket

The following code enables versioning for a bucket.

import AlibabaCloudOSS 
import Foundation

struct Main {  
    static func main() async {  
        do {  
            // Specify the region where the bucket is located. Example: China (Hangzhou) is cn-hangzhou.
            let region = "cn-hangzhou"
            // Specify the bucket name.
            let bucket = "yourBucketName"
            // Optional. Specify the domain name to access OSS. For example, for China (Hangzhou), set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
            let endpoint: String? = nil
            // Specify the versioning state. 
            let status = "yourStatus" 

            // 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 set.
            let credentialsProvider = EnvironmentCredentialsProvider()
            
            // Configure OSS client parameters.
            let config = Configuration.default()
                .withRegion(region)        // Set the region where the bucket is located.
                .withCredentialsProvider(credentialsProvider)  // Set the access credentials.
                
            // Set the endpoint.
            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }

                
            // Create a client. 
            let client = Client(config)  

            // Execute the operation.  
            let result = try await client.putBucketVersioning(
                PutBucketVersioningRequest(  
                    bucket: bucket, // Specify the bucket.  
                    versioningConfiguration: VersioningConfiguration(status: status) // Configure the state.  
                )  
            )  
            // Print the result.  
            print("result:\n\(result)") 
        } catch {  
            // Print the error.
            print("error: \(error)")
        }  
    }  
}  

Get the versioning state of a bucket

The following code retrieves the versioning state of a bucket.

import AlibabaCloudOSS 
import Foundation

struct Main {  
    static func main() async {  
        do {  
            // Specify the region where the bucket is located. Example: China (Hangzhou) is cn-hangzhou.
            let region = "cn-hangzhou"
            // Specify the bucket name.
            let bucket = "yourBucketName"
            // Specify the endpoint for the bucket. Example: For China (Hangzhou), the endpoint is https://oss-cn-hangzhou.aliyuncs.com.
            let endpoint: String? = "https://oss-cn-hangzhou.aliyuncs.com"

            // 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 set.
            let credentialsProvider = EnvironmentCredentialsProvider()
            
            // Configure OSS client parameters.
            let config = Configuration.default()
                .withRegion(region)        // Set the region where the bucket is located.
                .withCredentialsProvider(credentialsProvider)  // Set the access credentials.
                
            // Set the endpoint.
            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }
                
            // Create a client. 
            let client = Client(config)  

            // Execute the operation.  
            let result = try await client.getBucketVersioning(
                GetBucketVersioningRequest(
                    bucket: bucket
                )
            )
            // Print the result.  
            print("result:\n\(result)") 
        } catch {  
            // Print the error.
            print("error: \(error)")
        }
    }
}

References