Skip to content

Commit bae001f

Browse files
authored
Merge pull request #10 from huanghantao/feature/s3fs-path-style-and-bucket-check
Feature/s3fs path style and bucket check
2 parents 159179c + 62b2fa1 commit bae001f

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

agfs-server/pkg/plugins/s3fs/client.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ type S3Config struct {
4141
Endpoint string // Optional custom endpoint (for S3-compatible services)
4242
Prefix string // Optional prefix for all keys (will be wrapped for isolation)
4343
DisableSSL bool // For testing with local S3
44+
UsePathStyle bool // Use path-style requests (required for MinIO and some S3-compatible services)
45+
}
46+
47+
// checkBucketAccess verifies that the bucket exists and is accessible
48+
// Uses ListObjectsV2 instead of HeadBucket for better compatibility
49+
// Some S3-compatible services (e.g., certain object storage providers) don't support HeadBucket
50+
func checkBucketAccess(ctx context.Context, client *s3.Client, bucket string) error {
51+
_, err := client.ListObjectsV2(ctx, &s3.ListObjectsV2Input{
52+
Bucket: aws.String(bucket),
53+
MaxKeys: aws.Int32(1), // Only need to check if we can access the bucket
54+
})
55+
return err
4456
}
4557

4658
// NewS3Client creates a new S3 client
@@ -74,17 +86,21 @@ func NewS3Client(cfg S3Config) (*S3Client, error) {
7486
if cfg.Endpoint != "" {
7587
clientOpts = append(clientOpts, func(o *s3.Options) {
7688
o.BaseEndpoint = aws.String(cfg.Endpoint)
77-
o.UsePathStyle = true // Required for MinIO and some S3-compatible services
89+
})
90+
}
91+
92+
// Set path-style requests if configured (required for MinIO and some S3-compatible services)
93+
if cfg.UsePathStyle {
94+
clientOpts = append(clientOpts, func(o *s3.Options) {
95+
o.UsePathStyle = true
7896
})
7997
}
8098

8199
client := s3.NewFromConfig(awsCfg, clientOpts...)
82100

83-
// Verify bucket exists
84-
_, err = client.HeadBucket(ctx, &s3.HeadBucketInput{
85-
Bucket: aws.String(cfg.Bucket),
86-
})
87-
if err != nil {
101+
// Verify bucket exists using ListObjectsV2 (more compatible than HeadBucket)
102+
// Some object storage services don't support HeadBucket
103+
if err = checkBucketAccess(ctx, client, cfg.Bucket); err != nil {
88104
return nil, fmt.Errorf("failed to access bucket %s: %w", cfg.Bucket, err)
89105
}
90106

agfs-server/pkg/plugins/s3fs/s3fs.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ func (p *S3FSPlugin) Validate(cfg map[string]interface{}) error {
519519
// Check for unknown parameters
520520
allowedKeys := []string{
521521
"bucket", "region", "access_key_id", "secret_access_key", "endpoint", "prefix", "disable_ssl", "mount_path",
522-
"cache_enabled", "cache_ttl", "stat_cache_ttl", "cache_max_size",
522+
"cache_enabled", "cache_ttl", "stat_cache_ttl", "cache_max_size", "use_path_request_style",
523523
}
524524
if err := config.ValidateOnlyKnownKeys(cfg, allowedKeys); err != nil {
525525
return err
@@ -538,7 +538,7 @@ func (p *S3FSPlugin) Validate(cfg map[string]interface{}) error {
538538
}
539539

540540
// Validate boolean parameters
541-
for _, key := range []string{"disable_ssl", "cache_enabled"} {
541+
for _, key := range []string{"disable_ssl", "use_path_request_style", "cache_enabled"} {
542542
if err := config.ValidateBoolType(cfg, key); err != nil {
543543
return err
544544
}
@@ -559,6 +559,7 @@ func (p *S3FSPlugin) Initialize(config map[string]interface{}) error {
559559
Endpoint: getStringConfig(config, "endpoint", ""),
560560
Prefix: getStringConfig(config, "prefix", ""),
561561
DisableSSL: getBoolConfig(config, "disable_ssl", false),
562+
UsePathStyle: getBoolConfig(config, "use_path_request_style", false),
562563
}
563564

564565
if cfg.Bucket == "" {
@@ -643,6 +644,13 @@ func (p *S3FSPlugin) GetConfigParams() []plugin.ConfigParameter {
643644
Default: "false",
644645
Description: "Disable SSL for S3 connections",
645646
},
647+
{
648+
Name: "use_path_request_style",
649+
Type: "bool",
650+
Required: false,
651+
Default: "false",
652+
Description: "Use path-style requests instead of virtual-hosted-style (required for MinIO and some S3-compatible services)",
653+
},
646654
{
647655
Name: "cache_enabled",
648656
Type: "bool",
@@ -718,6 +726,7 @@ CONFIGURATION:
718726
secret_access_key = "minioadmin"
719727
endpoint = "http://localhost:9000"
720728
disable_ssl = true
729+
use_path_request_style = true # Required for MinIO and some S3-compatible services
721730
722731
Multiple S3 Buckets:
723732
[plugins.s3fs_prod]

0 commit comments

Comments
 (0)