Object Storage Service (OSS) は、アップロードされたデータをサーバー上で暗号化できます。これはサーバー側暗号化と呼ばれます。OSS にデータをアップロードすると、OSS はアップロードされたデータを暗号化し、暗号化されたデータを永続的に保存します。OSS からデータをダウンロードすると、OSS はデータを復号化し、復号化されたデータを返します。さらに、データがサーバー上で暗号化されていることを宣言するために、レスポンスにヘッダーが追加されます。
使用上の注意
サーバー側暗号化を構成する前に、この機能をよく理解しておいてください。詳細については、「サーバー側暗号化」をご参照ください。
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用しています。OSS と同じリージョン内の他の Alibaba Cloud サービスから OSS にアクセスする場合は、内部エンドポイントを使用します。OSS のリージョンとエンドポイントの詳細については、「リージョンとエンドポイント」をご参照ください。
このトピックでは、OSS エンドポイントを使用して OSSClient インスタンスを作成します。カスタムドメイン名または Security Token Service (STS) を使用して OSSClient インスタンスを作成する場合は、「初期化」をご参照ください。
バケットのサーバー側暗号化を構成するには、
oss:PutBucketEncryption
権限が必要です。バケットのサーバー側暗号化構成を照会するには、oss:GetBucketEncryption
権限が必要です。バケットのサーバー側暗号化構成を削除するには、oss:DeleteBucketEncryption
権限が必要です。詳細については、「RAM ユーザーにカスタムポリシーをアタッチする」をご参照ください。
バケットのサーバー側暗号化を構成する
次のサンプルコードは、バケットのデフォルトの暗号化方法を構成する方法の例を示しています。デフォルトの暗号化方法を構成した後、暗号化方法を指定せずにバケットにアップロードされたすべてのオブジェクトは、デフォルトの暗号化方法を使用して暗号化されます。
using Aliyun.OSS;
using Aliyun.OSS.Common;
// 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.
var endpoint = "yourEndpoint";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var 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.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify parameters as required.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Configure server-side encryption for the bucket.
var request = new SetBucketEncryptionRequest(bucketName, "KMS", null);
client.SetBucketEncryption(request);
Console.WriteLine("Set bucket:{0} Encryption succeeded ", bucketName);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
バケットのサーバー側暗号化構成を照会する
次のサンプルコードは、バケットのサーバー側暗号化構成を照会する方法の例を示しています。
using Aliyun.OSS;
using Aliyun.OSS.Common;
// 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.
var endpoint = "yourEndpoint";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var 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.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify parameters as required.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Query the server-side encryption configurations of the bucket.
var result = client.GetBucketEncryption(bucketName);
Console.WriteLine("Set bucket:{0} Encryption succeeded ", bucketName);
Console.WriteLine("SSEAlgorithm: {0}", result.SSEAlgorithm);
Console.WriteLine("KMSMasterKeyID: {0}", result.KMSMasterKeyID);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
バケットのサーバー側暗号化構成を削除する
次のサンプルコードは、バケットのサーバー側暗号化構成を削除する方法の例を示しています。
using Aliyun.OSS;
using Aliyun.OSS.Common;
// 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.
var endpoint = "yourEndpoint";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var 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.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify parameters as required.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Delete the server-side encryption configurations of the bucket.
client.DeleteBucketEncryption(bucketName);
Console.WriteLine("Delete bucket:{0} Encryption succeeded ", bucketName);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
関連情報
サーバー側暗号化を構成するために呼び出すことができる API 操作の詳細については、「PutBucketEncryption」をご参照ください。
サーバー側暗号化構成を照会するために呼び出すことができる API 操作の詳細については、「GetBucketEncryption」をご参照ください。
サーバー側暗号化構成を削除するために呼び出すことができる API 操作の詳細については、「DeleteBucketEncryption」をご参照ください。