創建桶
功能說明
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("*"); // 可以使用//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屬性描述了桶的版本控制設置狀態 |