Get object tags (Swift SDK)

Updated at:

This topic describes how to use the Swift software development kit (SDK) to retrieve the tag information for an object.

Notes

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

  • To retrieve object tags, you must have the oss:GetObjectTagging permission. For more information, see Grant custom permissions to a RAM user.

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

  • For more information about how to retrieve object tags, see GetObjectTagging.

Sample code

Use the following code to retrieve the tag information for a specified object in a bucket.

import AlibabaCloudOSS
import Foundation

@main
struct Main {
    static func main() async {
        do {
            // Specify the bucket region. Example: cn-hangzhou for China (Hangzhou).
            let region = "cn-hangzhou" // The actual region ID for China (Hangzhou).
            // Specify the bucket name.
            let bucket = "your-bucket-name" // The actual bucket name.
            // Optional. Specify the OSS endpoint. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
            let endpoint: String? = nil
            // Specify the object name. Example: document.txt.
            let key = "document.txt" // The actual object name.

            // Load access credentials from environment variables. Before you run the sample code, set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
            let credentialsProvider = EnvironmentCredentialsProvider()

            // Configure OSS client parameters. The chained call ensures that all settings are applied.
            let config = Configuration.default()
                .withRegion(region)          // Set the bucket region.
                .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)

            // Get the tags of the specified object.
            let result = try await client.getObjectTagging(
                GetObjectTaggingRequest(
                    bucket: bucket,
                    key: key
                )
            )
            print("result:\n\(result)") // Print the tags.

        } catch {
            // Handle errors.
            print("error: \(error)")
        }
    }
}
-

References

  • For the complete sample code for retrieving object tags, see GitHub sample.

  • For more information about the API operation for retrieving object tags, see GetObjectTagging.