Object Storage Service (OSS), paket düzeyindeki erişim kontrol listelerine (ACL) ek olarak nesne düzeyinde ACL'ler de sunar. Bir nesneyi yüklerken ACL'sini belirtebilir veya daha önce yüklenmiş bir nesnenin ACL'sini değiştirebilirsiniz.
Kullanım notları
Bu konuda China (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. Aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek için dahili uç nokta kullanın. Desteklenen bölgeler ve uç noktalar hakkında ayrıntılı bilgi için bkz. Bölgeler ve uç noktalar.
Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınır. Daha fazla bilgi için bkz. Erişim Kimlik Bilgilerini Yapılandırma.
Bu konu, bir OSS uç noktası kullanarak bir OSSClient örneği oluşturmayı gösterir. Özel alan adı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. İstemci yapılandırması.
Bir nesne için ACL yapılandırmak üzere
oss:PutObjectAcliznine sahip olmanız gerekir. Nesne ACL'lerini sorgulamak içinoss:GetObjectAcliznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atama.
ACL türleri
Aşağıdaki tabloda, bir nesne için yapılandırabileceğiniz ACL türleri açıklanmıştır.
Bir nesnenin ACL'si, nesnenin depolandığı paketin ACL'sinden önceliklidir. Örneğin, özel bir pakette bulunan bir nesnenin ACL'si public-read olarak ayarlanırsa, anonim kullanıcılar dahil tüm kullanıcılar nesneyi okuyabilir.
ACL türü | Açıklama | Değer |
Paketten devralınır | Nesnenin ACL'si, nesnenin depolandığı paketin ACL'si ile aynıdır. Bu, bir nesnenin varsayılan ACL'sidir. | CannedAccessControlList.Default |
Private | Yalnızca nesne sahibi nesneyi okuyup yazabilir. Diğer kullanıcılar nesneye erişemez. | CannedAccessControlList.Private |
Public-read | Yalnızca nesne sahibi nesneyi yazabilir. Anonim kullanıcılar dahil diğer kullanıcılar yalnızca nesneyi okuyabilir. Uyarı Bu durum yetkisiz erişime ve beklenmeyen maliyetlere neden olabilir. Bu ACL'yi kullanırken dikkatli olun. | CannedAccessControlList.PublicRead |
Public-read-write | Anonim kullanıcılar dahil tüm kullanıcılar nesneyi okuyup yazabilir. Uyarı Bu ACL, tüm kullanıcıların İnternet üzerinden nesneye erişmesine ve veri yazmasına olanak tanır; bu durum yetkisiz erişime, beklenmeyen maliyetlere veya yasaklı içerik yüklemelerine neden olabilir. Gerekmedikçe nesne ACL'sini public-read-write olarak ayarlamayın. | CannedAccessControlList.PublicReadWrite |
Bir nesnenin ACL'sini yapılandırın
Aşağıdaki örnek kod, bir nesnenin ACL'sinin nasıl yapılandırılacağını gösterir:
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.CannedAccessControlList;
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 full path of the object. Do not include the bucket name in the full path. Example: testfolder/exampleobject.txt.
String objectName = "testfolder/exampleobject.txt";
// 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.
// Call the shutdown method to release associated resources when the OSSClient is no longer in use.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Set the ACL of the object to public read.
ossClient.setObjectAcl(bucketName, objectName, CannedAccessControlList.PublicRead);
} 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();
}
}
}
}
Bir nesnenin ACL'sini sorgulayın
Aşağıdaki örnek kod, bir nesnenin ACL'sinin nasıl sorgulanacağını gösterir:
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.ObjectAcl;
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 full path of the object. Do not include the bucket name in the full path. Example: testfolder/exampleobject.txt.
String objectName = "testfolder/exampleobject.txt";
// 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.
// Call the shutdown method to release associated resources when the OSSClient is no longer in use.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Query the ACL of the object.
ObjectAcl objectAcl = ossClient.getObjectAcl(bucketName, objectName);
System.out.println(objectAcl.getPermission().toString());
} 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();
}
}
}
}
Başvurular
Nesne ACL yönetimi için eksiksiz örnek kodu görüntülemek üzere GitHub adresini ziyaret edin.
Bir nesnenin ACL'sini yapılandırmaya yönelik API işlemi hakkında daha fazla bilgi için bkz. PutObjectACL.
Bir nesnenin ACL'sini sorgulamaya yönelik API işlemi hakkında daha fazla bilgi için bkz. GetObjectACL.