Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle the case with trailing slash in the service URL #37

Merged
merged 1 commit into from
Jul 23, 2024
Merged
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
14 changes: 13 additions & 1 deletion src/S3Commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,22 @@ bool AmazonRequest::parseURL(const std::string &url, std::string &bucket_path,

if (m_style == "path") {
host = substring(url, hostStartIdx, resourceStartIdx);
path = substring(url, resourceStartIdx) + "/" + bucket + "/" + object;
auto resourcePrefix = substring(url, resourceStartIdx);
if (resourcePrefix[resourcePrefix.size() - 1] == '/') {
resourcePrefix =
substring(resourcePrefix, 0, resourcePrefix.size() - 1);
}
if (bucket.empty()) {
path = resourcePrefix + object;
bucket_path = resourcePrefix + object.substr(0, object.find('/'));
} else {
path = resourcePrefix + "/" + bucket + "/" + object;
bucket_path = resourcePrefix + "/" + bucket;
}
} else {
host = bucket + "." + substring(url, hostStartIdx, resourceStartIdx);
path = substring(url, resourceStartIdx) + object;
bucket_path = "/";
}

return true;
Expand Down
29 changes: 29 additions & 0 deletions test/s3_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ s3.end
}
};

// Regression test for when the service_url ends in a `/`
class FileSystemS3PathBucketSlash : public FileSystemFixtureBase {
protected:
virtual std::string GetConfig() override {
return R"(
s3.begin
s3.path_name /test
s3.service_name s3.amazonaws.com
s3.region us-east-1
s3.bucket_name genome-browser
s3.service_url https://s3.us-east-1.amazonaws.com/
s3.url_style path
s3.end
)";
}
};

void TestDirectoryContents(S3FileSystem &fs, const std::string &dirname) {
std::unique_ptr<XrdOssDF> dir(fs.newDir());
ASSERT_TRUE(dir);
Expand Down Expand Up @@ -279,6 +296,18 @@ TEST_F(FileSystemS3PathNoBucket, List) {
TestDirectoryContents(fs, "/test/genome-browser/cells/tabula-sapiens/");
}

TEST_F(FileSystemS3PathBucketSlash, Stat) {
S3FileSystem fs(m_log.get(), m_configfn.c_str(), nullptr);
struct stat buff;
auto rv = fs.Stat("/test/cells/tabula-sapiens/cellbrowser.json.bak", &buff);
ASSERT_EQ(rv, 0) << "Failed to stat AWS bucket (" << strerror(-rv) << ")";
}

TEST_F(FileSystemS3PathBucketSlash, List) {
S3FileSystem fs(m_log.get(), m_configfn.c_str(), nullptr);
TestDirectoryContents(fs, "/test/cells/tabula-sapiens");
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
Expand Down
Loading