Coverage Report

Created: 2026-08-14 19:23

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 "io/fs/file_system.h"
23
#include "io/fs/path.h"
24
namespace doris {
25
class Status;
26
struct S3ClientConf;
27
namespace io {
28
29
// Names are in lexico order.
30
enum class ObjStorageType : uint8_t {
31
    UNKNOWN = 0,
32
    AWS = 1,
33
    AZURE,
34
    BOS,
35
    COS,
36
    OSS,
37
    OBS,
38
    GCP,
39
    TOS,
40
};
41
42
struct ObjectStoragePathOptions {
43
    Path path = "";
44
    std::string bucket = std::string();                  // blob container in azure
45
    std::string key = std::string();                     // blob name in azure
46
    std::string prefix = std::string();                  // for batch delete and recursive delete
47
    std::optional<std::string> upload_id = std::nullopt; // token identifying this writer's parts
48
};
49
50
struct ObjectCompleteMultiPart {
51
    int part_num = 0;
52
    std::string etag = std::string();
53
};
54
55
struct ObjectStorageStatus {
56
    int code = 0;
57
    std::string msg = std::string();
58
};
59
60
// We only store error code along with err_msg instead of Status to unify BE and recycler's error handle logic
61
struct ObjectStorageResponse {
62
    ObjectStorageStatus status {};
63
    int http_code {200};
64
    std::string request_id = std::string();
65
2.25k
    static ObjectStorageResponse OK() {
66
        // clang-format off
67
2.25k
        return {
68
2.25k
                .status { .code = 0, },
69
2.25k
                .http_code = 200,
70
2.25k
        };
71
        // clang-format on
72
2.25k
    }
73
};
74
75
struct ObjectStorageUploadResponse {
76
    ObjectStorageResponse resp {};
77
    std::optional<std::string> upload_id = std::nullopt;
78
    std::optional<std::string> etag = std::nullopt;
79
};
80
81
struct ObjectStorageHeadResponse : ObjectStorageResponse {
82
    ObjectStorageResponse resp {};
83
    long long file_size {0};
84
};
85
86
class ObjStorageClient {
87
public:
88
112
    virtual ~ObjStorageClient() = default;
89
    // Create a multi-part upload request. The returned token may be provider-issued or local and
90
    // identifies this writer's parts.
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 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