Symbolic links can be used to access objects that are frequently used in buckets. After you create a symbolic link for an object, you can use the symbolic link to access the object.

Create a symbolic link

The following code provides an example on how to create a symbolic link:

// Construct a request. 
PutSymlinkRequest putSymlink = new PutSymlinkRequest();
// Specify the name of the bucket. 
putSymlink.setBucketName("yourBucketName");
// Specify the name of the symbolic link. 
putSymlink.setObjectKey("yourSymLink");
// Specify the name of the object to which the symbolic link points. 
putSymlink.setTargetObjectName("yourTargetObjectName");

ObjectMetadata metadata = new ObjectMetadata();
// Specify whether to overwrite the object with the same name. In this example, this parameter is set to true, which specifies that the object with the same name cannot be overwritten. 
//metadata.setHeader("x-oss-forbid-overwrite", "false");
// Specify the access control list (ACL) of the object. In this example, this parameter is set to private. 
//metadata.setHeader("x-oss-object-acl", "private");
// Specify the storage class of the object. In this example, this parameter is set to Standard. 
//metadata.setHeader("x-oss-storage-class", "Standard");
putSymlink.setMetadata(metadata);

OSSAsyncTask task = oss.asyncPutSymlink(putSymlink, new OSSCompletedCallback<PutSymlinkRequest, PutSymlinkResult>() {
    @Override
    public void onSuccess(PutSymlinkRequest request, PutSymlinkResult result) {
        Log.d("PutSymlink", "PutSymlink success");
    }

    @Override
    public void onFailure(PutSymlinkRequest request, ClientException clientException,
                          ServiceException serviceException) {
        // Handle request exceptions. 
        if (clientException != null) {
            // Handle client-side exceptions such as network errors. 
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // Handle server-side exceptions. 
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});
task.waitUntilFinished();

Query the name of the object to which a symbolic link points

To query a symbolic link, 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 points:

// Construct a request. 
GetSymlinkRequest getSymlink = new GetSymlinkRequest();
// Specify the name of the bucket. 
getSymlink.setBucketName("yourBucketName");
// Specify the name of the symbolic link that you want to query. 
getSymlink.setObjectKey("yourSymLink");

OSSAsyncTask task = oss.asyncGetSymlink(getSymlink, new OSSCompletedCallback<GetSymlinkRequest,
        GetSymlinkResult>() {
    @Override
    public void onSuccess(GetSymlinkRequest request, GetSymlinkResult result) {
        OSSLog.logInfo("targ::"+result.getTargetObjectName());

    }

    @Override
    public void onFailure(GetSymlinkRequest request, ClientException clientException,
                          ServiceException serviceException) {
        OSSLog.logError("error: "+serviceException.getRawMessage());

    }
});
task.waitUntilFinished();

References

  • For more information about the complete sample code of symbolic links, visit GitHub.
  • 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.