You can create symbolic links only by using OSS SDK for Java. The version of OSS SDK for Java must be 3.15.0 or later. The following sample code provides an example on how to create a symbolic link for an object by using OSS SDK for Java:
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
public class Demo {
public static void main(String[] args) throws Exception {
// Specify the data endpoint of the OSS on CloudBox bucket.
String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the OSS on CloudBox bucket. Example: examplebucket.
String bucketName = "examplebucket";
// Specify the region in which the OSS on CloudBox bucket is located.
String region = "cn-hangzhou";
// Specify the ID of the cloud box.
String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";
// Specify the name of the symbolic link that you want to create.
String symLink = "yourSymLink";
// Specify the name of the object to which you want the symbolic link to point.
String destinationObjectName = "yourDestinationObjectName";
// Create an OSSClient instance.
ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
conf.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(new DefaultCredentialProvider(credentialsProvider.getCredentials()))
.clientConfiguration(conf)
.region(region)
.cloudBoxId(cloudBoxId)
.build();
try {
// Specify metadata for the symbolic link.
// ObjectMetadata metadata = new ObjectMetadata();
// metadata.setContentType("text/plain");
// Set the value property to property-value.
// metadata.addUserMetadata("property", "property-value");
// Specify whether to overwrite the existing object that has the same name when you create a symbolic link.
// metadata.setHeader("x-oss-forbid-overwrite", "true");
// Specify the access control list (ACL) of the object.
// metadata.setHeader(OSSHeaders.OSS_OBJECT_ACL, CannedAccessControlList.Default);
// Specify the storage class of the object.
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard);
// Create CreateSymlinkRequest.
CreateSymlinkRequest createSymlinkRequest = new CreateSymlinkRequest(bucketName, symLink, destinationObjectName);
// Specify metadata.
// createSymlinkRequest.setMetadata(metadata);
// Create the symbolic link.
ossClient.createSymlink(createSymlinkRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}