Append upload (Swift SDK)
You can append content to existing appendable objects. This topic describes how to perform append upload using Object Storage Service (OSS) SDK for Swift.
Notes
-
The sample code in this topic uses the China (Hangzhou) region ID
cn-hangzhouas an example. By default, a public Endpoint is used. If you want to access OSS from other Alibaba Cloud products in the same region, you can use an internal Endpoint. For more information about the mappings between OSS-supported regions and Endpoints, see Regions and Endpoints. -
If the object to which you want to append content does not exist, an appendable object is created when you call this operation.
-
If the file already exists:
-
If the object is an appendable object and the specified position from which the append operation starts is equal to its current length, the content is appended to the end of the object.
-
If the object is an appendable object and the specified position from which the append operation starts is not equal to the current length of this object, the PositionNotEqualToLength error is returned.
-
If the object is not an appendable object, the ObjectNotAppendable error is returned.
-
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 |
|
AppendObject |
|
You can call this operation to upload an object by appending the object to an existing object. |
|
|
When uploading an object by appending the object to an existing object, if you specify object tags through x-oss-tagging, this permission is required. |
Sample code
Below is the sample code for append upload.
import AlibabaCloudOSS
import Foundation
@main
struct Main {
static func main() async {
do {
// Specify the region in which the bucket is located. For example, if the bucket is located 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 endpoint used to access OSS. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
let endpoint: String? = nil
// Specify the object name. Example: my-object.txt.
let key = "yourKey"
// Load access credentials from environment variables (the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables must be configured in advance).
let credentialsProvider = EnvironmentCredentialsProvider()
// Configure parameters for the OSSClient instance.
let config = Configuration.default()
.withRegion(region) // Specify the region.
.withCredentialsProvider(credentialsProvider) // Specify the credential.
.withUploadCRC64Validation(false) // (Optional) Disable CRC-64.
// Specify the endpoint.
if let endpoint = endpoint {
config.withEndpoint(endpoint)
}
// Create an OSSClient instance.
let client = Client(config)
// Prepare the content for multipart upload.
let content1 = "Hello"
let content2 = ", OSS!"
// The position from which the first append operation starts is 0.
var result = try await client.appendObject(
AppendObjectRequest(
bucket: bucket,
key: key,
position: 0,
body: .data(content1.data(using: .utf8)!)
)
)
print("result:\n\(result)")
// The second append operation starts from the position where the first operation ends.
result = try await client.appendObject(
AppendObjectRequest(
bucket: bucket,
key: key,
position: result.nextAppendPosition,
body: .data(content2.data(using: .utf8)!)
)
)
print("result:\n\(result)")
} catch {
// Capture and handle errors.
print("error:\n\(error)")
}
}
}
Seal an appendable object
Call sealAppendObject to prevent further appends and make an appendable object non-appendable. After the object is sealed, you can use lifecycle rules to transition the object to Cold Archive or Deep Cold Archive. Add the following code after the second append operation in the preceding sample. For more information, see SealAppendObject.
if let position = result.nextAppendPosition {
let sealResult = try await client.sealAppendObject(
SealAppendObjectRequest(
bucket: bucket,
key: key,
position: position
)
)
print("seal result:\n\(sealResult)")
}
References
-
For the complete sample code used to perform append upload, visit GitHub.
-
For more information about the API operation that you can call to perform append upload, visit AppendObject.