All Products
Search
Document Center

Object Storage Service:Manage symbolic links

Last Updated:Feb 21, 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 sample code provides an example on how to create a symbolic link:

// Create 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 you want the symbolic link to point. 
putSymlink.setTargetObjectName("yourTargetObjectName");

ObjectMetadata metadata = new ObjectMetadata();
// Specify whether to overwrite the object that has the same name. In this example, this parameter is set to true, which indicates that the object that has the same name cannot be overwritten. 
//metadata.setHeader("x-oss-forbid-overwrite", "true");
// Specify the access control list (ACL) of the object. In this example, the ACL is set to private. 
//metadata.setHeader("x-oss-object-acl", "private");
// Specify the storage class of the object. In this example, the storage class 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 exceptions, such as network exceptions. 
            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 sample code provides an example on how to query the name of the object to which a symbolic link points:

// Create a request. 
GetSymlinkRequest getSymlink = new GetSymlinkRequest();
// Specify the name of the bucket. 
getSymlink.setBucketName("yourBucketName");
// Specify the name of the symbolic link. 
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 the complete sample code that is used to manage 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.

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