创建桶
功能说明
PutBucket操作用于创建桶(bucket),每个用户可以拥有多个桶。桶的名称在媒体存储范围内必须是全局唯一的,一旦创建之后无法修改名称。桶的创建者默认是桶的所有者,对桶拥有FULL_CONTROL权限,可以通过设置参数的方式为其他用户配置创建桶的权限。桶的命名规范如下:
使用字母、数字和短横线(-);
以小写字母或者数字开头和结尾;
长度在3-63字节之间。
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace DotNetSDK.BucketOperation
{
public class PutBucketExample
{
public static async Task PutBucket()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var putBucketRequest = new PutBucketRequest()
{
BucketName = bucketName
};
var result = await s3Client.PutBucketAsync(putBucketRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to create bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
Console.WriteLine("create bucket {0} success.", bucketName);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
PutBucket可设置的参数如下:
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
BucketName | string | 创建桶的名称 | 是 |
获取桶列表
功能说明
桶(Bucket),是用于存储对象(Object)的容器,所有的对象都必须隶属于某个桶。用户可以设置和修改存储空间属性用来设置访问权限、生命周期等,这些属性设置直接作用于该存储空间内所有对象,因此可以通过灵活创建不同的存储空间来完成不同的管理功能。用户需通过身份验证来查询自己创建的桶,且无法匿名发送请求。
ListBuckets操作列出用户创建的桶。
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
namespace DotNetSDK.BucketOperation
{
public class ListBucketExample
{
public static async Task ListBuckets()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var result = await s3Client.ListBucketsAsync();
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to list buckets, HttpStatusCode:{0}, ErrorCode:{1}.", (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
Console.WriteLine("the buckets of {0} are:", result.Owner.DisplayName);
result.Buckets.ForEach(b => { Console.WriteLine(b.BucketName); });
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
返回结果
ListBuckets操作返回的结果如下:
属性名 | 类型 | 说明 |
---|---|---|
Buckets | List<S3Bucket> | 桶信息的数组,包含了每个桶的名字和创建时间 |
Owner | Owner | 桶的所有者信息 |
判断桶是否存在
功能说明
可以使用AmazonS3Util.DoesS3BucketExistAsync接口判断桶是否存在。
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Util;
namespace DotNetSDK.BucketOperation
{
public class DoesBucketExistExample
{
public static async Task DoesBucketExist()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var exist = AmazonS3Util.DoesS3BucketExistAsync(s3Client, bucketName);
if (exist.Result)
{
Console.Out.WriteLine("bucket exist");
} else
{
Console.Out.WriteLine("bucket not exist");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
bucketName | string | 桶名称 | 是 |
返回结果
DoesS3BucketExistAsync操作返回的结果如下:
属性名 | 类型 | 说明 |
---|---|---|
Result | bool | true表示桶存在,false表示桶不存在 |
删除桶
功能说明
DeleteBucket操作用于删除桶,删除一个桶前,需要先删除该桶中的全部对象(包括object versions和delete markers)。
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace DotNetSDK.BucketOperation
{
public class DeleteBucketExample
{
public static async Task DeleteBucket()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var deleteBucketRequest = new DeleteBucketRequest()
{
BucketName = bucketName
};
var result = await s3Client.DeleteBucketAsync(deleteBucketRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.NoContent)
{
Console.WriteLine("fail to delete bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
Console.WriteLine("delete bucket {0} success.", bucketName);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
DeleteBucket可设置的参数如下:
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
BucketName | string | 桶的名称 | 是 |
设置桶访问权限
功能说明
PutACL操作可以通过access control list(ACL)设置一个桶的访问权限。用户在设置桶的ACL之前需要具备WRITE_ACP 权限。
桶的访问权限说明:
权限类型 | 说明 |
---|---|
READ | 可以对桶进行list操作 |
READ_ACP | 可以读取桶的ACL信息。桶的所有者默认具有桶的READ_ACP权限 |
WRITE | 可以在桶中创建对象,修改原有对象数据和删除对象 |
WRITE_ACP | 可以修改桶的ACL信息,授予该权限相当于授予FULL_CONTROL权限,因为具有WRITE_ACP权限的用户可以配置桶的任意权限。桶的所有者默认具有桶的WRITE_ACP权限 |
FULL_CONTROL | 同时授予READ、READ_ACP、WRITE和WRITE_ACP权限 |
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace DotNetSDK.BucketOperation
{
public class PutBucketACLExample
{
public static async Task PutBucketACL()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var putACLRequest = new PutACLRequest()
{
BucketName = bucketName,
// 添加一个公共读权限
CannedACL = S3CannedACL.PublicRead
};
var result = await s3Client.PutACLAsync(putACLRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to put bucket ACL to bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
Console.WriteLine("set {0} to bucket {1}", putACLRequest.CannedACL.Value, bucketName);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
PutACL可设置的参数如下:
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
CannedACL | S3CannedACL | 配置预定义的标准ACL信息 | 否 |
AccessControlList | S3AccessControlList | 配置自定义的ACL信息 | 否 |
BucketName | string | 桶的名称 | 是 |
S3CannedACL包含了一组预定义的访问控制权限,可以应用于桶的访问权限如下:
权限 | 说明 |
---|---|
NoACL | 默认访问权限,桶的所有者拥有FULL_CONTROL权限,其他用户没有访问权限 |
Private | 桶的所有者拥有FULL_CONTROL权限,其他用户没有访问权限 |
PublicRead | 桶的所有者拥有FULL_CONTROL权限,其他用户拥有READ权限 |
PublicReadWrite | 桶的所有者拥有FULL_CONTROL权限,其他用户拥有READ和WRITE权限 |
获取桶访问权限
功能说明
GetACL操作可以获取桶的access control list(ACL)信息。桶的ACL可以在创建的时候设置并且通过API查看,用户需要具有READ_ACP(读取桶ACL信息)权限才可以查询桶的ACL信息。
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace DotNetSDK.BucketOperation
{
public class GetBucketACLExample
{
public static async Task GetBucketACL()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var getAclRequest = new GetACLRequest()
{
BucketName = bucketName
};
var result = await s3Client.GetACLAsync(getAclRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to get ACL of bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
foreach (var grant in result.AccessControlList.Grants)
{
Console.WriteLine("Type:{0}, CanonicalUser:{1}, DisplayName:{2}, EmailAddress:{3}, URI:{4}, Permission:{5}",
grant.Grantee.Type, grant.Grantee.CanonicalUser, grant.Grantee.DisplayName, grant.Grantee.EmailAddress, grant.Grantee.URI, grant.Permission.Value);
}
Console.WriteLine("OwnerId:{0}, OwnerDisplayName:{1}.", result.AccessControlList.Owner.Id, result.AccessControlList.Owner.DisplayName);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
GetACL可设置的参数如下:
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
BucketName | string | 桶的名称 | 是 |
返回结果
GetACL返回的结果如下:
属性名 | 类型 | 说明 |
---|---|---|
Grants | List<S3Grant> | Grant信息的数组,包含了每项授权和被授予人的信息 |
Owner | Owner | 桶的所有者信息 |
设置桶策略
功能说明
桶策略(bukcet policy)可以灵活地配置用户各种操作和访问资源的权限。访问控制列表(access control lists,ACL)只能对单一对象设置权限,而桶策略可以基于各种条件对一个桶内的全部或者一组对象配置权限。桶的所有者拥有PutBucketPolicy操作的权限,如果桶已经被设置了policy,则新的policy会覆盖原有的policy。
PutBucketPolicy操作可以设置桶策略,描述桶策略的信息以JSON格式的字符串形式通过Policy参数传入。一个policy的示例如下:
{
"Id":"PolicyId",
"Version":"2012-10-17",
"Statement":[
{
"Sid":"ExampleStatementID1",
"Principal":{
"AWS":[
"arn:aws:iam:::user/userId",
"arn:aws:iam:::user/userName"
]
},
"Effect":"Allow",
"Action":[
"s3:ListBucket",
"s3:CreateBucket"
],
"Resource":[
"arn:aws:iam:::exampleBucket"
],
"Condition":"some conditions"
},
......
]
}
Statement的内容说明如下:
元素 | 描述 | 是否必要 |
---|---|---|
Sid | statement Id,可选关键字,描述statement的字符串 | 否 |
Principal | 可选关键字,被授权人,指定本条statement权限针对的Domain以及User,支持通配符“*”,表示所有用户(匿名用户)。当对Domain下所有用户授权时,Principal格式为arn:aws:iam:::user/*。当对某个User进行授权时,Principal格式为arn:aws:iam:::user/userId或者arn:aws:iam:::user/userName | 可选,Principal与NotPrincipal选其一 |
NotPrincipal | 可选关键字,不被授权人,statement匹配除此之外的其他人。取值同Principal | 可选,NotPrincipal与Principal选其一 |
Action | 可选关键字,指定本条statement作用的操作,Action字段为媒体存储支持的所有操作集合,以字符串形式表示,不区分大小写。支持通配符“*”,表示该资源能进行的所有操作。例如:"Action":["s3:List*", "s3:Get*"]。 | 可选,Action与NotAction选其一 |
NotAction | 可选关键字,指定一组操作,statement匹配除该组操作之外的其他操作。 取值同Action | 可选,NotAction与Action选其一 |
Effect | 必选关键字,指定本条statement的权限是允许还是拒绝,Effect的值必须为Allow或者Deny | 必选 |
Resource | 可选关键字,指定statement起作用的一组资源,支持通配符“*”,表示所有资源 | 可选,Resource与NotResource选其一 |
NotResource | 可选关键字,指定一组资源,statement匹配除该组资源之外的其他资源。 取值同Resource | 可选,NotResource与Resource选其一 |
Condition | 可选关键字,本条statement生效的条件 | 可选 |
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace DotNetSDK.BucketOperation
{
public class PutBucketPolicyExample
{
public static async Task PutBucketPolicy()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
var policyJsonStr = "<policy-json>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var putBucketPolicyRequest = new PutBucketPolicyRequest()
{
BucketName = bucketName,
Policy = policyJsonStr
};
var result = await s3Client.PutBucketPolicyAsync(putBucketPolicyRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to put policy to bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
PutBucketPolicy可以设置的参数如下:
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
BucketName | string | 桶的名称 | 是 |
Policy | string | JSON格式的桶策略信息 | 是 |
获取桶策略
功能说明
GetBucketPolicy操作用于获取桶的policy,policy配置功能可以使用户根据需求更精确地定义桶的访问策略。桶的所有者可以查看桶的policy信息。
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace DotNetSDK.BucketOperation
{
public class GetBucketPolicyExample
{
public static async Task GetBucketPolicy()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var getBucketPolicyRequest = new GetBucketPolicyRequest()
{
BucketName = bucketName
};
var result = await s3Client.GetBucketPolicyAsync(getBucketPolicyRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to get policy of bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
Console.WriteLine("the policy of bucket {0} is: {1}.", bucketName, result.Policy);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
GetBucketPolicy可设置的参数如下:
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
BucketName | string | bucekt的名称 | 是 |
返回结果
GetBucketPolicy返回的结果如下:
属性名 | 类型 | 说明 |
---|---|---|
Policy | string | JSON格式的bucekt策略信息 |
删除桶策略
功能说明
DeleteBucketPolicy操作可以删除桶已经配置的策略,桶的所有者默认拥有删除桶策略的权限。
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace DotNetSDK.BucketOperation
{
public class DeleteBucketPolicyExample
{
public static async Task DeleteBucketPolicy()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var deleteBucketPolicyRequest = new DeleteBucketPolicyRequest()
{
BucketName = bucketName
};
var result = await s3Client.DeleteBucketPolicyAsync(deleteBucketPolicyRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to delete policy of bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
DeleteBucketPolicy可以设置的参数如下:
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
BucketName | string | bucekt的名称 | 是 |
设置桶生命周期配置
功能说明
PutLifecycleConfiguration操作可以设置桶的生命周期规则,规则可以通过匹配对象key前缀、标签匹配等方式获取当前版本或者历史版本对象的过期时间,对象过期后会被自动删除。桶的版本控制状态必须处于Enabled或者Suspended,历史版本对象过期时间配置才能生效。每次执行PutBucketLifecycleConfiguration操作会覆盖桶中已存在的生命周期规则。
代码示例
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace DotNetSDK.BucketOperation
{
public class PutBucketLifeCycleConfigurationExample
{
public static async Task PutBucketLifecycleConfiguration()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
List<LifecycleRule> rules = new List<LifecycleRule>();
// rule1:设置符合指定前缀的对象一天后过期
var rule1 = new LifecycleRule()
{
Id = "expireAfterOneDay",
Filter = new LifecycleFilter()
{
LifecycleFilterPredicate = new LifecyclePrefixPredicate()
{
Prefix = "expireAfterOneDay/"
}
},
Status = LifecycleRuleStatus.Enabled,
Expiration = new LifecycleRuleExpiration()
{
Days = 1
}
};
rules.Add(rule1);
// rule2: 设置符合指定前缀的对象的历史版本一天后过期
var rule2 = new LifecycleRule()
{
Id = "noncurrentVersionExpireAfterOneDay",
Status = LifecycleRuleStatus.Enabled,
Filter = new LifecycleFilter()
{
LifecycleFilterPredicate = new LifecyclePrefixPredicate()
{
Prefix = "noncurrentVersionExpireAfterOneDay/"
}
},
NoncurrentVersionExpiration = new LifecycleRuleNoncurrentVersionExpiration()
{
NoncurrentDays = 1
}
};
rules.Add(rule2);
// rule3: 设置匹配指定标签信息的对象一天后过期
var rule3 = new LifecycleRule()
{
Id = "withTagsExpireAfterOneDay",
Status = LifecycleRuleStatus.Enabled,
Expiration = new LifecycleRuleExpiration()
{
Days = 1
},
Filter = new LifecycleFilter()
{
LifecycleFilterPredicate = new LifecycleTagPredicate()
{
Tag = new Tag()
{
Key = "<key1>",
Value = "<value1>"
}
}
},
};
rules.Add(rule3);
LifecycleConfiguration configuration = new LifecycleConfiguration()
{
Rules = rules
};
var putLifecycleConfigurationRequest = new PutLifecycleConfigurationRequest()
{
BucketName = bucketName,
Configuration = configuration
};
var result = await s3Client.PutLifecycleConfigurationAsync(putLifecycleConfigurationRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to put lifecycle configuration to bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
PutLifecycleConfiguration可设置的参数如下:
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
BucketName | string | 桶的名称 | 是 |
Configuration | LifecycleConfiguration | 封装了生命周期规则的数组,最多可以包含1000条规则 | 是 |
获取桶生命周期配置
功能说明
生命周期管理可以通过设置规则实现自动清理过期的对象,优化存储空间。GetBucketLifecycleConfiguration操作可以查看桶当前的生命周期规则。
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace DotNetSDK.BucketOperation
{
public class GetBucketLifecycleConfigurationExample
{
public static async Task GetBucketLifecycleConfiguration()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var getLifecycleConfigurationRequest = new GetLifecycleConfigurationRequest()
{
BucketName = bucketName
};
var result = await s3Client.GetLifecycleConfigurationAsync(getLifecycleConfigurationRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to get lifecycle configuration of bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
Console.WriteLine("the lifecycle configuration of bucket {0} are:", bucketName);
foreach (var lifecycleRule in result.Configuration.Rules)
{
Console.WriteLine("Lifecycle rule id: {0}", lifecycleRule.Id);
Console.WriteLine("Lifecycle rule status: {0}", lifecycleRule.Status);
if (null != lifecycleRule.Expiration)
{
Console.WriteLine("expiration days: {0}", lifecycleRule.Expiration.Days);
}
if (null != lifecycleRule.NoncurrentVersionExpiration)
{
Console.WriteLine("NoncurrentVersionExpiration NoncurrentDays: {0}", lifecycleRule.NoncurrentVersionExpiration.NoncurrentDays);
}
if (null != lifecycleRule.Transitions)
{
foreach (var transition in lifecycleRule.Transitions)
{
Console.WriteLine("Transition Days: {0}", transition.Days.ToString());
}
}
if (null != lifecycleRule.NoncurrentVersionTransitions)
{
foreach (var nontransition in lifecycleRule.NoncurrentVersionTransitions)
{
Console.WriteLine("NoncurrentVersionTransition NoncurrentDays: {0}", nontransition.NoncurrentDays.ToString());
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
GetBucketLifecycleConfiguration可设置的参数如下:
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
BucketName | string | 桶的名称 | 是 |
返回结果
GetBucketLifecycleConfiguration返回的结果如下:
属性名 | 类型 | 说明 |
---|---|---|
Rules | List<LifecycleRule> | 一个描述生命周期管理的规则数组,一条规则包含了规则ID、匹配的对象key前缀、匹配的对象标签信息、当前版本对象过期时间、历史版本对象过期时间和是否生效标识等信息 |
删除桶生命周期配置
功能说明
DeleteLifecycleConfiguration操作可以删除桶中的全部生命周期规则。
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace DotNetSDK.BucketOperation
{
public class DeleteBucketLifecycleConfigurationExample
{
public static async Task DeleteBucketLifeConfiguration()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var deleteLifecycleConfigurationRequest = new DeleteLifecycleConfigurationRequest()
{
BucketName = bucketName
};
var result = await s3Client.DeleteLifecycleConfigurationAsync(deleteLifecycleConfigurationRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.NoContent)
{
Console.WriteLine("fail to delete lifecycle configuration of bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
DeleteLifecycleConfiguration可设置的参数如下:
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
BucketName | string | 桶的名称 | 是 |
设置桶跨域访问配置
功能说明
跨域资源共享 (CORS) 定义了在一个域中加载的客户端 Web 应用程序与另一个域中的资源交互的方式。利用 CORS 支持,您可以构建丰富的客户端 Web 应用程序,同时可以选择性地允许跨源访问您的资源。
您可以通过PutCORSConfiguration接口设置桶的跨域访问配置。
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace DotNetSDK.BucketOperation
{
public class PutBucketCORSExample
{
public static async Task PutCORSConfiguration()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var rule = new CORSRule();
rule.AllowedMethods.Add("PUT");
rule.AllowedMethods.Add("GET");
rule.AllowedMethods.Add("HEAD");
rule.AllowedMethods.Add("POST");
rule.AllowedMethods.Add("DELETE");
rule.AllowedHeaders.Add("*");
rule.AllowedOrigins.Add("*"); // 可以使用http://domain:port
rule.ExposeHeaders.Add("ETag");
rule.MaxAgeSeconds = 3600;
var req = new PutCORSConfigurationRequest()
{
BucketName = bucketName,
Configuration = new CORSConfiguration()
};
req.Configuration.Rules.Add(rule);
var result = await s3Client.PutCORSConfigurationAsync(req);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to put bucket cors {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
BucketName | string | 桶名称 | 是 |
Configuration | CORSConfiguration | 跨域访问规则 | 是 |
关于CORSConfiguration一些说明
参数 | 说明 |
---|---|
AllowedMethods | 允许的请求方法 |
AllowedOrigins | 允许的请求源 |
AllowedHeaders | 允许的请求头 |
ExposedHeaders | 允许返回的Response Header |
MaxAgeSeconds | 跨域请求结果的缓存时间 |
获取桶跨域访问配置
功能说明
跨域资源共享 (CORS) 定义了在一个域中加载的客户端 Web 应用程序与另一个域中的资源交互的方式。利用 CORS 支持,您可以构建丰富的客户端 Web 应用程序,同时可以选择性地允许跨源访问您的资源。
您可以通过GetCORSConfiguration接口设置桶的跨域访问配置。
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
namespace DotNetSDK.BucketOperation
{
public class GetBucketCORSExample
{
public static async Task GetCORSConfiguration()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var result = await s3Client.GetCORSConfigurationAsync(bucketName);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to get bucket cors {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
foreach (var corsRule in result.Configuration.Rules)
{
Console.WriteLine("cors rule's methods: {0}", string.Join(", ", corsRule.AllowedMethods));
Console.WriteLine("cors rule's headers: {0}", string.Join(", ", corsRule.AllowedHeaders));
Console.WriteLine("cors rule's origins: {0}", string.Join(", ", corsRule.AllowedOrigins));
Console.WriteLine("cors rule's expose headers: {0}", string.Join(", ", corsRule.ExposeHeaders));
Console.WriteLine("cors rule's expired seconds: {0}", corsRule.MaxAgeSeconds);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
bucketName | string | 桶名称 | 是 |
返回结果
参数 | 类型 | 说明 |
---|---|---|
Configuration | CORSConfiguration | 跨域访问规则,包含规则id,请求方法,请求源等信息 |
关于CORSConfiguration一些说明
参数 | 说明 |
---|---|
AllowedMethods | 允许的请求方法 |
AllowedOrigins | 允许的请求源 |
AllowedHeaders | 允许的请求头 |
ExposedHeaders | 允许返回的Response Header |
MaxAgeSeconds | 跨域请求结果的缓存时间 |
删除桶跨域访问配置
功能说明
跨域资源共享 (CORS) 定义了在一个域中加载的客户端 Web 应用程序与另一个域中的资源交互的方式。利用 CORS 支持,您可以构建丰富的客户端 Web 应用程序,同时可以选择性地允许跨源访问您的资源。
您可以通过DeleteCORSConfiguration接口删除桶跨域访问配置。
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
namespace DotNetSDK.BucketOperation
{
public class DeleteBucketCORSExample
{
public static async Task DeleteCORSConfiguration()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var result = await s3Client.DeleteCORSConfigurationAsync(bucketName);
if (result.HttpStatusCode != System.Net.HttpStatusCode.NoContent)
{
Console.WriteLine("fail to get bucket cors {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName,
(int)result.HttpStatusCode, result.HttpStatusCode);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
Bucket | string | 桶名称 | 是 |
设置桶版本控制状态
请求参数
PutBucketVersioning操作可以设置桶版本控制状态。桶的版本控制状态可以设置为以下的值:
Enabled:对桶中的所有对象启用版本控制,之后每个添加到桶中的对象都会被设置一个唯一的version id。
Suspended:关闭桶的版本控制,之后每个添加到桶中的对象的version id会被设置为null。
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace DotNetSDK.BucketOperation
{
public class PutBucketVersioningExample
{
public static async Task PutBucketVersioning()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var putBucketVersioningRequest = new PutBucketVersioningRequest()
{
BucketName = bucketName,
VersioningConfig = new S3BucketVersioningConfig()
{
Status = VersionStatus.Enabled
}
};
var result = await s3Client.PutBucketVersioningAsync(putBucketVersioningRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to put versioning config to bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
PutBucketVersioning可设置的参数如下:
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
BucketName | string | 桶的名称 | 是 |
VersioningConfig | S3BucketVersioningConfig | 封装了设置版本控制状态的参数 | 是 |
获取桶版本控制状态
功能说明
GetBucketVersioning操作可以获取桶的版本控制状态信息。桶的所有者默认拥有获取到桶的版本控制信息的权限。
每个桶的版本控制有三个状态:未开启(Off)、开启(Enabled)和暂停(Suspended)版本控制,如果桶从来没有被设置过版本控制状态,那么该桶默认为未开启版本控制状态。
代码示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace DotNetSDK.BucketOperation
{
public class GetBucketVersioningExample
{
public static async Task GetBucketVersioning()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var getBucketVersioningRequest = new GetBucketVersioningRequest()
{
BucketName = bucketName
};
var result = await s3Client.GetBucketVersioningAsync(getBucketVersioningRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to get versioning config of bucket {0}, HttpStatusCode: {1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
Console.WriteLine("the versioning config of bucket {0} is: {1}.", bucketName,
result.VersioningConfig.Status.Value);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
请求参数
GetBucketVersioning可设置的参数如下:
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
BucketName | string | 桶的名称 | 是 |
返回结果
GetBucketVersioning返回的结果如下:
属性名 | 类型 | 说明 |
---|---|---|
VersioningConfig | S3BucketVersioningConfig | 封装桶的版本控制状态信息的类,其中的Status属性描述了桶的版本控制设置状态 |