Use the Swift SDK to download an object from a versioning-enabled bucket.
Notes
-
The sample code in this topic uses the China (Hangzhou) region ID
cn-hangzhouas an example and a public Endpoint. If you access Object Storage Service (OSS) from another Alibaba Cloud product in the same region, use an internal Endpoint. For more information about the regions and Endpoints that OSS supports, see Regions and Endpoints.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM policies or Bucket Policy.
|
API |
Action |
Description |
|
GetObject |
|
Downloads an object. |
|
|
When downloading an object, if you specify the object version through versionId, this permission is required. |
|
|
|
When downloading an object, if the object metadata contains X-Oss-Server-Side-Encryption: KMS, this permission is required. |
When you call the GetObject operation on a bucket, note the following:
-
If the current version of the object in the bucket is a delete marker, 404 Not Found is returned.
-
If you specify the versionId of the object in the query parameter, the specified object version is returned. If you set versionId to "null", the object version whose versionId is "null" is returned.
-
If you try to obtain a delete marker by specifying its versionId, 405 Method Not Allowed is returned.
Sample code
The following code downloads a file from a versioning-enabled bucket.
import AlibabaCloudOSS
import Foundation
@main
struct Main {
static func main() async {
do {
// Specify the region where the bucket is located. For example, set Region to cn-hangzhou for the China (Hangzhou) region.
let region = "cn-hangzhou"
// Specify the bucket name.
let bucket = "yourBucketName"
// Optional. Specify the domain name used to access OSS. For example, set Endpoint to https://oss-cn-hangzhou.aliyuncs.com for the China (Hangzhou) region.
let endpoint: String? = nil
// Specify the object name. Example: my-object.txt.
let key = "yourKey"
// Specify the path of the local file.
let filePath = "yourFilePath"
// The version ID.
let versionId = "versionId"
// Obtain access credentials from environment variables. Before you run the program, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
let credentialsProvider = EnvironmentCredentialsProvider()
// Configure the OSS client parameters.
let config = Configuration.default()
.withRegion(region) // Set the region.
.withCredentialsProvider(credentialsProvider) // Set the credentials.
// Set the Endpoint.
if let endpoint = endpoint {
config.withEndpoint(endpoint)
}
// Create an OSS client instance.
let client = Client(config)
// Download the object from OSS to a local file.
let result = try await client.getObjectToFile(
GetObjectRequest(
bucket: bucket,
key: key,
versionId: versionId
),
URL(fileURLWithPath: filePath)
)
print("result:\n\(result)")
} catch {
// Stop the program and print the error message.
print("error: \(error)")
}
}
}
References
-
For the API operation used to download files, see GetObject.