快速入门
本节介绍如何快速使用OSS C++ SDK完成常见操作,如创建存储空间(Bucket)、上传/下载文件(Object)等。
创建存储空间
存储空间是OSS全局命名空间,相当于数据的容器,可以存储若干文件。 以下代码用于新建一个存储空间:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS账号信息 */
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
std::string Endpoint = "yourEndpoint";
std::string BucketName = "yourBucketName";
/* 初始化网络等资源 */
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
/* 指定新创建bucket的名称、存储类型和ACL */
CreateBucketRequest request(BucketName, StorageClass::IA, CannedAccessControlList::PublicReadWrite);
/* 创建bucket */
auto outcome = client.CreateBucket(request);
if (!outcome.isSuccess()) {
/* 异常处理 */
std::cout << "CreateBucket fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
}
/* 释放网络等资源 */
ShutdownSdk();
return 0;
}
存储空间的命名规范,请参见基本概念中的命名规范。创建存储空间详情,请参见创建存储空间。
获取endpoint信息,请参见访问域名和数据中心文档。
上传文件
以下代码用于上传文件至OSS:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS账号信息 */
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
std::string Endpoint = "yourEndpoint";
std::string BucketName = "yourBucketName";
/* yourObjectName表示上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg */
std::string ObjectName = "yourObjectName";
/* 初始化网络等资源 */
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
/* 上传文件 */
/* yourLocalFilename由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。
auto outcome = client.PutObject(BucketName, ObjectName,"yourLocalFilename");
if (!outcome.isSuccess()) {
/* 异常处理 */
std::cout << "PutObject fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
}
/* 释放网络等资源 */
ShutdownSdk();
return 0;
}
下载文件
以下代码用于将指定的OSS文件下载到本地文件:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS账号信息 */
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
std::string Endpoint = "yourEndpoint";
std::string BucketName = "yourBucketName";
/* yourObjectName表示从OSS下载文件时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg */
std::string ObjectName = "yourObjectName";
/* yourFileName由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt */
std::string FileNametoSave = "yourFileName";
/* 初始化网络等资源 */
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
/* 获取文件到本地文件 */
GetObjectRequest request(BucketName, ObjectName);
request.setResponseStreamFactory([=]() {return std::make_shared<std::fstream>(FileNametoSave, std::ios_base::out | std::ios_base::in | std::ios_base::trunc| std::ios_base::binary); });
auto outcome = client.GetObject(request);
if (outcome.isSuccess()) {
std::cout << "GetObjectToFile success, ContentLength is" << outcome.result().Metadata().ContentLength() << std::endl;
}
else {
/* 异常处理 */
std::cout << "GetObjectToFile fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
}
/* 释放网络等资源 */
ShutdownSdk();
return 0;
}
列举文件
以下代码用于列举指定存储空间下的文件。默认列举100个文件。
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS账号信息 */
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
std::string Endpoint = "yourEndpoint";
std::string BucketName = "yourBucketName";
/* 初始化网络等资源 */
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
/* 列举文件 */
ListObjectsRequest request(BucketName);
auto outcome = client.ListObjects(request);
if (!outcome.isSuccess()) {
/* 异常处理 */
std::cout << "ListObjects fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
}
else {
for (const auto& object : outcome.result().ObjectSummarys()) {
std::cout << "object"<<
",name:" << object.Key() <<
",size:" << object.Size() <<
",last modified time:" << object.LastModified() << std::endl;
}
}
/* 释放网络等资源 */
ShutdownSdk();
return 0;
}
删除文件
以下代码用于删除指定文件:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS账号信息 */
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
std::string Endpoint = "yourEndpoint";
std::string BucketName = "yourBucketName";
/* yourObjectName表示删除OSS文件时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg */
std::string ObjectName = "yourObjectName";
/* 初始化网络等资源 */
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
DeleteObjectRequest request(BucketName, ObjectName);
/* 删除文件 */
auto outcome = client.DeleteObject(request);
if (!outcome.isSuccess()) {
/* 异常处理 */
std::cout << "DeleteObject fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
}
/* 释放网络等资源 */
ShutdownSdk();
return 0;
}
删除文件详情请参见管理文件中的删除文件。
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
评论