Symbolic links work like file shortcuts on Windows and let you quickly access associated objects in Object Storage Service (OSS).
Prerequisites
Before you begin, ensure that you have:
An initialized
OSSClientinstance using a custom domain name or Security Token Service (STS). For details, see Initialization (iOS SDK)
Create a symbolic link
Call putSymlink: with an OSSPutSymlinkRequest that specifies the bucket name, the symbolic link name, and the full path of the target object (excluding the bucket name). On success, OSS creates the symbolic link and associates it with the target object.
The following code:
Builds an
OSSPutSymlinkRequestwith the bucket name (examplebucket), the symbolic link name (examplesymlink), and the target object path (exampleobject.txt).Calls
[client putSymlink:request]to submit the request, which returns anOSSTask.Handles success and error in a continuation block.
OSSPutSymlinkRequest *request = [OSSPutSymlinkRequest new];
// Specify the bucket name.
request.bucketName = @"examplebucket";
// Specify the name of the symbolic link.
request.objectKey = @"examplesymlink";
// Specify the full path of the target object. Do not include the bucket name.
request.targetObjectName = @"exampleobject.txt";
OSSTask *putSymlinkTask = [client putSymlink:request];
[putSymlinkTask continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
if (!task.error) {
NSLog(@"put symlink success");
} else {
NSLog(@"put symlink failed, error: %@", task.error);
}
return nil;
}];
// Optional: block the current thread until the task completes.
// [putSymlinkTask waitUntilFinished];Get a symbolic link
Read permissions on the symbolic link are required to retrieve it.
Call getSymlink: with an OSSGetSymlinkRequest to retrieve the name of the object that a symbolic link points to. OSS returns the target object name in the x-oss-symlink-target HTTP response header.
The following code:
Builds an
OSSGetSymlinkRequestwith the bucket name (examplebucket) and the symbolic link name (examplesymlink).Calls
[client getSymlink:request]to submit the request, which returns anOSSTask.Reads the target object name from
result.httpResponseHeaderFields[@"x-oss-symlink-target"]in a continuation block.
OSSGetSymlinkRequest *request = [OSSGetSymlinkRequest new];
// Specify the bucket name.
request.bucketName = @"examplebucket";
// Specify the name of the symbolic link.
request.objectKey = @"examplesymlink";
OSSTask *getSymlinkTask = [client getSymlink:request];
[getSymlinkTask continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
if (!task.error) {
OSSGetSymlinkResult *result = task.result;
// The target object name is returned in the x-oss-symlink-target response header.
NSLog(@"target object: %@", result.httpResponseHeaderFields[@"x-oss-symlink-target"]);
} else {
NSLog(@"get symlink failed, error: %@", task.error);
}
return nil;
}];
// Optional: block the current thread until the task completes.
// [getSymlinkTask waitUntilFinished];References
For the API operation to create a symbolic link, see PutSymlink.
For the API operation to retrieve a symbolic link, see GetSymlink.
For OSSClient initialization options, see Initialization (iOS SDK).