Get object metadata (Swift SDK)

更新时间:
复制 MD 格式

Use the OSS Swift SDK to retrieve metadata for a specific version of an object in a versioning-enabled bucket.

Choose a method

Two SDK methods query object metadata, each returning a different set of fields:

MethodReturnsUse when
headObjectAll metadata headers, including system-defined and user-defined metadataYou need the full metadata of an object
getObjectMetaContentLength, ETag, LastModified, LastAccessTime, VersionId, HashCRC64You need a lightweight check — no object body is returned

Both methods accept a versionId parameter to target a specific object version.

Prerequisites

Before you begin, make sure you have:

  • A versioning-enabled OSS bucket

  • The oss:GetObject permission on the target object. For setup instructions, see Attach a custom policy to a RAM user

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables set with your access credentials

Query all metadata with headObject

headObject sends a HEAD request and returns all metadata headers for the specified object version — without downloading the object body.

import AlibabaCloudOSS
import Foundation

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

        do {
            // Region where the bucket is located. Example: cn-hangzhou.
            let region = "cn-hangzhou"
            // Bucket name.
            let bucket = "yourBucketName"
            // (Optional) Custom endpoint. Example: https://oss-cn-hangzhou.aliyuncs.com.
            // Leave as nil to use the default public endpoint for the region.
            let endpoint: String? = nil
            // Object name.
            let key = "yourObjectName"
            // Version ID of the object version to query.
            let versionId = "versionId"

            // Load access credentials from environment variables.
            let credentialsProvider = EnvironmentCredentialsProvider()

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

            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }

            let client = Client(config)

            // Query all metadata headers for the specified object version.
            let result = try await client.headObject(
                HeadObjectRequest(
                    bucket: bucket,
                    key: key,
                    versionId: versionId
                )
            )
            print("result:\n\(result)")

        } catch {
            print("error: \(error)")
        }
    }
}
If your application runs on another Alibaba Cloud service in the same region as the bucket, use the internal endpoint (for example, https://oss-cn-hangzhou-internal.aliyuncs.com) to avoid cross-region traffic charges. For a full list of regions and endpoints, see Regions and endpoints.

Query partial metadata with getObjectMeta

getObjectMeta is a lightweight alternative that returns only six fields: ContentLength, ETag, LastModified, LastAccessTime, VersionId, and HashCRC64 (64-bit CRC value). It does not return user-defined metadata or other system headers.

import AlibabaCloudOSS
import Foundation

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

        do {
            // Region where the bucket is located. Example: cn-hangzhou.
            let region = "cn-hangzhou"
            // Bucket name.
            let bucket = "yourBucketName"
            // (Optional) Custom endpoint. Example: https://oss-cn-hangzhou.aliyuncs.com.
            // Leave as nil to use the default public endpoint for the region.
            let endpoint: String? = nil
            // Object name.
            let key = "yourObjectName"
            // Version ID of the object version to query.
            let versionId = "versionId"

            // Load access credentials from environment variables.
            let credentialsProvider = EnvironmentCredentialsProvider()

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

            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }

            let client = Client(config)

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

        } catch {
            print("error: \(error)")
        }
    }
}

API reference