All Products
Search
Document Center

Object Storage Service:Manage symbolic links by using OSS SDK for iOS

Last Updated:Aug 20, 2024

Symbolic links work like file shortcuts on Windows and allow you to quickly access associated objects in Object Storage Service (OSS).

Usage notes

  • Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.

Create a symbolic link

The following code provides an example on how to create a symbolic link named examplesymlink that points to an object named exampleobject.txt in a bucket named examplebucket:

OSSPutSymlinkRequest *request = [OSSPutSymlinkRequest new];
// Specify the name of the bucket. Example: examplebucket. 
request.bucketName = @"examplebucket";
// Specify the name of the symbolic link. 
request.objectKey = @"examplesymlink";
// Specify the full path of the object to which you want the symbolic link to point. Do not include the bucket name in the full path. 
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;
}];
// Implement synchronous blocking to wait for the task to complete. 
// [putSymlinkTask waitUntilFinished];

Query a symbolic link

To query the object to which a symbolic link points, you must have read permissions on the symbolic link.

The following code provides an example on how to query the name of the object to which a symbolic link named examplesymlink points in the examplebucket bucket:

OSSGetSymlinkRequest *request = [OSSGetSymlinkRequest new];
// Specify the name of the bucket. Example: examplebucket. 
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;
        NSLog(@"get symlink: %@", result.httpResponseHeaderFields[@"x-oss-symlink-target"]);
    } else {
        NSLog(@"get symlink failed, error: %@", task.error);
    }
    return nil;
}];
// Implement synchronous blocking to wait for the task to complete. 
// [putSymlinkTask waitUntilFinished];

References

  • For more information about the API operation that you can call to create a symbolic link, see PutSymlink.

  • For more information about the API operation that you can call to query a symbolic link, see GetSymlink.

  • For more information about how to initialize an OSSClient instance, see Initialization.