獲取對象列表
功能說明
ListObjects操作用于列出桶中的全部對象,該操作返回最多1000個對象信息,可以通過設置過濾條件來列出桶中符合特定條件的對象信息。
代碼示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
?
namespace DotNetSDK.ObjectOperation
{
public class ListObjectsExample
{
public static async Task ListObjects()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
var credentials = new BasicAWSCredentials(accessKey, secretKey);
try
{
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var listObjectsRequest = new ListObjectsRequest()
{
BucketName = bucketName
};
?
var result = await s3Client.ListObjectsAsync(listObjectsRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to list objects in bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
?
Console.WriteLine("the objects in bucket {0} are: ", bucketName);
foreach (var obj in result.S3Objects)
{
Console.WriteLine(obj.Key);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}如果 list 大于1000,則返回的結果中 isTruncated 為true,通過返回的 nextMarker 標記可以作為下次讀取的起點。列舉所有對象示例代碼如下:
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
?
namespace DotNetSDK.ObjectOperation
{
public class ListObjectsExample
{
public static async Task ListObjects2()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
var credentials = new BasicAWSCredentials(accessKey, secretKey);
try
{
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
?
string marker = string.Empty;
bool isTruncated;
?
do
{
var listObjectsRequest = new ListObjectsRequest()
{
BucketName = bucketName,
Marker = marker
};
?
var result = await s3Client.ListObjectsAsync(listObjectsRequest);
?
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to list objects in bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.",
bucketName, (int)result.HttpStatusCode, result.HttpStatusCode);
return;
}
?
foreach (var obj in result.S3Objects)
{
Console.WriteLine(obj.Key);
}
?
isTruncated = result.IsTruncated;
marker = result.NextMarker ?? string.Empty;
?
} while (isTruncated && !string.IsNullOrEmpty(marker));
?
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}請求參數
ListObjects可以設置的參數如下:
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| BucketName | string | 桶的名稱 | 是 |
| Delimiter | string | 與Prefix參數一起用于對對象key進行分組的字符。所有key包含指定的Prefix且第一次出現Delimiter字符的對象作為一組。如果沒有指定Prefix參數,按Delimiter對所有對象key進行分割,多個對象分割后從對象key開始到第一個Delimiter之間相同的部分形成一組 | 否 |
| Marker | string | 指定一個標識符,返回的對象的key將是按照字典順序排序后位于該標識符之后的所有對象 | 否 |
| MaxKeys | int | 設置response中返回對象的數量,默認值和最大值均為1000 | 否 |
| Prefix | string | 限定返回對象的key必須以Prefix作為前綴 | 否 |
返回結果
ListObjects返回的結果如下:
| 屬性名 | 類型 | 說明 |
|---|---|---|
| CommonPrefixes | List<string> | 當請求中設置了Delimiter和Prefix屬性時,所有包含指定的Prefix且第一次出現Delimiter字符的key作為一組 |
| Contents | List<S3Object> | 對象數據,每個對象包含了Etag、Key、LastModifiedTime、Owner和Size等信息 |
| Delimiter | string | 與請求中設置的Delimiter一致 |
| IsTruncated | bool | 當為false時表示返回結果中包含了全部符合本次請求查詢條件的對象信息,否則只返回了數量為MaxKeys個的對象信息 |
| MaxKeys | int | 本次返回結果包含的對象數量上限 |
| Name | string | 桶的名字 |
| NextMarker | string | 當返回結果中的IsTruncated為true時,可以使用NextMarker作為下次查詢的Marker,繼續查詢出下一部分的對象信息 |
| Prefix | string | 與請求中設置的Prefix一致 |
上傳對象
功能說明
PutObject操作用于上傳對象。如果對同一個對象同時發起多個上傳請求,最后一次完成的請求將覆蓋之前所有請求的上傳的對象。可以通過設置請求頭部中的Content-MD5字段來保證數據被完整上傳,如果上傳的數據不能通過MD5校驗,該操作將返回一個錯誤提示。用戶可以通過比較上傳對象后獲得的ETag與原文件的MD5值是否相等來確認上傳操作是否成功。
代碼示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
?
namespace DotNetSDK.ObjectOperation
{
public class PutObjectExample
{
public static async Task PutObject()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
var key = "<your-object-key>";
var filePath = "<file-path>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var putObjectRequest = new PutObjectRequest()
{
BucketName = bucketName,
Key = key,
FilePath = filePath
};
?
var result = await s3Client.PutObjectAsync(putObjectRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to put object {0}, HttpStatusCode:{1}, ErrorCode:{2}.", key, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
?
Console.WriteLine("put obejct {0}, ETag: {1}, versionId:{2}", key, result.ETag, result.VersionId);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}請求參數
PutObject可設置的參數如下:
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| CannedACL | S3CannedACL | 配置上傳對象的預定義的標準ACL信息 | 否 |
| ContentBody | string | 上傳對象的數據內容,與FilePath、InputStream任選其一作為上傳對象數據的來源 | 否 |
| ContentType | string | 描述上傳文件格式的標準MIME類型 | 否 |
| FilePath | string | 上傳文件的本地路徑,與ContentBody、InputStream任選其一作為上傳對象數據的來源 | 否 |
| InputStream | Stream | 對象的數據,與ContentBody、FilePath任選其一作為上傳對象數據的來源 | 否 |
| Bucket | string | 桶的名稱 | 是 |
| MD5Digest | string | 上傳對象數據的base64編碼的128位MD5值,不包含請求頭部的信息 | 否 |
| Key | string | 上傳文件到媒體存儲服務后對應的key | 是 |
| Tagset | List<Tag> | 對象的標簽信息 | 否 |
| WebsiteRedirectLocation | string | 如果桶被配置用于提供網站的靜態數據,該參數可以用于設置訪問對象時候重定向到當前桶下的其他對象或者外部的URL | 否 |
返回結果
PutObject返回的結果如下:
| 參數 | 類型 | 說明 |
|---|---|---|
| ETag | string | 上傳對象后的對應的Entity Tag |
| VersionId | string | 上傳對象后相應的版本id |
下載對象
功能說明
GetObject操作可以獲取對象數據,并且保存為本地文件。執行GetObject操作必須對目標對象具有READ權限。
代碼示例
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
?
namespace DotNetSDK.ObjectOperation
{
public class GetObjectExample
{
public static async Task GetObject()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
var key = "<your-object-key>";
var filePath = "<file-path>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var getObjectRequest = new GetObjectRequest()
{
BucketName = bucketName,
Key = key
};
?
using (var result = await s3Client.GetObjectAsync(getObjectRequest))
using (Stream stream = result.ResponseStream)
{
var fileStream = File.Create(filePath);
await stream.CopyToAsync(fileStream);
fileStream.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}請求參數
GetObject可設置的參數如下:
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| BucketName | string | 桶的名稱 | 是 |
| EtagToMatch | string | 用于指定只有在對象的ETag和該參數值匹配的情況下才返回對象數據,否則返回412錯誤碼 | 否 |
| ModifiedSinceDateUtc | DateTime | 用于只有當對象在指定時間后被修改的情況下才返回該對象,否則返回304錯誤碼 | 否 |
| EtagToNotMatch | string | 用于指定只有在對象的ETag和該參數值不匹配的情況下才返回對象數據,否則返回304錯誤碼 | 否 |
| UnmodifiedSinceDateUtc | DateTime | 用于僅當對象自指定時間以來未被修改的情況下才返回對象數據,否則返回412錯誤碼 | 否 |
| Key | string | 對象的key | 是 |
| PartNumber | int | 讀取對象指定的分片,該參數大于等于1,小于等于10000 | 否 |
| ByteRange | ByteRange | 下載對象指定范圍內的數據(單位:字節) | 否 |
| VersionId | string | 當桶開啟版本控制的時候,用于指定獲取指定版本的對象數據,當不指定該參數的時候,默認獲取最新版本的對象數據 | 否 |
返回結果
GetObject返回的結果如下:
| 參數 | 類型 | 說明 |
|---|---|---|
| BucketName | string | 對象所在桶的名稱 |
| ContentLength | long | 對象數據的長度,單位為字節 |
| ETag | string | 對象的Entity Tag |
| LastModified | DateTime | 最后修改對象的時間 |
| TagCount | int | 對象標簽的數量 |
| VersionId | string | 對象的version id |
復制對象
功能說明
CopyObject操作用于根據一個已存在的對象創建新的對象。使用CopyOoject可以復制單個最大為5GB的對象,如果需要復制更大的對象,可以使用UploadPartCopy操作。執行CopyObject操作,必須具有對被拷貝對象的READ權限和對目標桶的WRITE權限。
拷貝生成的對象默認保留原對象的元數據信息,也可以才CopyObject操作中指定新的元數據。拷貝生成的對象不會保留原來的ACL信息,該對象默認是發起CopyObject操作的用戶私有的。
CopyObject操作默認拷貝原對象的當前版本數據,如果需要拷貝原對象的指定版本,可以在CopySource中加入version id來拷貝指定的Object版本,如果原對象的version id為刪除標記,則不會被拷貝。
代碼示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
?
namespace DotNetSDK.ObjectOperation
{
public class CopyObjectExample
{
public static async Task CopyObject()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var sourceBucket = "<source-bucket>";
var sourceKey = "<source-key>";
var destinationBucket = "<destination-bucket>";
var destinationKey = "<destination-key>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var copyObjectRequest = new CopyObjectRequest()
{
SourceBucket = sourceBucket,
SourceKey = sourceKey,
DestinationBucket = destinationBucket,
DestinationKey = destinationKey
};
var result = await s3Client.CopyObjectAsync(copyObjectRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to copy object, HttpStatusCode:{0}, ErrorCode:{1}.", (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
?
Console.WriteLine("copy object from {0}/{1} to {2}/{3}.", sourceBucket, sourceKey, destinationBucket, destinationKey);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}請求參數
CopyObject可設置的參數如下:
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| CannedACL | S3CannedACL | 拷貝后對象的預定義的標準ACL信息 | 否 |
| DestinationBucket | string | 放置拷貝生成對象的桶名稱 | 是 |
| DestinationKey | string | 拷貝生成對象的key | 是 |
| SourceBucket | string | 放置被拷貝對象的桶的名稱 | 是 |
| SourceKey | string | 被拷貝對象的key | 是 |
| Metadata | MetadataCollection | 拷貝生成對象的元數據信息 | 否 |
| MetadataDirective | S3MetadataDirective | 指定拷貝生成的對象的元數據信息來自被拷貝對象,還是來自請求中附帶的信息 | 否 |
| TagSet | List<Tag> | 拷貝生成對象的標簽信息 | 否 |
| SourceVersionId | string | 指定被拷貝對象的版本信息,如果不指定,默認拷貝對象的當前版本 | 否 |
| WebsiteRedirectLocation | string | 如果桶被配置用于提供網站的靜態數據,該參數可以用于設置訪問對象時候重定向到當前桶下的其他對象或者外部的URL | 否 |
返回結果
CopyObject返回的結果如下:
| 參數 | 類型 | 說明 |
|---|---|---|
| ETag | string | 拷貝生成對象的ETag |
| LastModified | string | 拷貝生成對象的最新修改時間 |
刪除對象
功能說明
DeleteObject操作用于刪除一個對象。當桶未開啟多版本時會直接刪除對象。對開啟版本控制的桶執行刪除對象操作時,如果未指定version id ,則保留對象的當前版本,并插入刪除標記(Delete Marker)。如果指定version id,則永久刪除該指定版本的對象。如果在未指定version id的情況下執行DeleteObject操作時,默認僅作用于對象的當前版本,但不會直接刪除該對象的當前版本,而是插入一個刪除標記(Delete Marker),并保留原來的當前版本。當執行GetObject操作時,會檢測到當前版本為刪除標記,并返回404 NotFound。
代碼示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
?
namespace DotNetSDK.ObjectOperation
{
public class DeleteObjectExample
{
public static async Task DeleteObject()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
var key = "<your-object-key>";
var credentials = new BasicAWSCredentials(accessKey, secretKey);
try
{
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
?
var deleteObjectRequest = new DeleteObjectRequest()
{
BucketName = bucketName,
Key = key
};
var result = await s3Client.DeleteObjectAsync(deleteObjectRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.NoContent)
{
Console.WriteLine("fail to delete object {0}, HttpStatusCode:{1}, ErrorCode:{2}.", key, (int) result.HttpStatusCode, result.HttpStatusCode);
}
?
Console.WriteLine("deleted object {0}.", key);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}請求參數
DeleteObject可設置的參數如下:
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| BucketName | string | 桶的名稱 | 是 |
| Key | string | 對象的key | 是 |
| VersionId | string | 用于指定要刪除對象的版本,即version id | 否 |
返回結果
DeleteObject返回的結果如下:
| 參數 | 類型 | 說明 |
|---|---|---|
| DeleteMarker | string | 有效值為true。在桶開啟版本控制的情況下,執行DeleteObject 操作時如果未指定對象的版本,會創建刪除標記,此時DeleteMarker為true。如果指定對象的版本來永久刪除指定對象版本時,若該版本是刪除標記,則DeleteMarker也為true。其他情況下DeleteMarker的值為空 |
| VersionId | string | 執行DeleteObject操作時如果未指定對象的版本Id,會創建刪除標記,VersionId為刪除標記的版本。 如果指定版本來永久刪除對象指定版本時,返回的VersionId為對象的版本 |
批量刪除對象
功能說明
(本接口目前僅支持部分資源池使用,如需使用,請聯系天翼云客服確認。)
DeleteObjects操作可以實現通過一個請求批量刪除多個對象的功能,可以減少發起多起請求去刪除大量對象的花銷。DeleteObjects操作發起一個包含了最多1000個對象的刪除請求,對象存儲服務會對相應的對象逐個進行刪除,并且將刪除成功或者失敗的結果通過response返回。如果請求刪除的對象不存在,會當作已刪除處理。
DeleteObjects操作返回verbose和quiet兩種response模式。 verbose response是默認的返回模式,該模式的返回結果包含了每個key的刪除結果。quiet response返回模式返回的結果僅包含了刪除失敗的key,對于一個完全成功的刪除操作,該返回模式不在相應消息體中返回任何信息。
代碼示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
?
namespace DotNetSDK.ObjectOperation
{
public class DeleteObjectsExample
{
public static async Task DeleteObjects()
{
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);
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest()
{
BucketName = bucketName,
Quiet = false
};
deleteObjectsRequest.AddKey("<object-key1>");
deleteObjectsRequest.AddKey("<object-key2>");
?
var result = await s3Client.DeleteObjectsAsync(deleteObjectsRequest);
Console.WriteLine("HttpStatusCode:{0}, {1}", (int) result.HttpStatusCode, result.HttpStatusCode);
//打印刪除成功的對象信息
foreach (var deletedObject in result.DeletedObjects)
{
Console.WriteLine("Key:{0}, DeleteMarker:{1}, VersionId:{2}, DeleteMarkerVersionId:{3}.", deletedObject.Key, deletedObject.DeleteMarker, deletedObject.VersionId,
deletedObject.DeleteMarkerVersionId);
}
?
//打印刪除失敗的對象信息
foreach (var deleteError in result.DeleteErrors)
{
Console.WriteLine("Code:{0}, Key:{1}, Message:{2}, VersionId:{3}", deleteError.Code, deleteError.Key, deleteError.Message, deleteError.VersionId);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}請求參數
DeleteObjects可設置的參數如下:
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| BucketName | string | 桶的名稱 | 是 |
| Quiet | bool | 設置為ture表示采用返回結果為quiet response模式,設置為false表示返回結果為verbose response模式 | 否 |
返回結果
DeleteObjects返回的結果如下:
| 參數 | 類型 | 說明 |
|---|---|---|
| Deleted | List<DeletedObject> | 被刪除對象信息的數組,數組中每一項包含了一個被成功刪除的對象的信息,包括刪除標記、對象名稱和版本等信息 |
| Errors | List<DeleteError> | 刪除失敗的對象信息的數組,數組中每一項包含了一個刪除失敗的對象的信息,包括錯誤碼、對象名稱和版本等信息 |
獲取對象元數據
功能說明
GetObjectMetadata操作用于獲取對象的元數據信息。執行HeadObject操作需要具有對該對象的READ權限。GetObjectMetadata操作可以用于判斷對象是否存在。
代碼示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
?
namespace DotNetSDK.ObjectOperation
{
public class GetObjectMetadataExample
{
public static async Task GetObjectMetadata()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
var key = "<your-object-key>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var getObjectMetadataRequest = new GetObjectMetadataRequest()
{
BucketName = bucketName,
Key = key
};
?
var result = await s3Client.GetObjectMetadataAsync(getObjectMetadataRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to get metadata of object {0}, HttpStatusCode:{1}, ErrorCode:{2}.", key, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
?
Console.WriteLine("get object metadata response: {0}", (int) result.HttpStatusCode);
Console.WriteLine("object ETag:{0}, object versionId:{1}", result.ETag, result.VersionId);
Console.WriteLine("object storage class:{0}", result.StorageClass);
Console.WriteLine("object content length:{0}", result.ContentLength);
Console.WriteLine("object last modified time:{0}", result.LastModified);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}請求參數
GetObjectMetadata可設置的參數如下:
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| BucketName | string | 桶的名稱 | 是 |
| EtagToMatch | string | 用于指定只有在對象的ETag和該參數值匹配的情況下才返回對象數據,否則返回412錯誤碼 | 否 |
| ModifiedSinceDateUtc | DateTime | 用于只有當對象在指定時間后被修改的情況下才返回該對象,否則返回304錯誤碼 | 否 |
| EtagToNotMatch | string | 用于指定只有在對象的ETag和該參數值不匹配的情況下才返回對象數據,否則返回304錯誤碼 | 否 |
| UnmodifiedSinceDateUtc | DateTime | 用于僅當對象自指定時間以來未被修改的情況下才返回對象數據,否則返回412錯誤碼 | 否 |
| Key | string | 對象的key | 是 |
| PartNumber | int | 讀取對象指定的分片,該參數大于等于1,小于等于10000 | 否 |
| VersionId | string | 當桶開啟版本控制的時候,用于指定獲取指定版本的對象數據,當不指定該參數的時候,默認獲取最新版本的對象數據 | 否 |
返回結果
GetObjectMetadata返回的結果如下:
| 參數 | 類型 | 說明 |
|---|---|---|
| ContentLength | long | 本次請求返回數據的大小(單位:字節) |
| ETag | string | 對象的Entity Ttag |
| LastModified | DateTime | 最近一次修改對象的時間 |
| VersionId | string | 對象最新的版本ID |
| StorageClass | 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.ObjectOperation
{
public class PutObjectACLExample
{
public static async Task PubObjectACL()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
var key = "<your-object-key>";
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,
Key = key,
// 添加一個公共讀權限
CannedACL = S3CannedACL.PublicRead
};
?
var result = await s3Client.PutACLAsync(putACLRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to put ACL to object {0}, HttpStatusCode:{1}, ErrorCode:{2}.", key, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
?
Console.WriteLine("set {0} to object {1}", putACLRequest.CannedACL.Value, key);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}請求參數
PutACL可設置的參數如下:
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| CannedACL | S3CannedACL | 配置對象的預定義的標準ACL信息 | 否 |
| AccessControlList | S3AccessControlList | 配置自定義的ACL信息 | 否 |
| BucketName | string | 桶的名稱 | 是 |
| Key | string | 對象的key | 是 |
| VersionId | string | 設置對象的version id | 否 |
S3CannedACL包含了一組預定義的訪問控制權限,可以應用于對象的訪問權限如下:
| 權限 | 說明 |
|---|---|
| NoACL | 默認訪問權限,對象的所有者擁有FULL_CONTROL權限,其他用戶沒有訪問權限 |
| Private | 對象的所有者擁有FULL_CONTROL權限,其他用戶沒有訪問權限 |
| PublicRead | 對象的所有者擁有FULL_CONTROL權限,其他用戶擁有READ權限 |
| PublicReadWrite | 對象的所有者擁有FULL_CONTROL權限,其他用戶擁有READ和WRITE權限 |
| BucketOwnerRead | 對象的所有者擁有FULL_CONTROL權限,桶的所有者擁有READ權限 |
| BucketOwnerFullControl | 對象的所有者和桶的所有者擁有FULL_CONTROL權限 |
獲取對象訪問權限
功能說明
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.ObjectOperation
{
public class GetObjectACLExample
{
public static async Task GetObjectACL()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
var key = "<your-object-key>";
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,
Key = key
};
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 | 桶的名稱 | 是 |
| Key | string | 對象的key | 是 |
| VersionId | string | 設置對象的版本 | 否 |
返回結果
GetACL返回的結果如下:
| 屬性名 | 類型 | 說明 |
|---|---|---|
| Grants | List<S3Grant> | Grant信息的數組,包含了每項授權和被授予人的信息 |
| Owner | Owner | 對象的所有者信息 |
獲取對象標簽
功能說明
GetObjectTagging操作可以查詢對象的標簽信息,對象標簽以key-value的形式設置。桶的所有者默認擁有查詢桶中對象的標簽的權限,并且可以將權限授予其他用戶。
代碼示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
?
namespace DotNetSDK.ObjectOperation
{
public class GetObjectTaggingExample
{
public static async Task GetObjectTagging()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
var key = "<your-object-key>";
var credentials = new BasicAWSCredentials(accessKey, secretKey);
try
{
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var getObjectTaggingRequest = new GetObjectTaggingRequest()
{
BucketName = bucketName,
Key = key
};
var result = await s3Client.GetObjectTaggingAsync(getObjectTaggingRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to get tags of object {0}, HttpStatusCode:{1}, ErrorCode:{2}.", key, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
?
Console.WriteLine("the tags of {0} are: ", key);
foreach (var tag in result.Tagging)
{
Console.WriteLine("key={0}, value={1}", tag.Key, tag.Value);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}請求參數
GetObjectTagging可設置的參數如下:
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| BucketName | string | 執行本操作的桶名稱 | 是 |
| Key | string | 想獲取標簽信息的對象的key | 是 |
| VersionId | string | 想獲取標簽信息的對象的版本Id | 否 |
返回結果
GetObjectTagging返回的結果如下:
| 參數 | 類型 | 說明 |
|---|---|---|
| Tagging | List<Tag> | 對象標簽信息數組,數組中的每一項包含了標簽Key和Value的值 |
刪除對象標簽
功能說明
DeleteObjectTagging操作可以刪除一個對象的全部標簽信息,刪除時可以指定version id參數刪除指定版本對象的標簽信息,如果不設置version id,則默認刪除當前版本的對象標簽信息。
代碼示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
?
namespace DotNetSDK.ObjectOperation
{
public class DeleteObjectTaggingExample
{
public static async Task DeleteObjectTagging()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
var key = "<your-object-key>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var deleteObjectTaggingRequest = new DeleteObjectTaggingRequest()
{
BucketName = bucketName,
Key = key
};
var result = await s3Client.DeleteObjectTaggingAsync(deleteObjectTaggingRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.NoContent)
{
Console.WriteLine("fail to delete tags of object {0}, HttpStatusCode:{1}, ErrorCode:{2}.", key, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
?
Console.WriteLine("deleted the tags of object {0} ", key);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}請求參數
DeleteObjectTagging可設置的參數如下:
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| BucketName | string | 執行本操作的桶名稱 | 是 |
| Key | string | 被刪除標簽信息的對象的key | 是 |
| VersionId | string | 被刪除標簽信息的對象的version id | 否 |
設置對象標簽
功能說明
PutObjectTagging操作可以為對象設置標簽,對象標簽以key-value的形式設置,每個對象最多可以有10個標簽。桶的所有者默認擁有給桶中的對象設置標簽的權限,并且可以將權限授予其他用戶。
代碼示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
?
namespace DotNetSDK.ObjectOperation
{
public class PutObjectTaggingExample
{
public static async Task PutObjectTagging()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
var key = "<your-object-key>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var putObjectTaggingRequest = new PutObjectTaggingRequest()
{
BucketName = bucketName,
Key = key,
};
putObjectTaggingRequest.Tagging.TagSet.Add(new Tag()
{
Key = "<key1>",
Value = "<value1>"
});
putObjectTaggingRequest.Tagging.TagSet.Add(new Tag()
{
Key = "<key2>",
Value = "<value2>"
});
var result = await s3Client.PutObjectTaggingAsync(putObjectTaggingRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to put tags of object {0}, HttpStatusCode:{1}, ErrorCode:{2}.", key, (int) result.HttpStatusCode, result.HttpStatusCode);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}請求參數
PutObjectTagging可設置的參數如下:
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| BucketName | string | 設置標簽信息的對象所在桶的名稱 | 是 |
| Key | string | 設置標簽信息的對象的key | 是 |
| VersionId | string | 設置標簽信息的對象的version id | 否 |
生成下載預簽名URL
功能說明
PutObjectTagging操作可以為對象設置標簽,對象標簽以key-value的形式設置,每個對象最多可以有10個標簽。桶的所有者默認擁有給桶中的對象設置標簽的權限,并且可以將權限授予其他用戶。
代碼示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
?
namespace DotNetSDK.ObjectOperation
{
public class PutObjectTaggingExample
{
public static async Task PutObjectTagging()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
var key = "<your-object-key>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var putObjectTaggingRequest = new PutObjectTaggingRequest()
{
BucketName = bucketName,
Key = key,
};
putObjectTaggingRequest.Tagging.TagSet.Add(new Tag()
{
Key = "<key1>",
Value = "<value1>"
});
putObjectTaggingRequest.Tagging.TagSet.Add(new Tag()
{
Key = "<key2>",
Value = "<value2>"
});
var result = await s3Client.PutObjectTaggingAsync(putObjectTaggingRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to put tags of object {0}, HttpStatusCode:{1}, ErrorCode:{2}.", key, (int) result.HttpStatusCode, result.HttpStatusCode);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}請求參數
PutObjectTagging可設置的參數如下:
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| BucketName | string | 設置標簽信息的對象所在桶的名稱 | 是 |
| Key | string | 設置標簽信息的對象的key | 是 |
| VersionId | string | 設置標簽信息的對象的version id | 否 |
生成上傳預簽名URL
功能說明
GetPreSignedURL接口為一個指定對象生成一個預簽名的上傳鏈接,訪問該鏈接可以直接上傳對象。
代碼示例
生成上傳預簽名URL:
public string GeneratePutUrl()
{
Console.Out.WriteLine("generateUploadUrl");
?
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
BucketName = "<your-bucket-name>",
Key = "<your-object-key>",
Expires = DateTime.Now.AddMinutes(5),
Protocol = Protocol.HTTP, //Protocol.HTTP or Protocol.HTTPS
Verb = HttpVerb.PUT, // 設置 HTTP 方法為 PUT,用于上傳
ContentType = "application/octet-stream" // 指定 Content-Type
};
?
string url = this.s3Client.GetPreSignedURL(request);
Console.Out.WriteLine("generateUploadUrl: {0}", url);
return url;
}通過該預簽名URL,可以直接將文件上傳到指定的桶:
public void PutObjUsingPresignedUrl(string url, string filePath)
{
try
{
using (WebClient client = new WebClient())
{
client.Headers.Add("Content-Type", "application/octet-stream");
client.UploadFile(url, "PUT", filePath);
}
Console.WriteLine("Upload successful. File uploaded from: " + filePath);
}
catch (Exception ex)
{
Console.WriteLine("Upload failed: " + ex.ToString());
}
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| BucketName | string | 桶的名稱 | 是 |
| Key | string | 對象的key | 是 |
| Expires | DateTime | 超時的時間戳 | 否 |
| Protocol | Protocol | 指定生成的URL使用http協議還是https協議 | 否 |
| Verb | HttpVerb | HTTP 方法,設置為 PUT 以生成上傳 URL,默認為GET | 否 |
| ContentType | String | 對象的ContentType | 否。若生成預簽名URL時指定了,則通過預簽名鏈接上傳時也需要指定為相同的ContentType |
返回結果
生成對應的預簽名上傳 URL,該鏈接允許用戶在指定的時間內直接將對象上傳到媒體存儲存儲桶。
服務端加密
功能說明
上傳對象時可以指定對象的加密算法,即使設置桶的加密配置也可以加密請求上傳的對象數據,服務端根據指定的加密算法對對象數據進行加密。目前支持AES256和國密SM4加密算法。
代碼示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
?
namespace DotNetSDK.ObjectOperation
{
public class PutObjectWithEncryptionExample
{
public static async Task PutObjectWithEncryption()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
var key = "<your-object-key>";
var filePath = "<file-path>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint,
UseHttp = false
};
var s3Client = new AmazonS3Client(credentials, conf);
var putObjectRequest = new PutObjectRequest()
{
BucketName = bucketName,
Key = key,
FilePath = filePath,
ServerSideEncryptionMethod = new ServerSideEncryptionMethod("AES256")
};
?
var result = await s3Client.PutObjectAsync(putObjectRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to put object {0}, HttpStatusCode:{1}, ErrorCode:{2}.", key, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
Console.WriteLine("object ETag:{0}, object versionId:{1}", result.ETag, result.VersionId);
Console.WriteLine("encryption method:{0}", result.ServerSideEncryptionMethod.Value);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}請求參數
加密參數說明如下:
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| ServerSideEncryptionMethod | ServerSideEncryptionMethod | 指定服務端采用的加密算法,如AES256、SM4 | 否 |
返回結果
PutObject返回的結果如下:
| 參數 | 類型 | 說明 |
|---|---|---|
| ETag | string | 上傳對象后的對應的Entity Tag |
| VersionId | string | 上傳對象后相應的版本id |
| ServerSideEncryptionMethod | ServerSideEncryptionMethod | 返回對象數據加密的算法名稱 |
獲取多版本對象列表
功能說明
ListVersions操作可以獲取關于對象版本信息的元數據,執行該操作需要對桶有READ權限。
代碼示例
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
?
namespace DotNetSDK.ObjectOperation
{
public class ListVersionsExample
{
public static async Task ListVersions()
{
var accessKey = "<your-access-key>";
var secretKey = "<your-secret-access-key>";
var endpoint = "<your-endpoint>";
var bucketName = "<your-bucket-name>";
var key = "<your-object-key>";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var listVersionsRequest = new ListVersionsRequest()
{
BucketName = bucketName,
Prefix = key
};
var result = await s3Client.ListVersionsAsync(listVersionsRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to list versions, HttpStatusCode:{0}, ErrorCode:{1}.", (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
?
foreach (var s3ObjectVersion in result.Versions)
{
Console.WriteLine("key: {0}, versionId: {1}.", s3ObjectVersion.Key, s3ObjectVersion.VersionId);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}請求參數
ListVersions可以設置的參數如下:
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| BucketName | string | 查詢版本信息的對象所在的桶的名稱 | 是 |
| Delimiter | string | 與Prefix參數一起用于對對象key進行分組的字符。所有key包含指定的Prefix且第一次出現Delimiter字符之間的對象作為一組。如果沒有指定Prefix參數,按Delimiter對所有對象key進行分割,多個對象分割后從對象key開始到第一個Delimiter之間相同的部分形成一組 | 否 |
| KeyMarker | string | 指定一個標識符,返回的對象的key將是按照字典順序排序后位于該標識符之后的所有對象 | 否 |
| MaxKeys | int | 設置response中返回對象key的數量,默認值和最大值均為1000 | 否 |
| Prefix | string | 限定返回對象的key必須以Prefix作為前綴 | 否 |
返回結果
ListVersions返回的結果如下:
| 屬性名 | 類型 | 說明 |
|---|---|---|
| CommonPrefixes | List<string> | 當請求中設置了Delimiter和Prefix屬性時,所有包含指定的Prefix且第一次出現Delimiter字符的對象key作為一組 |
| Delimiter | string | 與請求中設置的Delimiter一致 |
| IsTruncated | bool | 當為false時表示返回結果中包含了全部符合本次請求查詢條件的對象版本信息,否則只返回了MaxKeys個對象版本信息 |
| KeyMarker | string | 與請求中設置的KeyMarker一致 |
| MaxKeys | int | 本次返回結果中包含的對象版本信息數量的最大值 |
| Name | string | 執行本操作的桶名稱 |
| NextKeyMarker | string | 當返回結果中的IsTruncated為true時,可以使用NextKeyMarker作為下次查詢請求中的KeyMarker,繼續查詢出下一部分的對象版本信息 |
| NextVersionIdMarker | string | 本次查詢返回的最后一個的對象version id |
| Prefix | string | 與請求中設置的Prefix一致 |
| Versions | List<S3ObjectVersion> | 對象版本信息的數組,數組中每一項包含了對象的Entity Tag、是否為最新版本以及DeleteMarker、對象key、最新修改時間、擁有者、大小、存儲類型和對象版本的信息 |