Get object metadata (Swift SDK)

Updated at:

This topic describes how to query object metadata in a versioning-enabled Object Storage Service (OSS) bucket.

Notes

  • In this topic, the region ID cn-hangzhou of the China (Hangzhou) region is used as an example. By default, the public endpoint for a region is used. If you want to access OSS from other Alibaba Cloud services in the same region as the OSS bucket, we recommend that you use an internal endpoint for the region. For more information about a list of regions and endpoints for OSS, see Regions and endpoints.

  • To query object metadata, you must have the oss:GetObject permission. For more information, see Attach a custom policy to a RAM user.

Sample code

Use headObject to query all metadata headers of an object

The following sample code uses the headObject method to query all metadata headers of a specific object version:

import AlibabaCloudOSS
import Foundation

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

        do {

            // Specify the ID of the region where the bucket is located. Example: cn-hangzhou for China (Hangzhou).
            let region = "cn-hangzhou"
            // Specify the bucket name.
            let bucket = "yourBucketName"
            // (Optional) Specify the endpoint for accessing OSS. For example, if the bucket is located in China (Hangzhou), you can set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
            let endpoint: String? = nil
            // Specify the name of the object whose metadata you want to query.
            let key = "yourObjectName" // Replace the value with the actual object name.
            // Version ID
            let versionId = "versionId"

            // Obtain access credentials from the 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 OSS client parameters.
            let config = Configuration.default()
                .withRegion(region)        // Set the region where the bucket is located.
                .withCredentialsProvider(credentialsProvider)  // Set the access credential provider.
                
            // Use the custom endpoint.
            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }
            
            // Create an OSS Client instance.
            let client = Client(config)

            // Query metadata.
            let result = try await client.headObject(
                HeadObjectRequest(
                    bucket: bucket,
                    key: key,
                    versionId: versionId
                )
            )
            print("result:\n\(result)")

        } catch {
            // Error output
            print("error: \(error)")
        }
    }
}

Use getObjectMeta to query partial metadata of an object

Note

You can use the getObjectMeta method to query only partial metadata of an object, including the content length (ContentLength), entity tag (ETag), last modified time (LastModified), last access time (LastAccessTime), version ID (VersionId), and 64-bit CRC value (HashCRC64).

The following sample code uses the getObjectMeta method to query partial metadata of a specific object version:

import AlibabaCloudOSS
import Foundation

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

        do {
            // Specify the ID of the region where the bucket is located. Example: cn-hangzhou for China (Hangzhou).
            let region = "cn-hangzhou"
            // Specify the bucket name.
            let bucket = "yourBucketName"
            // (Optional) Specify the endpoint for accessing OSS. For example, if the bucket is located in China (Hangzhou), you can set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
            let endpoint: String? = nil
            // Specify the name of the object whose metadata you want to query.
            let key = "yourObjectName" // Replace the value with the actual object name.
            // Version ID
            let versionId = "versionId"

            // Obtain access credentials from the 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 OSS client parameters.
            let config = Configuration.default()
                .withRegion(region)        // Set the region where the bucket is located.
                .withCredentialsProvider(credentialsProvider)  // Set the access credential provider.
                
            // Use the custom endpoint if provided.
            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }
            
            // Create an OSS Client instance.
            let client = Client(config)

            // Query partial metadata of the specified object version.
            let result = try await client.getObjectMeta(
                GetObjectMetaRequest(
                    bucket: bucket,
                    key: key,
                    versionId: versionId
                )
            )
            print("result:\n\(result)")

        } catch {
            // Error output
            print("error: \(error)")
        }
    }
}

References