Upload a file (Swift SDK)

更新时间:
复制 MD 格式

Upload objects to OSS from a Swift application using the PutObject API.

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket

  • The oss:PutObject permission. For setup instructions, see Grant custom policies to RAM users

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables set with your credentials

Usage notes

  • The sample code uses cn-hangzhou as the region. Replace it with the region where your bucket is located. For region IDs and endpoint mappings, see Regions and endpoints.

  • By default, the public endpoint is used. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead.

Simple upload

The following code uploads an object from in-memory data.

import AlibabaCloudOSS
import Foundation

@main
struct Main {
    static func main() async {
        do {
            // Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
            let region = "cn-hangzhou"
            // Specify the bucket name.
            let bucket = "yourBucketName"
            // Optional. Specify the domain name used to access OSS. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
            let endpoint: String? = nil
            // Specify the object name, for example, my-object.txt.
            let key = "yourKey"

            // Load 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 the OSS client.
            let config = Configuration.default()
                .withRegion(region) // Set the region.
                .withCredentialsProvider(credentialsProvider) // Set the credentials provider.

            // Set the endpoint.
            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }

            // Create an OSS client instance.
            let client = Client(config)

            // Prepare the content to upload.
            let content = "Hello, OSS!"

            // Upload the object.
            let result = try await client.putObject(
                PutObjectRequest(
                    bucket: bucket,
                    key: key,
                    body: .data(content.data(using: .utf8)!)
                )
            )

            // Print the result.
            print("result:\n\(result)")

        } catch {
            // Catch and handle exceptions.
            print("error:\n\(error)")
        }
    }
}

Versioning behavior

The upload behavior depends on whether versioning is enabled on the bucket.

Bucket stateVersion IDSame-name object behavior
Versioning enabledUnique version ID, returned in the x-oss-version-id response headerBoth versions are retained
Versioning suspended"null"New object overwrites the previous one; only one version can have a "null" version ID

What's next