Coverage Report

Created: 2026-08-18 00:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/io/fs/s3_file_system.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 <filesystem>
21
#include <memory>
22
#include <shared_mutex>
23
#include <string>
24
#include <utility>
25
#include <vector>
26
27
#include "common/status.h"
28
#include "io/fs/file_reader_writer_fwd.h"
29
#include "io/fs/path.h"
30
#include "io/fs/remote_file_system.h"
31
#include "util/s3_util.h"
32
33
namespace Aws::S3 {
34
class S3Client;
35
} // namespace Aws::S3
36
namespace Aws::Utils::Threading {
37
class PooledThreadExecutor;
38
} // namespace Aws::Utils::Threading
39
40
namespace doris::io {
41
// In runtime, AK and SK may be modified, and the original `S3Client` instance will be replaced.
42
// The `S3FileReader` cached by the `Segment` must hold a shared `ObjClientHolder` in order to
43
// access S3 data with latest AK SK.
44
class ObjClientHolder {
45
public:
46
    explicit ObjClientHolder(S3ClientConf conf);
47
    ~ObjClientHolder();
48
49
    Status init();
50
51
    // Update s3 conf and reset client if `conf` is different. This method is threadsafe.
52
    Status reset(const S3ClientConf& conf);
53
54
4.52k
    std::shared_ptr<ObjStorageClient> get() const {
55
4.52k
        std::shared_lock lock(_mtx);
56
4.52k
        return _client;
57
4.52k
    }
58
59
    Result<int64_t> object_file_size(const std::string& bucket, const std::string& key) const;
60
61
    // For error msg
62
    std::string full_s3_path(std::string_view bucket, std::string_view key) const;
63
64
1
    const S3ClientConf& s3_client_conf() { return _conf; }
65
66
private:
67
    mutable std::shared_mutex _mtx;
68
    std::shared_ptr<ObjStorageClient> _client;
69
    S3ClientConf _conf;
70
};
71
72
// File system for S3 compatible object storage
73
// When creating S3FileSystem, all required info should be set in S3Conf,
74
// such as ak, sk, region, endpoint, bucket.
75
// And the root_path of S3FileSystem is s3_conf.prefix.
76
// When using S3FileSystem, it accepts 2 kinds of path:
77
//  1. Full path: s3://bucket/path/to/file.txt
78
//      In this case, the root_path is not used.
79
//  2. only key: path/to/file.txt
80
//      In this case, the final key will be "prefix + path/to/file.txt"
81
class S3FileSystem final : public RemoteFileSystem {
82
public:
83
    static Result<std::shared_ptr<S3FileSystem>> create(S3Conf s3_conf, std::string id);
84
85
    ~S3FileSystem() override;
86
87
6
    const std::shared_ptr<ObjClientHolder>& client_holder() const { return _client; }
88
89
58
    const std::string& bucket() const { return _bucket; }
90
58
    const std::string& prefix() const { return _prefix; }
91
92
    std::string generate_presigned_url(const Path& path, int64_t expiration_secs,
93
                                       bool is_public_endpoint) const;
94
95
protected:
96
    Status create_file_impl(const Path& file, FileWriterPtr* writer,
97
                            const FileWriterOptions* opts) override;
98
    Status open_file_internal(const Path& file, FileReaderSPtr* reader,
99
                              const FileReaderOptions& opts) override;
100
    Status create_directory_impl(const Path& dir, bool failed_if_exists = false) override;
101
    Status delete_file_impl(const Path& file) override;
102
    Status delete_directory_impl(const Path& dir) override;
103
    Status batch_delete_impl(const std::vector<Path>& files) override;
104
    Status exists_impl(const Path& path, bool* res) const override;
105
    Status file_size_impl(const Path& file, int64_t* file_size) const override;
106
    Status list_impl(const Path& dir, bool only_file, std::vector<FileInfo>* files,
107
                     bool* exists) override;
108
    Status rename_impl(const Path& orig_name, const Path& new_name) override;
109
110
    Status upload_impl(const Path& local_file, const Path& remote_file) override;
111
    Status batch_upload_impl(const std::vector<Path>& local_files,
112
                             const std::vector<Path>& remote_files) override;
113
    Status download_impl(const Path& remote_file, const Path& local_file) override;
114
115
2.09k
    Status absolute_path(const Path& path, Path& abs_path) const override {
116
2.09k
        if (path.string().find("://") != std::string::npos) {
117
            // the path is with schema, which means this is a full path like:
118
            // s3://bucket/path/to/file.txt
119
            // so no need to concat with prefix
120
0
            abs_path = path;
121
2.09k
        } else {
122
            // path with no schema
123
2.09k
            abs_path = _prefix / path;
124
2.09k
        }
125
2.09k
        return Status::OK();
126
2.09k
    }
127
128
private:
129
    S3FileSystem(S3Conf s3_conf, std::string id);
130
131
    Status init();
132
133
    // For error msg
134
    std::string full_s3_path(std::string_view key) const;
135
136
    std::string _bucket;
137
    std::string _prefix;
138
    std::shared_ptr<ObjClientHolder> _client;
139
};
140
141
} // namespace doris::io