Java管理軟鏈接
軟鏈接功能用于便捷訪問(wèn)Bucket內(nèi)的常用Object。設(shè)置軟鏈接后,您可以使用類(lèi)似于Windows的快捷方式,通過(guò)軟鏈接文件打開(kāi)Object。
注意事項(xiàng)
本文以華東1(杭州)外網(wǎng)Endpoint為例。如果您希望通過(guò)與OSS同地域的其他阿里云產(chǎn)品訪問(wèn)OSS,請(qǐng)使用內(nèi)網(wǎng)Endpoint。關(guān)于OSS支持的Region與Endpoint的對(duì)應(yīng)關(guān)系,請(qǐng)參見(jiàn)OSS訪問(wèn)域名、數(shù)據(jù)中心、開(kāi)放端口。
本文以從環(huán)境變量讀取訪問(wèn)憑證為例。如何配置訪問(wèn)憑證,請(qǐng)參見(jiàn)Java配置訪問(wèn)憑證。
本文以OSS域名新建OSSClient為例。如果您希望通過(guò)自定義域名、STS等方式新建OSSClient,請(qǐng)參見(jiàn)新建OSSClient。
要?jiǎng)?chuàng)建軟鏈接,您必須具有
oss:PutObject
權(quán)限;要獲取軟鏈接,您必須具有oss:GetObject
權(quán)限。具體操作,請(qǐng)參見(jiàn)為RAM用戶授權(quán)自定義的權(quán)限策略。
創(chuàng)建軟鏈接
以下代碼用于創(chuàng)建軟鏈接:
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請(qǐng)按實(shí)際情況填寫(xiě)。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環(huán)境變量中獲取訪問(wèn)憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫(xiě)B(tài)ucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 指定軟鏈接名稱。
String symLink = "yourSymLink";
// 指定軟鏈接指向的目標(biāo)Object名稱。
String destinationObjectName = "yourDestinationObjectName";
// 填寫(xiě)B(tài)ucket所在地域。以華東1(杭州)為例,Region填寫(xiě)為cn-hangzhou。
String region = "cn-hangzhou";
// 創(chuàng)建OSSClient實(shí)例。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// 創(chuàng)建上傳文件元數(shù)據(jù)。
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("text/plain");
// 設(shè)置自定義元數(shù)據(jù)property的值為property-value。
metadata.addUserMetadata("property", "property-value");
// 指定創(chuàng)建軟鏈接時(shí)是否覆蓋同名Object。
// metadata.setHeader("x-oss-forbid-overwrite", "true");
// 指定Object的訪問(wèn)權(quán)限。
// metadata.setHeader(OSSHeaders.OSS_OBJECT_ACL, CannedAccessControlList.Default);
// 指定Object的存儲(chǔ)類(lèi)型。
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard);
// 創(chuàng)建CreateSymlinkRequest。
CreateSymlinkRequest createSymlinkRequest = new CreateSymlinkRequest(bucketName, symLink, destinationObjectName);
// 設(shè)置元數(shù)據(jù)。
createSymlinkRequest.setMetadata(metadata);
// 創(chuàng)建軟鏈接。
ossClient.createSymlink(createSymlinkRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
獲取軟鏈接
獲取軟鏈接要求您對(duì)該軟鏈接有讀權(quán)限。以下代碼用于獲取軟鏈接以及軟鏈接指向的目標(biāo)文件名稱:
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請(qǐng)按實(shí)際情況填寫(xiě)。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環(huán)境變量中獲取訪問(wèn)憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫(xiě)B(tài)ucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫(xiě)軟鏈接名稱。
String symLink = "yourSymLink";
// 填寫(xiě)B(tài)ucket所在地域。以華東1(杭州)為例,Region填寫(xiě)為cn-hangzhou。
String region = "cn-hangzhou";
// 創(chuàng)建OSSClient實(shí)例。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// 獲取軟鏈接以及軟鏈接指向的目標(biāo)文件名稱。
OSSSymlink symbolicLink = ossClient.getSymlink(bucketName, symLink);
System.out.println(symbolicLink.getSymlink());
System.out.println(symbolicLink.getTarget());
System.out.println(symbolicLink.getRequestId());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
相關(guān)文檔
關(guān)于創(chuàng)建軟鏈接的API接口說(shuō)明,請(qǐng)參見(jiàn)PutSymlink。
關(guān)于獲取軟鏈接的API接口說(shuō)明,請(qǐng)參見(jiàn)GetSymlink。