Paket ilkesi, anonim kullanıcıların veya Alibaba Cloud hesapları, RAM kullanıcıları ve RAM rolleri gibi belirli kimliklerin Object Storage Service (OSS) kaynaklarına erişimini yetkilendirmenize veya kısıtlamanıza olanak tanır. Örneğin, başka bir Alibaba Cloud hesabına ait bir RAM kullanıcısına belirli kaynaklar üzerinde salt okuma izinleri verebilirsiniz.
Kullanım notları
Paket ilkelerini ayarlamadan önce bu özelliğe aşina olduğunuzdan emin olun. Daha fazla bilgi için bkz. Bucket Policy.
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. Aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek için dahili bir uç nokta kullanın. Desteklenen bölgeler ve uç noktalar hakkında ayrıntılı bilgi için bkz. Regions and endpoints.
Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınmaktadır. Daha fazla bilgi için bkz. Erişim Kimlik Bilgilerini Yapılandırma.
Bu konu, bir OSS uç noktası ile bir OSSClient örneğinin nasıl oluşturulacağını gösterir. Özel bir alan adı kullanmak veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulaması yapmak gibi alternatif yapılandırmalar için bkz. Client configuration.
Bir paket ilkesi ayarlamak için
oss:PutBucketPolicyiznine sahip olmanız gerekir. Bir paket ilkesini almak içinoss:GetBucketPolicyiznine sahip olmanız gerekir. Bir paket ilkesini silmek içinoss:DeleteBucketPolicyiznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Grant a custom policy.
Paket ilkesi ayarlama
Aşağıda, paket ilkesi ayarlamaya yönelik örnek kod verilmiştir:
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
public class Demo {
public static void main(String[] args) throws Exception {
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 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.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the bucket. Example: examplebucket.
String bucketName = "examplebucket";
// 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.
String region = "cn-hangzhou";
// Create an OSSClient instance.
// To release resources, shut down the OSSClient instance when it is no longer needed.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// In the following example, the bucket owner (UID 174649585760xxxx) uses a bucket policy to grant a RAM user (UID 20214760404935xxxx) permissions to and list all objects in examplebucket.
String policyText = "{\"Statement\": [{\"Effect\": \"Allow\", \"Action\": [\"oss:GetObject\", \"oss:ListObjects\"], \"Principal\": [\"20214760404935xxxx\"], \"Resource\": [\"acs:oss:*:174649585760xxxx:examplebucket/*\"]}], \"Version\": \"1\"}";
// Configure the bucket policy.
ossClient.setBucketPolicy(bucketName, policyText);
} 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();
}
}
}
}
Paket ilkesini alma
Aşağıda, paket ilkesini almaya yönelik örnek kod verilmiştir:
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GetBucketPolicyResult;
public class Demo {
public static void main(String[] args) throws Exception {
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 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.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the bucket. Example: examplebucket.
String bucketName = "examplebucket";
// 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.
String region = "cn-hangzhou";
// Create an OSS Client instance.
// To release resources, shut down the OSSClient instance when it is no longer needed.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Get the bucket policy.
GetBucketPolicyResult result = ossClient.getBucketPolicy(bucketName);
System.out.println("policyText:"+ result.getPolicyText());
} 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();
}
}
}
}
Paket ilkesini silme
Aşağıda, paket ilkesini silmeye yönelik örnek kod verilmiştir:
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
public class Demo {
public static void main(String[] args) throws Exception {
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 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.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the bucket. Example: examplebucket.
String bucketName = "examplebucket";
// 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.
String region = "cn-hangzhou";
// Create an OSS Client instance.
// To release resources, shut down the OSSClient instance when it is no longer needed.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Delete the bucket policy.
ossClient.deleteBucketPolicy(bucketName);
} 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();
}
}
}
}
İlgili Konular
Paket ilkelerini yönetmeye yönelik tam örnek kod için GitHub adresini ziyaret edin.
Paket ilkesi yapılandırmak için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. PutBucketPolicy.
Paket ilkelerini sorgulamak için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. GetBucketPolicy.
Bir paket ilkesini silmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. DeleteBucketPolicy.