Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions cpp/src/arrow/filesystem/s3fs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3052,13 +3052,21 @@ Result<std::string> S3FileSystem::MakeUri(std::string path) const {
if (path.length() <= 1 || path[0] != '/') {
return Status::Invalid("MakeUri requires an absolute, non-root path, got ", path);
}
ARROW_ASSIGN_OR_RAISE(auto uri, util::UriFromAbsolutePath(path));
ARROW_ASSIGN_OR_RAISE(auto uri_from_path, util::UriFromAbsolutePath(path));
constexpr std::string_view kFileScheme = "file://";
std::string_view uri_view(uri_from_path);
if (uri_view.starts_with(kFileScheme)) {
uri_view.remove_prefix(kFileScheme.size());
}
if (uri_view.starts_with("/")) {
// Remove leading slash if present
uri_view.remove_prefix(1);
}
std::string uri = "s3://";
if (!options().GetAccessKey().empty()) {
uri = "s3://" + options().GetAccessKey() + ":" + options().GetSecretKey() + "@" +
uri.substr("file:///"s.size());
} else {
uri = "s3" + uri.substr("file"s.size());
uri += options().GetAccessKey() + ":" + options().GetSecretKey() + "@";
}
uri += std::string(uri_view);
uri += "?";
uri += "region=" + util::UriEscape(options().region);
uri += "&";
Expand Down
29 changes: 29 additions & 0 deletions cpp/src/arrow/filesystem/s3fs_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,35 @@ TEST_F(S3FileSystemRegionTest, EnvironmentVariable) {
}
#endif

////////////////////////////////////////////////////////////////////////////
// S3FileSystem make URI test

class S3FileSystemMakeUri : public AwsTestMixin {};

TEST_F(S3FileSystemMakeUri, MakeUriWithCredentials) {
S3Options options;
options.ConfigureAccessKey("minio", "miniopass");
options.region = "us-east-1";
ASSERT_OK_AND_ASSIGN(auto fs, S3FileSystem::Make(options));
ASSERT_OK_AND_ASSIGN(auto uri, fs->MakeUri("/bucket/somedir/subdir/subfile"));
EXPECT_EQ(uri,
"s3://minio:miniopass@bucket/somedir/subdir/subfile"
"?region=us-east-1&scheme=https&endpoint_override="
"&allow_bucket_creation=0&allow_bucket_deletion=0");
}

TEST_F(S3FileSystemMakeUri, MakeUriWithoutCredentials) {
S3Options options;
options.ConfigureAnonymousCredentials();
options.region = "us-east-1";
ASSERT_OK_AND_ASSIGN(auto fs, S3FileSystem::Make(options));
ASSERT_OK_AND_ASSIGN(auto uri, fs->MakeUri("/bucket/somedir/subdir/subfile"));
EXPECT_EQ(uri,
"s3://bucket/somedir/subdir/subfile"
"?region=us-east-1&scheme=https&endpoint_override="
"&allow_bucket_creation=0&allow_bucket_deletion=0");
}

////////////////////////////////////////////////////////////////////////////
// Basic test for the Minio test server.

Expand Down
Loading