6. 文件管理

在NOS中,用户可以通过一系列的接口管理桶(Bucket)中的文件(Object),比如ListObjects,DeleteObject,CopyObject,DoesObjectExist等。

6.1. 列出桶中的文件

您可以使用S3Client.ListObjects列出存储中间中的文件:

void listObjectsWithPrintKeySize(const Aws::S3::S3Client& s3Client,const Aws::String& bucketName){
    Aws::S3::Model::ListObjectsRequest listObjectRequest;
    listObjectRequest.WithBucket(bucketName);
    auto result = s3Client.ListObjects(listObjectRequest);
    if (result.IsSuccess()) {
        for(auto content : result.GetResult().GetContents()){
            std::cout << content.GetKey() << " --> " << content.GetSize() << std::endl;
        }
    } else {
        std::cout << "list Objects failed" << std::endl;
    }
}

上述代码中的ListObjectsRequest中的可选参数如下所示:

参数 说明
Delimiter 用于对Object名字进行分组的字符。所有名字包含指定的前缀且第一次出现delimiter字符之间的object作为一组元素。限制长度为10。
Prefix 限定返回的object key必须以prefix作为前缀。注意使用prefix查询时,返回的key中仍会包含prefix
MaxKeys 限定此次返回object的最大数,如果不设定,默认为100
Marker 设定结果从marker之后按字母排序的第一个开始返回

Note

  1. 上述表中的参数都是可选参数

6.2. 判断文件是否存在

您可以使用S3Client.GetObjectMetadata的返回值来判断文件是否存在:

bool doesObjectExits(const Aws::S3::S3Client& s3Client,const Aws::String& bucketName,const Aws::String& objectName){
    Aws::S3::Model::HeadObjectRequest headObjectRequest;
    headObjectRequest.WithBucket(bucketName).WithKey(objectName);
    auto result = s3Client.HeadObject(headObjectRequest);
    return result.IsSuccess();
}

6.3. 删除单个文件

您可以使用S3Client.DeleteObject删除单个需要删除的文件:

void deleteObject(const Aws::S3::S3Client& s3Client,const Aws::String& bucketName,const Aws::String& objectName){

    Aws::S3::Model::DeleteObjectRequest object_request;
    object_request.WithBucket(bucketName).WithKey(objectName);

    auto delete_object_outcome = s3Client.DeleteObject(object_request);

    if (delete_object_outcome.IsSuccess())
    {
        std::cout << "Done!" << std::endl;
    }
    else
    {
        std::cout << "DeleteObject error: " <<
        delete_object_outcome.GetError().GetExceptionName() << " " <<
        delete_object_outcome.GetError().GetMessage() << std::endl;
    }
}

6.4. 删除多个文件

您可以使用S3Client.DeleteObjects批量删除文件:

void deleteObjects(const Aws::S3::S3Client& s3Client,const Aws::String& bucketName,const Aws::Vector<Aws::S3::Model::ObjectIdentifier>& objectNames){
    Aws::S3::Model::DeleteObjectsRequest deleteObjectsRequest;
    Aws::S3::Model::Delete deleteObjects;
    deleteObjects.SetObjects(objectNames);
    deleteObjects.SetQuiet(false);
    deleteObjectsRequest.WithBucket(bucketName).WithDelete(deleteObjects);
    auto result = s3Client.DeleteObjects(deleteObjectsRequest);
    if (result.IsSuccess()) {
        for(auto err : result.GetResult().GetErrors()){
            std::cout << "delete err : " << err.GetKey() << " --> " << err.GetMessage() << std::endl;
        }
        for (auto del : result.GetResult().GetDeleted()) {
            std::cout << "delete ok : " << del.GetKey()  << std::endl;
        }
        std::cout << "ok" << std::endl;
    } else {
        std::cout << result.GetError().GetMessage() << std::endl;
        std::cout << "failed" << std::endl;
    }

}

6.5. 拷贝文件

您可以使用S3Client.CopyObject拷贝文件:

void copyObject(const Aws::S3::S3Client& s3Client,const Aws::String& srcBucket,const Aws::String& srcObject,const Aws::String& dstBucket,const Aws::String& dstObject){
    Aws::S3::Model::CopyObjectRequest copyObjectRequest;
    copyObjectRequest.WithBucket(dstBucket).WithKey(dstObject).WithCopySource("/" + srcBucket + "/" + srcObject);
    auto result = s3Client.CopyObject(copyObjectRequest);
    if (result.IsSuccess()) {
        std::cout << "copy done" << std::endl;
    } else {
        std::cout << "copy failed" << std::endl;
    }
}

Attention

  1. 支持跨桶的文件copy

6.6. 获取文件的文件元信息

您可以使用S3Client.GetObjectMetadata获取对象的元数据信息:

void getObjectMeta(const Aws::S3::S3Client& s3Client,const Aws::String& bucketName,const Aws::String& objectName){
    Aws::S3::Model::HeadObjectRequest headObjectRequest;
    headObjectRequest.WithBucket(bucketName).WithKey(objectName);
    auto result = s3Client.HeadObject(headObjectRequest);
    if (result.IsSuccess()) {
        for(auto entry : result.GetResult().GetMetadata()){
            std::cout << entry.first << " -- > " << entry.second << std::endl;
        }
    } else {
        std::cout << "failed" << std::endl;
    }
}

6.7. 生成私有对象可下载的URL链接

您可以使用S3Client.GetPreSignedURL获取对象的下载URL:

void getObjectDonwloadUrl(Aws::S3::S3Client& s3Client,const Aws::String& bucketName,const Aws::String& objectName){
    Aws::String url = s3Client.GeneratePresignedUrl(bucketName, objectName,Aws::Http::HttpMethod::HTTP_GET,20);//最后一个20是过期时间,但是为second
    std::cout << url << std::endl;
}