Todos los productos
Search
Centro de documentos

Object Storage Service:Gestión de enlaces simbólicos (OSS SDK for Python 1.0)

Última actualización: Sep 22, 2026

Los enlaces simbólicos funcionan como los accesos directos de archivos en Windows y permiten acceder rápidamente a los objetos asociados en Object Storage Service (OSS).

Notas

  • En este tema se utiliza el endpoint público de la región China (Hangzhou). Si necesita acceder a OSS desde otros servicios de Alibaba Cloud ubicados en la misma región que OSS, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.

  • En este tema, las credenciales de acceso se obtienen de variables de entorno. Para obtener más información sobre cómo configurar las credenciales de acceso, consulte Configure access credentials using OSS SDK for Python 1.0.

  • Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para conocer configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Initialization.

  • Para crear un enlace simbólico, debe contar con el permiso oss:PutObject. Para consultar un enlace simbólico, requiere el permiso oss:GetObject. Para obtener más información, consulte Grant a custom policy.

Crear un enlace simbólico

A continuación se muestra un ejemplo de código para crear un enlace simbólico:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the name of the object to which you want the symbolic link to point. Example: exampleobject.txt. // For more information about the naming conventions for objects, see Object naming conventions. 
object_name = 'exampleobject.txt'
# Specify the name of the symbolic link. Example: examplesymlick.txt. 
symlink = 'examplesymlick.txt'

# headers = dict()
# Specify whether to overwrite the object that has the same name as the symbolic link. In this example, x-oss-forbid-overwrite is set to true. This indicates that the object that has the same name cannot be overwritten. 
# headers['x-oss-forbid-overwrite'] = 'true'
# Specify the access control list (ACL) of the object. In this example, this parameter is set to OBJECT_ACL_PRIVATE, which indicates that the ACL of the object is private. Only the owner of the object and authorized users have read and write permissions on the object. 
# headers[OSS_OBJECT_ACL] = oss2.OBJECT_ACL_PRIVATE
# Specify the storage class of the object. In this example, the storage class is set to Standard. 
# headers['x-oss-storage-class'] = oss2.BUCKET_STORAGE_CLASS_STANDARD
# bucket.put_symlink(object_name, symlink, headers=headers)
# Create the symbolic link. 
bucket.put_symlink(object_name, symlink)           

Consultar el nombre del objeto al que apunta un enlace simbólico

Para consultar un enlace simbólico, debe tener permisos de lectura sobre dicho enlace. A continuación se muestra un ejemplo de código para consultar un enlace simbólico y el nombre del objeto al que apunta:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the name of the symbolic link. 
symlink = 'examplesymlick.txt'
# Query the symbolic link and the name of the object to which the symbolic link points. 
bucket.get_symlink(symlink)
result = bucket.get_symlink(symlink)        
print(result.target_key)          

Referencias

  • Para obtener más información sobre la operación de API que permite crear un enlace simbólico, consulte PutSymlink.

  • Para obtener más información sobre la operación de API que permite consultar un enlace simbólico, consulte GetSymlink.