文档中心 > 设置桶加密
设置桶加密

最近更新时间:2022-07-25

UOS支持在服务器端对上传的数据进行加密编码(Server-Side Encryption)。上传数据时,UOS对收到的用户数据进行加密,然后再将得到的加密数据持久化保存下来;下载数据时,UOS自动对保存的加密数据进行解密并把原始数据返回给用户,并在返回的HTTP请求Header中,声明该数据进行了服务器端加密。

以下代码用于设置桶加密。

using Amazon.S3;

using Amazon.S3.Model;

 

namespace Createbucket

{

    class Program

    {

        static void Main(string[] args)

        {  

            var Ak = "";

            var Sk = "";

            var endpoint = "http://s3.test.com";

            var bucketName = "";

 

            AmazonS3Client serviceClient = new AmazonS3Client(Ak,Sk,

            new AmazonS3Config{ ServiceURL = endpoint });

 

            try

            {  

                ServerSideEncryptionRule rule = new ServerSideEncryptionRule

                {

                    ServerSideEncryptionByDefault = new ServerSideEncryptionByDefault

                    {

                        ServerSideEncryptionAlgorithm = ServerSideEncryptionMethod.AWSKMS,

                        ServerSideEncryptionKeyManagementServiceKeyId = "39462e0c-a6f5-4303-bf23-dbb0a7314100",

                    },

                };

                Listrules = new List();

                rules.Add(rule);

                PutBucketEncryptionResponse putRes;

                PutBucketEncryptionRequest putReq = new PutBucketEncryptionRequest

                {

                    BucketName = bucketName,

                    ServerSideEncryptionConfiguration = new ServerSideEncryptionConfiguration

                    {

                        ServerSideEncryptionRules = rules,

                    }

                };

                putRes = serviceClient.PutBucketEncryptionAsync(putReq).GetAwaiter().GetResult();

                Console.WriteLine(putRes.HttpStatusCode);

            }

            catch (AmazonS3Exception e)

            {

                Console.WriteLine(e.Message);

            }

        }

    }

}