Object Storage Service (OSS) のシンボリックリンクは、Windowsのファイルショートカットのように機能し、関連するオブジェクトにすばやくアクセスできます。
使用上の注意
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョンとエンドポイント」をご参照ください。
このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSecurity Token Service (STS) を使用してOSSClientを作成する場合は、「OSSClientインスタンスの作成」をご参照ください。
シンボリックリンクを作成するには、
oss:PutObject
権限が必要です。 シンボリックリンクをクエリするには、oss:GetObject
権限が必要です。 詳細については、「RAMユーザーへのカスタムポリシーのアタッチ」をご参照ください。
シンボリックリンクの作成
シンボリックリンクには複数のバージョンがあります。 バージョン管理が有効なバケットにシンボリックリンクを作成すると、シンボリックリンクのバージョンIDがOSSによって自動的に生成され、レスポンスでx-oss-version-idヘッダーとして返されます。 オブジェクトの現在のバージョンが指すシンボリックリンクを作成できます。
バージョン管理が有効なバケットでは、削除マーカーのシンボリックリンクを作成できません。
次のコードは、シンボリックリンクを作成する方法の例を示しています。
#include <alibabacloud/oss/OssClient.h>
#include <alibabacloud/oss/model/ObjectMetaData.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* 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. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Specify the name of the symbolic link that you want to create. */
std::string LinkName = "yourLinkName";
/* 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 HTTP headers. */
auto meta = ObjectMetaData();
meta.setContentType("text/plain");
/* Configure user metadata. */
meta.UserMetaData()["meta"] = "meta-value";
/* Create the symbolic link. */
CreateSymlinkRequest request(BucketName, ObjectName, meta);
request.SetSymlinkTarget(LinkName);
auto outcome = client.CreateSymlink(request);
/* Display the version ID of the uploaded symbolic link. */
if (outcome.isSuccess()) {
std::cout << "versionid:" << outcome.result().VersionId() << std::endl;
}
else {
/* 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;
}
シンボリックリンクの照会
デフォルトでは、GetSymlink操作はシンボリックリンクの現在のバージョンを取得します。 リクエストでバージョンIDを指定して、指定したバージョンのシンボリックリンクを照会できます。 シンボリックリンクの現在のバージョンが削除マーカーの場合、OSSは404 Not Foundを返し、レスポンスにx-oss-delete-marker = trueとx-oss-version-idを含めます。
シンボリックリンクを照会するには、シンボリックリンクの読み取り権限が必要です。
次のコードは、シンボリックリンクを照会する方法の例を示しています。
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* 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 name of the symbolic link that you want to query. */
std::string LinkName = "yourLinkName";
/* 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);
/* Query the symbolic link of the specified version. */
GetSymlinkRequest request(BucketName, LinkName);
request.setVersionId("yourObjectVersionId");
auto outcome = client.GetSymlink(request);
/* Display the version ID of the returned symbolic link. */
if (outcome.isSuccess()) {
std::cout << "versionid:" << outcome.result().VersionId()
<< "Symlink name:" << outcome.result().SymlinkTarget() << std::endl;
}
else {
/* Handle exceptions. */
std::cout << "GetSymlink 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;
}
関連ドキュメント
シンボリックリンクを作成するために呼び出すことができるAPI操作の詳細については、「PutSymlink」をご参照ください。
シンボリックリンクを照会するために呼び出すことができるAPI操作の詳細については、「GetSymlink」をご参照ください。