Manage symlinks (Java SDK V2)
Create and retrieve symlinks in a bucket with versioning enabled by using OSS Java SDK V2.
Prerequisites
OSS Java SDK V2 installed. If you use Maven, add the dependency
com.aliyun:alibabacloud-oss-v2to pom.xml.Access credentials configured by using environment variables. The examples in this topic use
EnvironmentVariableCredentialsProviderto read credentials from the environment variablesOSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRET.
Permissions
An Alibaba Cloud account has full permissions by default. RAM users and RAM roles under an Alibaba Cloud account have no permissions by default. The Alibaba Cloud account or an account administrator must grant the required permissions.
API | Action | Description |
PutSymlink |
| Manage symlinks in a bucket with versioning enabled. |
Sample code
import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;
public class VersioningSymlinkSample {
public static void main(String[] args) throws Exception {
String region = "cn-hangzhou";
String bucket = "examplebucket";
String symlinkKey = "example-symlink";
String targetKey = "exampleobject";
CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
OSSClientBuilder clientBuilder = OSSClient.newBuilder()
.credentialsProvider(provider)
.region(region);
try (OSSClient client = clientBuilder.build()) {
// Create a symlink. A version ID is returned when versioning is enabled for the bucket.
PutSymlinkResult putResult = client.putSymlink(PutSymlinkRequest.newBuilder()
.bucket(bucket)
.key(symlinkKey)
.symlinkTarget(targetKey)
.build());
System.out.printf("PutSymlink status code:%d, version id:%s%n",
putResult.statusCode(), putResult.versionId());
// Get the symlink with a specified version ID.
String symlinkVersionId = putResult.versionId();
GetSymlinkResult getResult = client.getSymlink(GetSymlinkRequest.newBuilder()
.bucket(bucket)
.key(symlinkKey)
.versionId(symlinkVersionId)
.build());
System.out.printf("GetSymlink target: %s, version id: %s%n",
getResult.symlinkTarget(), getResult.versionId());
} catch (Exception e) {
System.out.printf("error:%n%s", e);
}
}
}
References
For the synchronous sample code, see PutSymlink.java.
For the asynchronous sample code, see PutSymlinkAsync.java.
For the synchronous sample code, see GetSymlink.java.
For the asynchronous sample code, see GetSymlinkAsync.java.