This topic provides a guide for integrating and using the Alibaba Cloud Drive and Photo Service (PDS) SDK for iOS.
Integration guide
Integrate the SDK
If you use CocoaPods to integrate PDS SDK, simply add pod 'PDS_SDK' to the Podfile, then run the command pod update to complete the SDK integration.
Initialize the SDK
Before you initialize the SDK, make sure that PDS is activated, and a domain is created in the PDS console. For more information, see Get started with PDS.
The following sample code provides an example on how to initialize the SDK:
PDSClientConfig *clientConfig = [[PDSClientConfig alloc] init];
clientConfig.userAgent = config.userAgent;//The user agent of the current device
clientConfig.dbName = config.dbName;//Optional. The name of the database
clientConfig.host = config.apiHost;//The API Host. You can obtain it from the console
[PDSClientManager setupWithAccessToken:config.accessToken clientConfig:clientConfig];//The access token is obtained by the server through accessKeyId and accessKeySecret and then sent to the clientAccess Token refresh:
Note that in the current version, the SDK does not handle Access Token refresh. The SDK user needs to periodically request Access Token refresh from their server and pass it to the SDK.
[[PDSClientManager defaultClient] setAccessToken:accessToken];Upload files
PDSDownloadUrlRequest *request = [[PDSDownloadUrlRequest alloc] initWithDownloadUrl:taskInfo.downloadUrl
destination:filePath
fileSize:[taskInfo.fileSize unsignedLongLongValue]
fileID:taskInfo.fileId
hashValue:taskInfo.hashValue
hashType:PDSFileHashTypeCrc64
driveID:taskInfo.driveId
shareID:taskInfo.shareId];
downloadTask = [[PDSClientManager defaultClient].file downloadUrl:request taskIdentifier:taskInfo.taskId];
Upload files based on image IDs
PDSUploadPhotoRequest *request = [[PDSUploadPhotoRequest alloc] initWithLocalIdentifier:localIdentifier
parentFileID:taskInfo.parentId
driveID:taskInfo.driveId
shareID:taskInfo.shareId
fileName:taskInfo.fileName];
uploadTask = [[PDSClientManager defaultClient].file uploadPhotoAsset:request taskIdentifier:taskInfo.taskId];
Note that the uploadTask created here is a local variable. You must maintain your own reference to it. Otherwise, callbacks will have issues after this object is released.
Callback for task status and progress:
@weakify(self);
[uploadTask setResponseBlock:^(PDSFileMetadata *result, PDSRequestError *requestError, NSString *taskIdentifier) {
@strongify(self);
if (requestError) {//Upload failed
//TODO Error handling
} else {//Upload successful, remove temporary file
[[PDSClientManager defaultClient].file cleanUploadTaskWithTaskIdentifier:taskIdentifier force:YES];
}
} queue:self.transferQueue];
[uploadTask setProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
@strongify(self);
//TODO Progress callback
} queue:self.transferQueue];Note that after the upload is completed or fails, if you confirm that you will not continue operations such as resumable upload on this file, call the cleanUploadTaskWithTaskIdentifer method to delete the cache related to this upload task.
Download files
PDSDownloadTask *downloadTask = nil;
PDSDownloadUrlRequest *request = [[PDSDownloadUrlRequest alloc] initWithDownloadUrl:taskInfo.downloadUrl
destination:filePath
fileSize:[taskInfo.fileSize unsignedLongLongValue]
fileID:taskInfo.fileId
hashValue:taskInfo.hashValue
hashType:PDSFileHashTypeCrc64
driveID:taskInfo.driveId
shareID:taskInfo.shareId];
downloadTask = [[PDSClientManager defaultClient].file downloadUrl:request taskIdentifier:taskInfo.taskId];Callback for task progress and status:
@weakify(self);
[downloadTask setResponseBlock:^(PDSFileMetadata *_Nullable result, PDSRequestError *_Nullable requestError, NSString *_Nonnull taskIdentifier) {
@strongify(self);
if (result) {//Download successful
}
if (requestError) {//Download failed
}
} queue:self.transferQueue];
[downloadTask setProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
@strongify(self);
//Progress callback
} queue:self.transferQueue];
API operations on files
Call the common file interfaces through the class methods under [PDSClientManager defaultClient].file. The currently supported file operations are as follows:
Complete
Create
Move
Delete
Copy
Update
GetDownloadUrl
GetUploadUrl
GetAsyncTask
For detailed interface parameter descriptions, see File management.
Open source code
Release notes
0.0.2 2022-01-24
Added the ability to directly upload local photo assets.
Added file interface API.
Fixed resumable upload and download.
Fixed the issue where CRC64 verification fails for some files.
Fixed the issue where some server error codes were not properly handled.
0.0.1 2021-12-01
The first version, supporting file upload/download and basic file operations.