All Products
Search
Document Center

Object Storage Service:Manage symbolic links

Last Updated:Oct 07, 2023

Symbolic links work like file shortcuts on Windows and allow you to quickly access associated objects in Object Storage Service (OSS).

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.

  • If you want to access an OSS bucket from a browser but no CORS rules are configured for the bucket, the browser rejects the request. Therefore, you must configure CORS rules for a bucket if you want to access the bucket from a browser. For more information, see Installation.

  • In most cases, OSS SDK for Browser.js is used in browsers. To prevent your AccessKey pair from being exposed, we recommend that you use temporary access credentials obtained from Security Token Service (STS) to access OSS.

    The temporary access credentials consist of an AccessKey pair and a security token. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. For more information about how to obtain temporary access credentials, see Use STS for temporary access authorization.

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</button>
  <button id='symlink'>Create Symbolic Link</button>  
  <!-- Import the SDK file -->
  <script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.18.0.min.js"></script>
  <script type="text/javascript">
    const client = new OSS({
       // Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
       region: 'yourRegion',
       // Specify the temporary AccessKey pair obtained from STS. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. 
       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 to upload to the bucket. 
    const fileName = 'exampleobject.txt'

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

  </script>
</body>

</html>

Query the name of the object to which the 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 the symbolic link points:

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

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

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

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

  </script>
</body>

</html>

References

  • Create a symbolic link

    • For the complete sample code that is used to create a symbolic link, visit GitHub.

    • For more information about the API operation that you can call to create a symbolic link, see PutSymlink.

  • Query the object to which a symbolic link points

    • For the complete sample code that is used to query the object to which a symbolic link points, visit GitHub.

    • For more information about the API operation that you can call to query the object to which a symbolic link points, see GetSymlink.