zeroshade commented on code in PR #216: URL: https://github.com/apache/iceberg-go/pull/216#discussion_r1882673169
########## catalog/catalog.go: ########## @@ -32,6 +32,12 @@ type CatalogType string type AwsProperties map[string]string +type OSSConfig struct { Review Comment: Can we add a docstring comment to explain what this config is for? ########## catalog/catalog.go: ########## @@ -122,12 +128,32 @@ func WithPrefix(prefix string) Option[RestCatalog] { } } +func WithOSSEndpoint(endpoint string) Option[RestCatalog] { + return func(o *options) { + o.ossConfig.Endpoint = endpoint + } +} + +func WithOSSAccessKey(accessKey string) Option[RestCatalog] { + return func(o *options) { + o.ossConfig.AccessKey = accessKey + } +} + +func WithOSSSecretKey(secretKey string) Option[RestCatalog] { + return func(o *options) { + o.ossConfig.SecretKey = secretKey + } +} Review Comment: Should we just do `WithOSSConfig(cfg OSSConfig) Option[RestCatalog]` instead? ########## io/oss.go: ########## @@ -0,0 +1,153 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package io + +import ( + "fmt" + "io" + "io/fs" + "net/url" + "os" + "strconv" + "strings" + "sync" + "time" + + "github.com/aliyun/aliyun-oss-go-sdk/oss" +) + +// Constants for OSS configuration options +const ( + OSSAccessKey = "client.oss-access-key" + OSSSecretKey = "client.oss-secret-key" + OSSEndpoint = "client.oss-endpoint" +) + +func createOSSFileIO(parsed *url.URL, props map[string]string) (IO, error) { + endpoint, ok := props[OSSEndpoint] + if !ok { + endpoint = os.Getenv("OSS_ENDPOINT") + } + if endpoint == "" { + return nil, fmt.Errorf("oss endpoint must be specified") + } + + accessKey := props[OSSAccessKey] + secretKey := props[OSSSecretKey] + + client, err := oss.New(endpoint, accessKey, secretKey) + if err != nil { + return nil, err + } + + bucketName := parsed.Host + bucket, err := client.Bucket(bucketName) + if err != nil { + return nil, err + } + + ossFS := &ossFS{ + bucket: bucket, + } + + preprocess := func(n string) string { + _, after, found := strings.Cut(n, "://") + if found { + n = after + } + return strings.TrimPrefix(n, parsed.Host) + } + + return FSPreProcName(ossFS, preprocess), nil +} + +type ossFS struct { + bucket *oss.Bucket +} + +// Open implements fs.FS +func (o *ossFS) Open(name string) (fs.File, error) { + if !fs.ValidPath(name) { + return nil, &os.PathError{Op: "open", Path: name, Err: os.ErrInvalid} + } + if name == "." { + return &ossFile{ + name: name, + bucket: o.bucket, + }, nil + } + name = strings.TrimPrefix(name, "/") + + obj, err := o.bucket.GetObject(name) + if err != nil { + return nil, err + } + + return &ossFile{ + reader: obj, + name: name, + bucket: o.bucket, + }, nil +} + +type ossFile struct { + mutex sync.Mutex + reader io.ReadCloser + bucket *oss.Bucket + name string +} + +func (f *ossFile) Read(p []byte) (int, error) { + f.mutex.Lock() + defer f.mutex.Unlock() + + return f.reader.Read(p) +} Review Comment: this needs to be updated to implement `io.ReadSeekCloser` and `io.ReaderAt` interfaces -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org