本节介绍如何快速使用OSS C++ SDK完成常见操作,如创建存储空间(Bucket)、上传文件、下载文件(Object)等。
说明 示例代码中的conf为ClientConfiguration的实例,ClientConfiguration是OssClient的配置类,您可以通过此配置类来配置代理、连接超时、最大连接数等参数。更多信息,请参见
初始化。
创建存储空间
存储空间是OSS的全局命名空间,相当于数据的容器,可以存储若干文件。
以下代码用于创建examplebucket存储空间。
#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"; /* 填写Bucket名称,例如examplebucket */ std::string BucketName = "examplebucket"; /* 初始化网络等资源 */ 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; }
关于创建存储空间的更多信息,请参见控制台用户指南中的创建存储空间。
上传文件
以下代码用于上传文件到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"; /* 填写Bucket名称,例如examplebucket */ std::string BucketName = "examplebucket"; /* 填写文件完整路径,例如exampledir/exampleobject.txt。文件完整路径中不能包含Bucket名称 */ std::string ObjectName = "exampledir/exampleobject.txt"; /* 初始化网络等资源 */ InitializeSdk(); ClientConfiguration conf; OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf); /* 上传文件 */ /* 填写本地文件完整路径,例如D:\\localpath\\examplefile.txt,其中localpath为本地文件examplefile.txt所在本地路径 */ auto outcome = client.PutObject(BucketName, ObjectName,"D:\\localpath\\examplefile.txt"); 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"; /* 填写Bucket名称,例如examplebucket */ std::string BucketName = "examplebucket"; /* 填写文件完整路径,例如exampledir/exampleobject.txt。文件完整路径中不能包含Bucket名称 */ std::string ObjectName = "exampledir/exampleobject.txt"; /* 填写本地文件完整路径,例如D:\\localpath\\examplefile.txt,其中localpath为本地文件examplefile.txt所在本地路径 */ std::string FileNametoSave = "D:\\localpath\\examplefile.txt"; /* 初始化网络等资源 */ 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"; /* 填写Bucket名称,例如examplebucket */ std::string BucketName = "examplebucket"; /* 初始化网络等资源 */ 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"; /* 填写Bucket名称,例如examplebucket */ std::string BucketName = "examplebucket"; /* 填写文件完整路径,例如exampledir/exampleobject.txt。文件完整路径中不能包含Bucket名称 */ std::string ObjectName = "exampledir/exampleobject.txt"; /* 初始化网络等资源 */ 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; }
关于删除文件的更多信息,请参见管理文件中的删除文件。