Tous les produits
Search
Centre de documentation

Object Storage Service:Gérer les liens symboliques (OSS SDK for Python 1.0)

Dernière mise à jour :Sep 22, 2026

Les liens symboliques fonctionnent comme des raccourcis de fichiers sous Windows et vous permettent d'accéder rapidement aux objets associés dans Object Storage Service (OSS).

Notes

  • Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez Régions et points de terminaison.

  • Dans cette rubrique, les identifiants d'accès sont obtenus à partir des variables d'environnement. Pour savoir comment configurer les identifiants d'accès, consultez Configurer les identifiants d'accès à l'aide d'OSS SDK for Python 1.0.

  • Cette rubrique explique comment créer une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants Security Token Service (STS), consultez Initialisation.

  • Pour créer un lien symbolique, vous devez disposer de l'autorisation oss:PutObject. Pour interroger un lien symbolique, vous devez disposer de l'autorisation oss:GetObject. Pour plus d'informations, consultez Accorder une politique personnalisée.

Créer un lien symbolique

L'exemple de code suivant montre comment créer un lien symbolique :

# -*- 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)           

Interroger le nom de l'objet ciblé par un lien symbolique

Pour interroger un lien symbolique, vous devez disposer des autorisations de lecture sur ce lien. L'exemple de code suivant montre comment interroger un lien symbolique et obtenir le nom de l'objet qu'il cible :

# -*- 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)          

Références

  • Pour plus d'informations sur l'opération API permettant de créer un lien symbolique, consultez PutSymlink.

  • Pour plus d'informations sur l'opération API permettant d'interroger un lien symbolique, consultez GetSymlink.