Um link simbólico é um objeto especial que aponta para outro objeto. Os links simbólicos no Object Storage Service (OSS) funcionam de forma semelhante aos atalhos de arquivos no Windows e permitem acesso rápido aos objetos associados.
Notas de uso
Neste tópico, utiliza-se o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços Alibaba Cloud na mesma região, use um endpoint interno. Para mais informações sobre regiões e endpoints do OSS, consulte Regions and endpoints.
Este tópico demonstra como criar uma instância OSSClient usando um endpoint do OSS. Para configurações alternativas, como usar um domínio personalizado ou autenticar com credenciais do Security Token Service (STS), consulte Create an OssClient instance.
A criação de um link simbólico exige a permissão
oss:PutObject. A consulta de um link simbólico exige a permissãooss:GetObject. Para mais informações, consulte Grant a custom policy.Após excluir o objeto para o qual um link simbólico aponta, o link simbólico continua existindo, mas não é mais possível acessar o objeto por meio dele.
Criar um link simbólico
O código de exemplo a seguir demonstra como criar um link simbólico:
#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;
}
Consultar um link simbólico
Para consultar um link simbólico, você precisa ter permissões de leitura sobre ele. O código de exemplo a seguir mostra como consultar o link simbólico e obter o nome do objeto para o qual ele aponta:
#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;
}
Referências
Para mais informações sobre a operação de API para criar um link simbólico, consulte PutSymlink.
Para obter detalhes sobre a operação de API para consultar um link simbólico, consulte GetSymlink.