獲取對象列表
功能說明
您可以使用ListObjects接口獲取某一個桶中的所有對象。
代碼示例
bool S3Demo::ListObjects()
{
Aws::S3::Model::ListObjectsRequest request;
request.WithBucket("<your-bucket-name>");
?
auto outcome = s3_client->ListObjects(request);
if (outcome.IsSuccess()) {
std::cout << "Objects in bucket:" << std::endl;
?
Aws::Vector<Aws::S3::Model::Object> objects =
outcome.GetResult().GetContents();
?
for (Aws::S3::Model::Object& object : objects) {
std::cout << object.GetKey() << std::endl;
}
?
return true;
}
else {
std::cout << "Error: ListObjects: " << outcome.GetError().GetMessage() << std::endl;
return false;
}
}如果 list 大于1000,則返回的結果中 isTruncated 為true,通過返回的 nextMarker 標記可以作為下次讀取的起點。列舉所有對象示例代碼如下:
bool S3Demo::ListObjects2()
{
Aws::S3::Model::ListObjectsRequest request;
request.WithBucket("<your-bucket-name>");
?
Aws::String nextMarker = "";
bool isTruncated = false;
do {
request.WithMarker(nextMarker);
auto outcome = s3_client->ListObjects(request);
if (!outcome.IsSuccess()) {
std::cout << "Error: ListObjects: " << outcome.GetError().GetMessage() << std::endl;
return false;
}
Aws::Vector<Aws::S3::Model::Object> objects = outcome.GetResult().GetContents();
for (Aws::S3::Model::Object &object : objects) {
std::cout << object.GetKey() << std::endl;
}
nextMarker = outcome.GetResult().GetNextMarker();
isTruncated = outcome.GetResult().GetIsTruncated();
} while (isTruncated);
return true;
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| Bucket | string | 桶名稱 | 是 |
| MaxKeys | int | 設置響應中返回的最大鍵數。默認值和可設置最大值均為1000 | 否 |
| Prefix | string | 指定列出對象的鍵名需要包含的前綴 | 否 |
| Marker | string | 用于在某一個具體的鍵名后列出對象,可指定存儲桶中的任一個鍵名 | 否 |
返回結果
| 參數 | 類型 | 說明 |
|---|---|---|
| Contents | Object數組 | 對象列表 |
上傳對象
功能說明
您可以使用PutObject接口上傳對象。
代碼示例
bool S3Demo::PutObject()
{
const Aws::String file_name = "<file-path>";
const Aws::String object_name = "<your-object-key>";
const Aws::String bucket_name = "<your-bucket-name>";
// Verify that the file exists.
struct stat buffer;
if (stat(file_name.c_str(), &buffer) == -1)
{
std::cout << "Error: PutObject: File '" << object_name << "' does not exist." << std::endl;
return false;
}
?
Aws::S3::Model::PutObjectRequest request;
request.SetBucket(bucket_name);
request.SetKey(object_name);
request.SetACL(Aws::S3::Model::ObjectCannedACL::public_read); // 設置ACL
?
std::shared_ptr<Aws::IOStream> input_data =
Aws::MakeShared<Aws::FStream>("SampleAllocationTag",
file_name.c_str(),
std::ios_base::in | std::ios_base::binary);
?
request.SetBody(input_data);
?
Aws::S3::Model::PutObjectOutcome outcome = s3_client->PutObject(request);
if (outcome.IsSuccess()) {
std::cout << "Added object '" << object_name << "' to bucket '"
<< bucket_name << "'.";
return true;
}
else
{
std::cout << "Error: PutObject: " << outcome.GetError().GetMessage() << std::endl;
return false;
}
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| Bucket | string | 桶名 | 是 |
| Key | string | 對象名 | 是 |
| Body | IOStream | 要上傳的數據流對象 | 是 |
| ACL | ObjectCannedACL | 對象訪問權限,取值private | public-read | public-read-write | 否 |
| Metadata | Map | 自定義元數據 | 否 |
返回結果
| 參數 | 類型 | 說明 |
|---|---|---|
| ETag | string | 對象的唯一標簽 |
注意:PutObject對文件大小有限制,最大能上傳5GB大小的文件,超過5GB需要使用分片上傳。
下載對象
功能說明
您可以使用GetObject接口獲取指定桶中的指定對象的內容。
代碼示例
bool S3Demo::GetObject()
{
const Aws::String object_name = "<your-object-key>";
Aws::S3::Model::GetObjectRequest object_request;
object_request.SetBucket("<your-bucket-name>");
object_request.SetKey(object_name);
?
Aws::S3::Model::GetObjectOutcome get_object_outcome = s3_client->GetObject(object_request);
if (get_object_outcome.IsSuccess()) {
auto& retrieved_file = get_object_outcome.GetResultWithOwnership().GetBody();
?
// Print a beginning portion of the text file.
std::cout << "Beginning of file contents:\n";
char file_data[255] = { 0 };
retrieved_file.getline(file_data, 254);
std::cout << file_data << std::endl;
return true;
}
else {
auto err = get_object_outcome.GetError();
std::cout << "Error: GetObject: " <<
err.GetExceptionName() << ": " << err.GetMessage() << std::endl;
return false;
}
}請求參數
| 參數 | 類型 | 說明 | 是否必須 |
|---|---|---|---|
| Bucket | string | 桶名 | 是 |
| Key | string | 對象名 | 是 |
返回結果
| 參數 | 類型 | 說明 |
|---|---|---|
| Body | IOStream | 對象數據內容 |
| Metadata | Map | 自定義元數據 |
復制對象
功能說明
您可以使用CopyObject接口拷貝某一個桶中的對象到另外一個指定的桶中。
代碼示例
bool S3Demo::CopyObject()
{
const Aws::String bucket_source = "<source-bucket-name>";
const Aws::String bucket_dest = "<dst-bucket-name>";
const Aws::String object_source = "<source-object-key>";
const Aws::String object_dest = "<dst-object-key>";
?
Aws::S3::Model::CopyObjectRequest request;
request.SetBucket(bucket_dest);
request.SetKey(object_dest);
request.SetCopySource(bucket_source + "/" + object_source); // 注意這個參數
?
Aws::S3::Model::CopyObjectOutcome outcome = s3_client->CopyObject(request);
if (outcome.IsSuccess()) {
std::cout << "CopyObject " << object_source << " to " << object_dest << std::endl;
return true;
}
else {
Aws::S3::S3Error err = outcome.GetError();
std::cout << "Error: CopyObject: " << (int)err.GetResponseCode() << ", Message:" <<
err.GetMessage() << std::endl;
return false;
}
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| Bucket | string | 桶名稱 | 是 |
| Key | string | 目的對象key | 是 |
| CopySource | string | URL格式的拷貝對象數據來源,包含了桶名稱和對象key的信息,二者之間使用正斜杠(/)分割 | 是 |
返回結果
| 參數 | 類型 | 說明 |
|---|---|---|
| ETag | string | 對象的唯一標簽 |
刪除對象
功能說明
您可以使用DeleteObject接口刪除某一個桶中的對象。
代碼示例
bool S3Demo::DeleteObject()
{
const Aws::String object_name = "<your-object-key>";
?
Aws::S3::Model::DeleteObjectRequest request;
request.SetBucket("<your-bucket-name>");
request.SetKey(object_name);
?
Aws::S3::Model::DeleteObjectOutcome outcome = s3_client->DeleteObject(request);
if (outcome.IsSuccess()) {
std::cout << "DeleteObject " << object_name << " success";
return true;
}
else {
Aws::S3::S3Error err = outcome.GetError();
std::cout << "Error: DeleteObject: " << (int)err.GetResponseCode() << ", Message:" <<
err.GetMessage() << std::endl;
return false;
}
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| Bucket | string | 桶名 | 是 |
| Key | string | 對象名 | 是 |
批量刪除對象
功能說明
您可以使用DeleteObjects接口批量刪除多個對象,可以減少發起多個請求去刪除大量對象的花銷。DeleteObjects操作發起一個包含了最多1000個key的刪除請求,媒體存儲服務會對相應的對象逐個進行刪除,并且將刪除成功或者失敗的結果通過response返回。如果請求刪除的對象不存在,會返回已刪除的結果。
DeleteObjects操作返回包含verbose 和quiet兩種response模式。verbose response是默認的返回模式,該模式的返回結果包含了每個key的刪除結果。quiet response返回模式返回的結果僅包含了刪除失敗的key,對于一個完全成功的刪除操作,該返回模式不在相應消息體中返回任何信息。
代碼示例
bool S3Demo::DeleteObjects()
{
Aws::S3::Model::DeleteObjectsRequest request;
request.SetBucket("<your-bucket-name>");
Aws::S3::Model::Delete deleteObject;
deleteObject.AddObjects(Aws::S3::Model::ObjectIdentifier().WithKey("ExampleObject.txt"));
deleteObject.AddObjects(Aws::S3::Model::ObjectIdentifier().WithKey("ExampleObject1.txt"));
request.SetDelete(deleteObject);
?
Aws::S3::Model::DeleteObjectsOutcome outcome = s3_client->DeleteObjects(request);
if (outcome.IsSuccess()) {
std::cout << "DeleteObjects success";
return true;
} else {
Aws::S3::S3Error err = outcome.GetError();
std::cout << "Error: DeleteObjects: " << (int)err.GetResponseCode() << ", Message:" <<
err.GetMessage() << std::endl;
return false;
}
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| Bucket | string | 桶名稱 | 是 |
| Delete | Delete | 要刪除的對象key列表 | 是 |
獲取對象元數據
功能說明
您可以使用HeadObject接口獲取對象元數據信息。
代碼示例
bool S3Demo::HeadObject()
{
const Aws::String object_name = "<your-object-key>";
?
Aws::S3::Model::HeadObjectRequest request;
request.SetBucket("<your-bucket-name>");
request.SetKey(object_name);
?
Aws::S3::Model::HeadObjectOutcome outcome = s3_client->HeadObject(request);
if (outcome.IsSuccess()) {
std::cout << "HeadObject " << object_name << " success";
return true;
}
else {
Aws::S3::S3Error err = outcome.GetError();
std::cout << "Error: HeadObject: " << (int)err.GetResponseCode() << ", Message:" <<
err.GetMessage() << std::endl;
return false;
}
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| Bucket | string | 桶名稱 | 是 |
| Key | string | 對象key | 是 |
設置對象訪問權限
功能說明
媒體存儲支持一組預先定義的授權,稱為Canned ACL。每個Canned ACL都有一組預定義的被授權者和權限,下表列出了相關的預定義授權含義。
| ACL | 權限 | 描述 |
|---|---|---|
| private | 私有讀寫 | 對象擁有者有讀寫權限,其他用戶沒有訪問權限。 |
| public-read | 公共讀私有寫 | 對象擁有者有讀寫權限,其他用戶只有該對象的讀權限。 |
| public-read-write | 公共讀寫 | 所有用戶都有該對象的讀寫權限。 |
| authenticated-read | 注冊用戶可讀 | 對象擁有者有讀寫權限,注冊用戶具有該對象的讀限。 |
PutObjectAcl操作可以為媒體存儲服務中的對象設置ACL。對一個對象執行該操作需要具有WRITE_ACP權限。
代碼示例
bool S3Demo::PutObjectAcl()
{
const Aws::String object_name = "<your-object-key>";
?
Aws::S3::Model::PutObjectAclRequest request;
request.SetBucket("<your-bucket-name>");
request.SetKey(object_name);
request.SetACL(Aws::S3::Model::ObjectCannedACL::public_read);
?
Aws::S3::Model::PutObjectAclOutcome outcome = s3_client->PutObjectAcl(request);
if (outcome.IsSuccess()) {
?
std::cout << "PutObjectAcl " << object_name << " success";
return true;
} else
{
Aws::S3::S3Error err = outcome.GetError();
std::cout << "Error: PutObjectAcl: " << (int)err.GetResponseCode() << ", Message:" <<
err.GetMessage() << std::endl;
?
return false;
}
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| Bucket | string | 桶名稱 | 是 |
| Key | string | 對象key | 是 |
| ACL | ObjectCannedACL | acl值 | 是 |
獲取對象訪問權限
功能說明
您可以使用 GetObjectAcl操作獲取對象的access control list(ACL)信息。
代碼示例
bool S3Demo::GetObjectAcl()
{
const Aws::String object_name = "<your-object-key>";
?
Aws::S3::Model::GetObjectAclRequest request;
request.SetBucket("<your-bucket-name>");
request.SetKey(object_name);
?
Aws::S3::Model::GetObjectAclOutcome outcome = s3_client->GetObjectAcl(request);
if (outcome.IsSuccess()) {
?
Aws::Vector<Aws::S3::Model::Grant> grants = outcome.GetResult().GetGrants();
for (Aws::S3::Model::Grant& grant : grants)
{
std::cout << "Grant:" << grant.GetGrantee().GetDisplayName() << ", permission:" << (int)grant.GetPermission() << std::endl;
}
return true;
}
else
{
Aws::S3::S3Error err = outcome.GetError();
std::cout << "Error: GetObjectAcl: " << (int)err.GetResponseCode() << ", Message:" <<
err.GetMessage() << std::endl;
?
return false;
}
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| Bucket | string | 桶名稱 | 是 |
| Key | string | 對象key | 是 |
返回參數
| 參數 | 類型 | 說明 |
|---|---|---|
| Owner | Owner | 所有者信息 |
| Grants | Grant數組 | 每種類型用戶的詳細權限信息 |
獲取對象標簽
功能說明
您可以使用GetObjectTagging接口獲取對象標簽。
代碼示例
bool S3Demo::GetObjectTagging()
{
const Aws::String object_name = "<your-object-key>";
?
Aws::S3::Model::GetObjectTaggingRequest request;
request.SetBucket("<your-bucket-name>");
request.SetKey(object_name);
?
Aws::S3::Model::GetObjectTaggingOutcome outcome = s3_client->GetObjectTagging(request);
if (outcome.IsSuccess()) {
std::cout << "GetObjectTagging " << object_name << " success";
Aws::Vector<Aws::S3::Model::Tag> tags = outcome.GetResult().GetTagSet();
for (Aws::S3::Model::Tag& tag: tags)
{
std::cout << tag.GetKey() << ":" << tag.GetValue() << std::endl;
}
return true;
} else {
Aws::S3::S3Error err = outcome.GetError();
std::cout << "Error: GetObjectTagging: " << (int)err.GetResponseCode() << ", Message:" <<
err.GetMessage() << std::endl;
return false;
}
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| Bucket | string | 桶名稱 | 是 |
| Key | string | 對象key | 是 |
| VersionId | string | 設置標簽信息的對象的版本Id | 否 |
返回結果
| 參數 | 類型 | 說明 |
|---|---|---|
| TagSet | Tag數組 | 設置的標簽信息,包含了一個Tag結構體的數組,每個Tag以Key-Value的形式說明了標簽的內容 |
刪除對象標簽
功能說明
您可以使用DeleteObjectTagging接口刪除對象標簽。
代碼示例
bool S3Demo::DeleteObjectTagging()
{
const Aws::String object_name = "<your-object-key>";
?
Aws::S3::Model::DeleteObjectTaggingRequest request;
request.SetBucket("<your-bucket-name>");
request.SetKey(object_name);
?
Aws::S3::Model::DeleteObjectTaggingOutcome outcome = s3_client->DeleteObjectTagging(request);
if (outcome.IsSuccess()) {
std::cout << "DeleteObjectTagging " << object_name << " success";
return true;
} else {
Aws::S3::S3Error err = outcome.GetError();
std::cout << "Error: DeleteObjectTagging: " << (int)err.GetResponseCode() << ", Message:" <<
err.GetMessage() << std::endl;
return false;
}
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| Bucket | string | 執行本操作的桶名稱 | 是 |
| Key | string | 設置標簽信息的對象key | 是 |
| VersionId | string | 設置標簽信息的對象的版本Id | 否 |
設置對象標簽
功能說明
您可以使用PutObjectTagging接口為對象設置標簽。bucket的擁有者默認擁有給bucket中的對象設置標簽的權限,并且可以將權限授予其他用戶。每次執行PutObjectTagging操作會覆蓋對象已有的標簽信息。標簽是一個鍵值對,每個對象最多可以設置10個標簽,標簽Key和Value區分大小寫,并且Key不可重復。每個標簽的Key長度不超過128字節,Value長度不超過256字節。SDK通過HTTP header的方式設置標簽且標簽中包含任意字符時,需要對標簽的Key和Value做URL編碼。設置對象標簽信息不會更新對象的最新更改時間。
代碼示例
bool S3Demo::PutObjectTagging()
{
const Aws::String object_name = "<your-object-key>";
?
Aws::S3::Model::PutObjectTaggingRequest request;
request.SetBucket("<your-bucket-name>");
request.SetKey(object_name);
Aws::S3::Model::Tagging tagging;
tagging.AddTagSet(Aws::S3::Model::Tag().WithKey("key1").WithValue("value1"));
tagging.AddTagSet(Aws::S3::Model::Tag().WithKey("key2").WithValue("value2"));
request.SetTagging(tagging);
?
Aws::S3::Model::PutObjectTaggingOutcome outcome = s3_client->PutObjectTagging(request);
if (outcome.IsSuccess()) {
std::cout << "PutObjectTagging " << object_name << " success";
return true;
} else {
Aws::S3::S3Error err = outcome.GetError();
std::cout << "Error: PutObjectTagging: " << (int)err.GetResponseCode() << ", Message:" <<
err.GetMessage() << std::endl;
return false;
}
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| Bucket | string | 桶名稱 | 是 |
| Key | string | 對象key | 是 |
| Tagging | Tagging | 設置的標簽信息,包含了一個Tag結構體的數組,每個Tag以Key-Value的形式說明了標簽的內容 | 是 |
| VersionId | string | 設置標簽信息的對象的版本Id | 否 |
生成下載預簽名URL
功能說明
您可以使用GeneratePresignedUrl接口為一個指定對象生成一個預簽名的下載鏈接,訪問該鏈接可以直接下載該對象。
代碼示例
生成下載預簽名URL:
bool S3Demo::GeneratePresignUrl()
{
const Aws::String object_name = "<your-object-key>";
long expirationInSeconds = 900;
?
Aws::String path = s3_client->GeneratePresignedUrl("<your-bucket-name>", object_name, Aws::Http::HttpMethod::HTTP_GET, expirationInSeconds);
?
std::cout << "GeneratePresignUrl: " << path << std::endl;
return true;
}生成下載對象的預簽名URL后,可以通過該URL下載文件:
void S3Demo::GetObjUsingPresignedUrl(const std::string& url, const std::string& file_path) {
try {
std::ofstream file(file_path, std::ios::binary);
if (!file.is_open()) {
std::cerr << "Failed to open file for writing: " << file_path << std::endl;
return;
}
?
curlpp::Easy request;
request.setOpt(curlpp::options::Url(url));
request.setOpt(curlpp::options::WriteStream(&file));
request.perform();
?
std::cout << "Download successful: " << file_path << std::endl;
} catch (curlpp::RuntimeError& e) {
std::cerr << "Runtime Error: " << e.what() << std::endl;
} catch (curlpp::LogicError& e) {
std::cerr << "Logic Error: " << e.what() << std::endl;
}
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| Bucket | string | 桶名稱 | 是 |
| Key | string | 對象key | 是 |
| method | HttpMethod | http請求方法,HTTP_GET表示下載,HTTP_PUT表示上傳 | 是 |
| expirationInSeconds | long long | 超時時間(秒) | 否,默認7天 |
返回結果
生成對應的預簽名下載 URL,該鏈接允許用戶在指定的時間內直接從媒體存儲下載對象。
生成上傳預簽名URL
功能說明
您可以使用GeneratePresignedUrl接口為一個指定對象生成一個預簽名的上傳鏈接,訪問該鏈接可以直接上傳該對象。
代碼示例
生成上傳預簽名URL:
bool S3Demo::GeneratePutPresignUrl() {
const Aws::String object_name = "ExampleObject.txt";
long expirationInSeconds = 900;
?
Aws::String path = s3_client->GeneratePresignedUrl(s3_bucket_name, object_name, Aws::Http::HttpMethod::HTTP_PUT, expirationInSeconds);
?
std::cout << "GeneratePutPresignUrl: " << path << std::endl;
return true;
}通過該預簽名URL,可以直接將文件上傳到指定的桶:
void S3Demo::PutObjUsingPresignedUrl(const std::string& url, const std::string& file_path) {
try {
std::ifstream file(file_path, std::ios::binary);
if (!file.is_open()) {
std::cerr << "Failed to open file for reading: " << file_path << std::endl;
return;
}
?
// 獲取文件大小
file.seekg(0, std::ios::end);
std::streamsize file_size = file.tellg();
file.seekg(0, std::ios::beg);
?
// 使用 curlpp 進行 PUT 上傳
curlpp::Easy request;
request.setOpt(curlpp::options::Url(url));
request.setOpt(curlpp::options::Upload(true));
request.setOpt(curlpp::options::InfileSize(file_size));
request.setOpt(curlpp::options::ReadStream(&file));
request.perform();
?
std::cout << "Upload successful: " << file_path << std::endl;
} catch (curlpp::RuntimeError& e) {
std::cerr << "Runtime Error: " << e.what() << std::endl;
} catch (curlpp::LogicError& e) {
std::cerr << "Logic Error: " << e.what() << std::endl;
}
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| Bucket | string | 桶名稱 | 是 |
| Key | string | 對象key | 是 |
| method | HttpMethod | http請求方法,HTTP_GET表示下載,HTTP_PUT表示上傳 | 是 |
| expirationInSeconds | long long | 超時時間(秒) | 否,默認7天 |
返回結果
生成對應的預簽名上傳 URL,該鏈接允許用戶在指定的時間內直接將對象上傳到媒體存儲存儲桶。
追加寫
功能說明
AppendObject可以對桶中的一個對象進行追加寫操作,如果該對象已經存在,執行該操作則向文件末尾追加內容,否則將創建對象。
通過Append操作創建的Object類型為Appendable,而通過PutObject操作上傳的Object的類型是Normal。對Appendable類型的對象進行普通上傳操作之后會覆蓋原有對象的內容并且將其類型設置為Normal。
Append操作僅可以在未開啟版本控制的桶中執行,如果桶的版本控制狀態為啟用(Enabled)或者暫停(Suspended)狀態將不支持Append操作。
代碼示例
bool S3Demo::AppendObject()
{
const Aws::String file_name = "<file-path>";
const Aws::String object_name = "<your-object-key>";
const Aws::String bucket_name = "<your-bucket-name>";
// Verify that the file exists.
struct stat buffer;
if (stat(file_name.c_str(), &buffer) == -1) {
std::cout << "Error: PutObject: File '" << object_name << "' does not exist." << std::endl;
return false;
}
auto appPos = 0;
?
Aws::S3::Model::PutObjectRequest request;
request.SetBucket(bucket_name);
request.SetKey(object_name);
request.SetACL(Aws::S3::Model::ObjectCannedACL::public_read_write); // 設置ACL
request.SetAppend(true); // 設置追加模式上傳Object
request.SetAppendPosition(appPos); // 指定追加位置
?
std::shared_ptr<Aws::IOStream> input_data =
Aws::MakeShared<Aws::FStream>("SampleAllocationTag",
file_name.c_str(),
std::ios_base::in | std::ios_base::binary);
std::shared_ptr<Aws::IOStream> append_data =
Aws::MakeShared<Aws::FStream>("SampleAllocationTag",
file_name.c_str(),
std::ios_base::in | std::ios_base::binary);
?
std::cout<<"首次寫入Pos: " << appPos << std::endl;
request.SetBody(input_data);
Aws::S3::Model::PutObjectOutcome outcome = s3_client->PutObject(request);
if (!outcome.IsSuccess())
{
std::cout << "ERROR: PutObject: " << "Error Msg: " << outcome.GetError().GetMessage() << std::endl;
return false;
}
else
{
appPos = outcome.GetResult().GetAppendPosition(); // 追加模式下,獲取下一次追加的起始位置
std::cout<<"追加寫入Pos: " << appPos << std::endl;
// 追加寫對象
request.SetAppend(true);
request.SetAppendPosition(appPos);
request.SetBody(append_data);
?
outcome = s3_client->PutObject(request);
result = outcome.GetResult();
?
std::cout << "Etag:" << outcome.GetResult().GetETag() << std::endl;
return true;
?
}
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| Bucket | string | 桶名 | 是 |
| Key | string | 對象名 | 是 |
| Body | IOStream | 要上傳的數據流對象 | 是 |
| Append | bool | 是否為Appendable類型 | 是 |
| AppendPosition | int | 追加前對象大小 | 是 |
| ACL | ObjectCannedACL | 對象訪問權限,取值private | public-read | public-read-write | 否 |
| Metadata | Map | 自定義元數據 | 否 |
返回結果
| 參數 | 類型 | 說明 |
|---|---|---|
| ETag | string | 對象的唯一標簽 |
注意:對Appendable類型的對象進行普通上傳操作會將其類型設置為Normal,但無法對Normal類型的對象進行追加上傳操作將其類型設置為Appendable類型。
獲取多版本對象列表
功能說明
如果桶開啟了版本控制,您可以使用 ListObjectVersions接口列舉對象的版本,每次list操作最多返回1000個對象。
代碼示例
bool S3Demo::ListObjectVersions()
{
Aws::S3::Model::ListObjectVersionsRequest request;
request.WithBucket("<your-bucket-name>");
?
Aws::S3::Model::ListObjectVersionsOutcome outcome = s3_client->ListObjectVersions(request);
if (outcome.IsSuccess()) {
std::cout << "Objects in bucket:" << std::endl;
?
Aws::Vector<Aws::S3::Model::ObjectVersion> objects =
outcome.GetResult().GetVersions();
?
for (Aws::S3::Model::ObjectVersion& object : objects) {
std::cout << object.GetKey() << std::endl;
}
?
return true;
} else {
std::cout << "Error: ListObjectVersions: " << outcome.GetError().GetMessage() << std::endl;
return false;
}
}請求參數
| 參數 | 類型 | 說明 | 是否必要 |
|---|---|---|---|
| Bucket | string | 桶名稱 | 是 |
| MaxKeys | int | 設置響應中返回的最大鍵數。默認值和可設置最大值均為1000 | 否 |
| Prefix | string | 指定列出對象的鍵名需要包含的前綴 | 否 |
| Marker | string | 用于在某一個具體的鍵名后列出對象,可指定存儲桶中的任一個鍵名 | 否 |
返回結果
| 參數 | 類型 | 說明 |
|---|---|---|
| Versions | ObjectVersion數組 | 對象列表 |