Un lien symbolique est un objet spécial qui pointe vers un autre objet. Dans Object Storage Service (OSS), les liens symboliques fonctionnent comme des raccourcis de fichiers sous Windows et permettent d'accéder rapidement aux objets associés.
Notes d'utilisation
Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Pour 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 la section Régions et points de terminaison.
Cette rubrique explique comment créer une instance
OSSClientavec un point de terminaison OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), reportez-vous à la section Créer une instance OssClient.Pour créer un lien symbolique, vous devez disposer de l'autorisation
oss:PutObject. Pour interroger un lien symbolique, vous devez disposer de l'autorisationoss:GetObject. Pour plus d'informations, consultez la section Accorder une stratégie personnalisée.Si vous supprimez l'objet ciblé par un lien symbolique, ce dernier reste présent, mais l'objet n'est plus accessible via ce lien.
Créer un lien symbolique
L'exemple de code suivant montre comment créer un lien symbolique :
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 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. */
std::string Endpoint = "yourEndpoint";
/* Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Specify the full path of the symbolic link. Example: shortcut/myobject.txt. */
std::string LinkName = "shortcut/myobject.txt";
/* Initialize resources, such as network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 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. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Configure the HTTP headers. */
auto meta = ObjectMetaData();
meta.setContentType("text/plain");
/* Configure the user metadata of the object. */
meta.UserMetaData()["meta"] = "meta-value";
/* Create the symbolic link. */
CreateSymlinkRequest request(BucketName, ObjectName, meta);
request.SetSymlinkTarget(LinkName);
auto outcome = client.CreateSymlink(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "CreateSymlink fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release resources, such as network resources. */
ShutdownSdk();
return 0;
}
Interroger un lien symbolique
Pour interroger un lien symbolique, vous devez disposer des autorisations de lecture sur celui-ci. L'exemple de code suivant montre comment interroger le lien symbolique et obtenir le nom de l'objet ciblé :
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 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. */
std::string Endpoint = "yourEndpoint";
/* Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the symbolic link. Example: shortcut/myobject.txt. */
std::string LinkName = "shortcut/myobject.txt";
/* Initialize resources, such as network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 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. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Obtain the name of the object to which the symbolic link points. */
GetSymlinkRequest request(BucketName, LinkName);
auto outcome = client.GetSymlink(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "GetSymlink fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
else {
std::cout << " GetSymlink success Symlink name:" << outcome.result().SymlinkTarget() << std::endl;
}
/* Release resources, such as network resources. */
ShutdownSdk();
return 0;
}
Références
Pour plus d'informations sur l'opération API permettant de créer un lien symbolique, consultez la section PutSymlink.
Pour plus d'informations sur l'opération API permettant d'interroger un lien symbolique, consultez la section GetSymlink.