建立符號連結
符號連結是一種特殊的檔案,它指向某個具體的檔案,類似於Windows上使用的捷徑。符號連結支援自訂元資訊。
以下代碼用於建立符號連結:
// Endpoint以杭州為例,其它Region請按實際情況填寫。
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號。
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
String symLink = "<yourSymLink>";
String destinationObjectName = "<yourDestinationObjectName>";
// 建立OSSClient執行個體。
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// 建立上傳檔案元資訊。
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("text/plain");
// 設定自訂元資訊property的值為property-value。
metadata.addUserMetadata("property", "property-value");
// 建立CreateSymlinkRequest。
CreateSymlinkRequest createSymlinkRequest = new CreateSymlinkRequest(bucketName, symLink, destinationObjectName);
// 設定元資訊。
createSymlinkRequest.setMetadata(metadata);
// 建立符號連結。
ossClient.createSymlink(createSymlinkRequest);
// 關閉OSSClient。
ossClient.shutdown();
符號連結的詳細資料請參見PutSymlink。
獲取符號連結指向的檔案內容
獲取符號連結要求您對該符號連結有讀許可權。以下代碼用於獲取符號連結指向的檔案內容:
// Endpoint以杭州為例,其它Region請按實際情況填寫。
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號。
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
String symLink = "<yourSymLink>";
// 建立OSSClient執行個體。
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// 獲取符號連結。
OSSSymlink symbolicLink = ossClient.getSymlink(bucketName, symLink);
// 列印符號連結指向的檔案內容。
System.out.println(symbolicLink.getSymlink());
System.out.println(symbolicLink.getTarget());
System.out.println(symbolicLink.getRequestId());
// 關閉OSSClient。
ossClient.shutdown();
符號連結的詳細資料請參見GetSymlink。