获取对象列表
功能说明
您可以使用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数组 | 对象列表 |