This topic describes how to manage symbol links in a bucket with versioning enabled or suspended.

Create a symbol link

You can create a symbol link that directs to the current version of the target object.
Note In a bucket with the versioning function enabled, you cannot create a symbol link for a delete marker.

A symbol link can has multiple versions that direct to different target objects. The version ID of the symbol link is automatically generated and is returned as the x-oss-version-id field in the response header.

You can run the following code to create a symbol link:
// This example uses the China East 1 (Hangzhou) endpoint. Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with the AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM user account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
String symLink = "<yourSymLink>";
String destinationObjectName = "<yourDestinationObjectName>";

// Creates an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

// Initializes the metadata of the object to be uploaded.
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("text/plain");
// Sets the value of property to property-value.
metadata.addUserMetadata("property", "property-value");

// Creates a CreateSymlinkRequest request.
CreateSymlinkRequest createSymlinkRequest = new CreateSymlinkRequest(bucketName, symLink, destinationObjectName);

// Sets the metadata of the symbol link.
createSymlinkRequest.setMetadata(metadata);

// Creates the symbol link.
ossClient.createSymlink(createSymlinkRequest);

// Closes the OSSClient instance.
ossClient.shutdown();

For more information about creating a symbol link, see PutSymlink.

Obtain a symbol link

GetSymlink obtains the current version of the target symbol link by default. You can specify the versionId in the request to obtain the specified version of a symbol link. If the current version of the target symbol link is a delete marker, OSS returns the 404 Not Found error and includes x-oss-delete-marker = true and x-oss-version-id in the response header.
Note You must have the read permission on a symbol link to obtain it.
You can run the following code to obtain a symbol link:
// This example uses the China East 1 (Hangzhou) endpoint. Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with the AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM user account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";

// Creates an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

String symLink = "<yourSymLink>";
String versionid = "<yourSymLinkVersionid>";

// Obtains the content of the specified version of the symbol link.
GenericRequest genericRequest = new GenericRequest(bucketName, symLink, versionid);
OSSSymlink symbolicLink = ossClient.getSymlink(genericRequest);
// Prints the content of the object that the symbol link directs to.
System.out.println(symbolicLink.getSymlink());
System.out.println(symbolicLink.getTarget());
System.out.println(symbolicLink.getRequestId());
// Prints the version ID of the symbol link.
System.out.println(symbolicLink.getMetadata().getVersionId());

// Closes the OSSClient instance.
ossClient.shutdown();

For more information about obtaining a symbol link, see GetSymlink.