Remove object tags (Swift SDK)

Updated at:

This topic describes how to remove tags from an object using the Swift SDK.

Precautions

  • The sample code in this topic uses the China (Hangzhou) region ID cn-hangzhou as an example. By default, a public endpoint is used. If you access OSS from other Alibaba Cloud services in the same region, you must use an internal endpoint. For more information about the regions and endpoints that OSS supports, see Regions and endpoints.

  • To remove object tags, you must have the oss:DeleteObjectTagging permission. For more information, see Grant custom access policies to RAM users.

Note
  • Object tags are key-value pairs that are used to mark objects. For more information about object tags, see Object tagging.

  • For more information about removing object tags, see DeleteObjectTagging.

Sample code

You can use the following code to remove tags from a specified object in a bucket.

import AlibabaCloudOSS
import Foundation

@main
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" // Replace with the actual region ID, such as cn-hangzhou.
            // Specify the bucket name.
            let bucket = "your-bucket-name" // Replace with your bucket name.
            // Optional. Specify the domain name used to access OSS. For example, if the region is China (Hangzhou), set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
            // If you do not specify this parameter, the SDK automatically determines the endpoint.
            let endpoint: String? = nil
            // Specify the name of the object from which you want to remove tags, such as document.txt.
            let key = "document.txt" // Replace with the actual object name.

            // Load AccessKey credentials from environment variables. You must set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables in advance.
            let credentialsProvider = EnvironmentCredentialsProvider()

            // Configure OSS client parameters. Chain calls to ensure all settings are applied.
            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 an OSS client instance.
            let client = Client(config)

            // Remove all tags from the specified object.
            let result = try await client.deleteObjectTagging(
                DeleteObjectTaggingRequest(
                    bucket: bucket,
                    key: key
                )
            )
            print("result:\n\(result)") // Print the operation result.

        } catch {
            // Print the error.
            print("error: \(error)")
        }
    }
}

References

  • For the complete sample code that shows how to remove object tags, see the GitHub sample.

  • For more information about the API operation for removing object tags, see DeleteObjectTagging.