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.

Usage notes

  • When you use packaging tools such as Webpack and Browserify, install OSS SDK for Browser.js by running the npm install ali-oss command.
  • OSS SDK for Browser.js is usually used in browsers. To prevent your AccessKey pair from being exposed, we recommend that you use a temporary access credential generated by Security Token Service (STS) to access OSS.

    For more information about how to set up STS, see Use a temporary access credential provided by STS to access OSS in OSS Developer Guide. You can call the AssumeRole operation or use STS SDKs for various programming languages to obtain a temporary access credential. For more information, see STS SDK overview. A temporary access credential contains a security token and a temporary AccessKey pair that consists of an AccessKey ID and an AccessKey secret.

Create a symbolic link

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

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>Document</title>
</head>

<body>
  <button id='upload'>Upload An Object</button>
  <button id='symlink'>Create A Symbolic Link</button>  
  <!-- import an SDK file -->
  <script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.16.0.min.js"></script>
  <script type="text/javascript">
    const client = new OSS({
       // Set yourRegion to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourRegion to oss-cn-hangzhou. 
       region: 'yourRegion',
       // Specify the temporary AccessKey pair obtained from STS. 
       accessKeyId: 'yourAccessKeyId',
       accessKeySecret: 'yourAccessKeySecret',
       // Specify the security token obtained from STS. 
       stsToken: 'yourSecurityToken',
       // Specify the name of the bucket. Example: examplebucket. 
       bucket: "examplebucket",
     });

    const upload = document.getElementById('upload')
    const symlink = document.getElementById('symlink')
    const getSymlink = document.getElementById("getSymlink")

    // Specify the object to upload. 
    const file = new Blob(['examplecontent'])
    // Specify the name of the object after it is uploaded to the bucket. 
    const fileName = 'exampleobject.txt'

    // Upload the object. 
    upload.addEventListener('click', () => {
      client.put(fileName, file).then(r => console.log(r))
    })
    
    // Create a symbolic link named symlink.txt. 
    sylink.addEventListener('click', () => {
      client.putSymlink('symlink.txt', fileName).then(r => console.log(r))
    })

  </script>
</body>

</html>

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

To query the name of the object to which a symbolic link points, 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 the symbolic link points:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>Document</title>
</head>

<body>  
  <button id='symlink'>Query A Symbolic Link</button>  
  <!-- import an SDK file -->
  <script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.16.0.min.js"></script>
  <script type="text/javascript">
    const client = new OSS({
       // Set yourRegion to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourRegion to oss-cn-hangzhou. 
       region: 'yourRegion',
       // Specify the temporary AccessKey pair obtained from STS. 
       accessKeyId: 'yourAccessKeyId',
       accessKeySecret: 'yourAccessKeySecret',
       // Specify the security token obtained from STS. 
       stsToken: 'yourSecurityToken',
       // Specify the name of the bucket. Example: examplebucket. 
       bucket: "examplebucket",
     });
    
    const symlink = document.getElementById('symlink')
    const getSymlink = document.getElementById("getSymlink")

    // Obtain the name of the object to which the symbolic link points. 
    getSylink.addEventListener('click', () => {
      client.getSymlink('symlink.txt').then(r => console.log(r))
    })

  </script>
</body>

</html>

References

  • Create a symbolic link
    • For more information about the complete sample code for creating a symbolic link, visit GitHub.
    • For more information about the API operation that is called to create a symbolic link, see PutSymlink.
  • Obtain the object to which a symbolic link points
    • For more information about the complete sample code that is used to obtain the object to which a symbolic link points, visit GitHub.
    • For more information about the API operation that is called to query the object to which a symbolic link points, see GetSymlink.