Coverage Report

Created: 2026-07-13 02:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/io/fs/obj_storage_client.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <optional>
21
22
#include "common/status.h"
23
#include "io/fs/file_system.h"
24
#include "io/fs/path.h"
25
namespace doris {
26
class Status;
27
struct S3ClientConf;
28
namespace io {
29
30
// Names are in lexico order.
31
enum class ObjStorageType : uint8_t {
32
    UNKNOWN = 0,
33
    AWS = 1,
34
    AZURE,
35
    BOS,
36
    COS,
37
    OSS,
38
    OBS,
39
    GCP,
40
    TOS,
41
};
42
43
struct ObjectStoragePathOptions {
44
    Path path = "";
45
    std::string bucket = std::string();                  // blob container in azure
46
    std::string key = std::string();                     // blob name in azure
47
    std::string prefix = std::string();                  // for batch delete and recursive delete
48
    std::optional<std::string> upload_id = std::nullopt; // only used for S3 upload
49
};
50
51
struct ObjectCompleteMultiPart {
52
    int part_num = 0;
53
    std::string etag = std::string();
54
};
55
56
struct ObjectStorageStatus {
57
    int code = 0;
58
    std::string msg = std::string();
59
};
60
61
// We only store error code along with err_msg instead of Status to unify BE and recycler's error handle logic
62
struct ObjectStorageResponse {
63
    ObjectStorageStatus status {};
64
    int http_code {200};
65
    std::string request_id = std::string();
66
2.23k
    static ObjectStorageResponse OK() {
67
        // clang-format off
68
2.23k
        return {
69
2.23k
                .status { .code = 0, },
70
2.23k
                .http_code = 200,
71
2.23k
        };
72
        // clang-format on
73
2.23k
    }
74
};
75
76
struct ObjectStorageUploadResponse {
77
    ObjectStorageResponse resp {};
78
    std::optional<std::string> upload_id = std::nullopt;
79
    std::optional<std::string> etag = std::nullopt;
80
};
81
82
struct ObjectStorageHeadResponse : ObjectStorageResponse {
83
    ObjectStorageResponse resp {};
84
    long long file_size {0};
85
};
86
87
class ObjStorageClient {
88
public:
89
70
    virtual ~ObjStorageClient() = default;
90
    // Create a multi-part upload request. On AWS-compatible systems, it will return an upload ID, but not on Azure.
91
    // The input parameters should include the bucket and key for the object storage.
92
    virtual ObjectStorageUploadResponse create_multipart_upload(
93
            const ObjectStoragePathOptions& opts) = 0;
94
    // To directly upload a piece of data to object storage and generate a user-visible file.
95
    // You need to clearly specify the bucket and key
96
    virtual ObjectStorageResponse put_object(const ObjectStoragePathOptions& opts,
97
                                             std::string_view stream) = 0;
98
    // To upload a part of a large file to object storage as a temporary file, which is not visible to the user
99
    // The temporary file's ID is the value of the part_num passed in
100
    // You need to specify the bucket and key along with the upload_id if it's AWS-compatible system
101
    // For the same bucket and key, as well as the same part_num, it will directly replace the original temporary file.
102
    virtual ObjectStorageUploadResponse upload_part(const ObjectStoragePathOptions& opts,
103
                                                    std::string_view stream, int part_num) = 0;
104
    // To combine the previously uploaded multiple file parts into a complete file, the file name is the name of the key passed in.
105
    // If it is an AWS-compatible system, the upload_id needs to be included.
106
    // After a successful execution, the large file can be accessed in the object storage
107
    virtual ObjectStorageResponse complete_multipart_upload(
108
            const ObjectStoragePathOptions& opts,
109
            const std::vector<ObjectCompleteMultiPart>& completed_parts) = 0;
110
    // According to the passed bucket and key, it will access whether the corresponding file exists in the object storage.
111
    // If it exists, it will return the corresponding file size
112
    virtual ObjectStorageHeadResponse head_object(const ObjectStoragePathOptions& opts) = 0;
113
    // According to the bucket and key, it finds the corresponding file in the object storage
114
    // and starting from the offset, it reads bytes_read bytes into the buffer, with size_return recording the actual number of bytes read
115
    virtual ObjectStorageResponse get_object(const ObjectStoragePathOptions& opts, void* buffer,
116
                                             size_t offset, size_t bytes_read,
117
                                             size_t* size_return) = 0;
118
    // According to the passed bucket and prefix, it traverses and retrieves all files under the prefix, and returns the name and file size of all files.
119
    // **Notice**: The files returned by this function contains the full key in object storage.
120
    virtual ObjectStorageResponse list_objects(const ObjectStoragePathOptions& opts,
121
                                               std::vector<FileInfo>* files) = 0;
122
    // According to the bucket and prefix specified by the user, it performs batch deletion based on the object names in the object array.
123
    virtual ObjectStorageResponse delete_objects(const ObjectStoragePathOptions& opts,
124
                                                 std::vector<std::string> objs) = 0;
125
    // Delete the file named key in the object storage bucket.
126
    virtual ObjectStorageResponse delete_object(const ObjectStoragePathOptions& opts) = 0;
127
    // According to the prefix, recursively delete all files under the prefix.
128
    virtual ObjectStorageResponse delete_objects_recursively(
129
            const ObjectStoragePathOptions& opts) = 0;
130
    // Return a presigned URL for users to access the object
131
    virtual Result<std::string> generate_presigned_url(const ObjectStoragePathOptions& opts,
132
                                                       int64_t expiration_secs,
133
                                                       const S3ClientConf& conf) = 0;
134
};
135
} // namespace io
136
} // namespace doris