本节介绍如何快速使用OSS C++ SDK完成常见操作,如创建存储空间(Bucket)、上传文件、下载文件(Object)等。
说明 示例代码中的conf为ClientConfiguration的实例,ClientConfiguration是OssClient的配置类,您可以通过此配置类来配置代理、连接超时、最大连接数等参数。更多信息,请参见
初始化。
创建存储空间
存储空间是OSS的全局命名空间,相当于数据的容器,可以存储若干文件。
以下代码用于创建examplebucket存储空间。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main( void ) { /* 初始化OSS账号信息 */ std::string AccessKeyId = "<var class="keyword varname" id="varname-lsy-b0z-iwf">yourAccessKeyId</var>"; std::string AccessKeySecret = "<var class="keyword varname" id="varname-n0u-lhp-02t">yourAccessKeySecret</var>"; std::string Endpoint = "<var class="keyword varname" id="varname-495-hxv-f0t">yourEndpoint</var>"; /* 填写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。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main( void ) { /* 初始化OSS账号信息 */ std::string AccessKeyId = "<var class="keyword varname" id="varname-ux6-fhr-o6d">yourAccessKeyId</var>"; std::string AccessKeySecret = "<var class="keyword varname" id="varname-5xm-7vw-mad">yourAccessKeySecret</var>"; std::string Endpoint = "<var class="keyword varname" id="varname-30j-i58-gbi">yourEndpoint</var>"; /* 填写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文件下载到本地文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main( void ) { /* 初始化OSS账号信息 */ std::string AccessKeyId = "<var class="keyword varname" id="varname-fge-6fo-k3y">yourAccessKeyId</var>"; std::string AccessKeySecret = "<var class="keyword varname" id="varname-04i-dyf-fjw">yourAccessKeySecret</var>"; std::string Endpoint = "<var class="keyword varname" id="varname-2g0-tqm-hj0">yourEndpoint</var>"; /* 填写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个文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main( void ) { /* 初始化OSS账号信息 */ std::string AccessKeyId = "<var class="keyword varname" id="varname-jdk-ofg-m3t">yourAccessKeyId</var>"; std::string AccessKeySecret = "<var class="keyword varname" id="varname-40w-kaj-216">yourAccessKeySecret</var>"; std::string Endpoint = "<var class="keyword varname" id="varname-cg8-y6g-twc">yourEndpoint</var>"; /* 填写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 ; } |
删除文件
以下代码用于删除指定文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main( void ) { /* 初始化OSS账号信息 */ std::string AccessKeyId = "<var class="keyword varname" id="varname-tuu-780-6fo">yourAccessKeyId</var>"; std::string AccessKeySecret = "<var class="keyword varname" id="varname-13a-va0-84z">yourAccessKeySecret</var>"; std::string Endpoint = "<var class="keyword varname" id="varname-xm7-l8h-q45">yourEndpoint</var>"; /* 填写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 ; } |
关于删除文件的更多信息,请参见管理文件中的删除文件。