-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Go SDK] S3 implementation of the Beam filesystem (#23992)
* Implement filesystem for S3 * Update CHANGES.md
- Loading branch information
1 parent
73142ba
commit 63362f5
Showing
8 changed files
with
516 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
// 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 s3 contains an AWS S3 implementation of the Beam file system. | ||
package s3 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"path/filepath" | ||
|
||
"github.com/apache/beam/sdks/v2/go/pkg/beam/io/filesystem" | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
) | ||
|
||
func init() { | ||
filesystem.Register("s3", New) | ||
} | ||
|
||
type fs struct { | ||
client *s3.Client | ||
} | ||
|
||
// New creates a new S3 filesystem using AWS default configuration sources. | ||
func New(ctx context.Context) filesystem.Interface { | ||
cfg, err := config.LoadDefaultConfig(ctx) | ||
if err != nil { | ||
panic(fmt.Sprintf("error loading AWS config: %v", err)) | ||
} | ||
|
||
client := s3.NewFromConfig(cfg) | ||
return &fs{client: client} | ||
} | ||
|
||
// Close closes the filesystem. | ||
func (f *fs) Close() error { | ||
return nil | ||
} | ||
|
||
// List returns a slice of the files in the filesystem that match the glob pattern. | ||
func (f *fs) List(ctx context.Context, glob string) ([]string, error) { | ||
bucket, keyPattern, err := parseUri(glob) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing S3 uri: %v", err) | ||
} | ||
|
||
keys, err := f.listObjectKeys(ctx, bucket, keyPattern) | ||
if err != nil { | ||
return nil, fmt.Errorf("error listing object keys: %v", err) | ||
} | ||
|
||
uris := make([]string, len(keys)) | ||
for i, key := range keys { | ||
uris[i] = makeUri(bucket, key) | ||
} | ||
|
||
return uris, nil | ||
} | ||
|
||
// listObjectKeys returns a slice of the keys in the bucket that match the key pattern. | ||
func (f *fs) listObjectKeys( | ||
ctx context.Context, | ||
bucket string, | ||
keyPattern string, | ||
) ([]string, error) { | ||
prefix := getPrefix(keyPattern) | ||
params := &s3.ListObjectsV2Input{ | ||
Bucket: aws.String(bucket), | ||
Prefix: aws.String(prefix), | ||
} | ||
paginator := s3.NewListObjectsV2Paginator(f.client, params) | ||
|
||
var objects []string | ||
for paginator.HasMorePages() { | ||
output, err := paginator.NextPage(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("error retrieving page: %v", err) | ||
} | ||
|
||
for _, object := range output.Contents { | ||
key := aws.ToString(object.Key) | ||
match, err := filepath.Match(keyPattern, key) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid key pattern: %s", keyPattern) | ||
} | ||
|
||
if match { | ||
objects = append(objects, key) | ||
} | ||
} | ||
} | ||
|
||
return objects, nil | ||
} | ||
|
||
// OpenRead returns a new io.ReadCloser to read contents from the file. The caller must call Close | ||
// on the returned io.ReadCloser when done reading. | ||
func (f *fs) OpenRead(ctx context.Context, filename string) (io.ReadCloser, error) { | ||
bucket, key, err := parseUri(filename) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing S3 uri %s: %v", filename, err) | ||
} | ||
|
||
params := &s3.GetObjectInput{ | ||
Bucket: aws.String(bucket), | ||
Key: aws.String(key), | ||
} | ||
output, err := f.client.GetObject(ctx, params) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting object %s: %v", filename, err) | ||
} | ||
|
||
return output.Body, nil | ||
} | ||
|
||
// OpenWrite returns a new io.WriteCloser to write contents to the file. The caller must call Close | ||
// on the returned io.WriteCloser when done writing. | ||
func (f *fs) OpenWrite(ctx context.Context, filename string) (io.WriteCloser, error) { | ||
bucket, key, err := parseUri(filename) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing S3 uri %s: %v", filename, err) | ||
} | ||
|
||
return newWriter(ctx, f.client, bucket, key), nil | ||
} | ||
|
||
// Size returns the size of the file. | ||
func (f *fs) Size(ctx context.Context, filename string) (int64, error) { | ||
bucket, key, err := parseUri(filename) | ||
if err != nil { | ||
return -1, fmt.Errorf("error parsing S3 uri %s: %v", filename, err) | ||
} | ||
|
||
params := &s3.HeadObjectInput{ | ||
Bucket: aws.String(bucket), | ||
Key: aws.String(key), | ||
} | ||
output, err := f.client.HeadObject(ctx, params) | ||
if err != nil { | ||
return -1, fmt.Errorf("error getting metadata for object %s: %v", filename, err) | ||
} | ||
|
||
return output.ContentLength, err | ||
} | ||
|
||
// Remove removes the file from the filesystem. | ||
func (f *fs) Remove(ctx context.Context, filename string) error { | ||
bucket, key, err := parseUri(filename) | ||
if err != nil { | ||
return fmt.Errorf("error parsing S3 uri %s: %v", filename, err) | ||
} | ||
|
||
params := &s3.DeleteObjectInput{ | ||
Bucket: aws.String(bucket), | ||
Key: aws.String(key), | ||
} | ||
if _, err = f.client.DeleteObject(ctx, params); err != nil { | ||
return fmt.Errorf("error deleting object %s: %v", filename, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Copy copies the file from the old path to the new path. | ||
func (f *fs) Copy(ctx context.Context, oldpath, newpath string) error { | ||
sourceBucket, sourceKey, err := parseUri(oldpath) | ||
if err != nil { | ||
return fmt.Errorf("error parsing S3 source uri %s: %v", oldpath, err) | ||
} | ||
|
||
copySource := fmt.Sprintf("%s/%s", sourceBucket, sourceKey) | ||
destBucket, destKey, err := parseUri(newpath) | ||
if err != nil { | ||
return fmt.Errorf("error parsing S3 destination uri %s: %v", newpath, err) | ||
} | ||
|
||
params := &s3.CopyObjectInput{ | ||
Bucket: aws.String(destBucket), | ||
CopySource: aws.String(copySource), | ||
Key: aws.String(destKey), | ||
} | ||
if _, err = f.client.CopyObject(ctx, params); err != nil { | ||
return fmt.Errorf("error copying object %s: %v", oldpath, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Compile time check for interface implementations. | ||
var ( | ||
_ filesystem.Remover = (*fs)(nil) | ||
_ filesystem.Copier = (*fs)(nil) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// 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 s3 | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// parseUri deconstructs the S3 uri in the format 's3://bucket/key' to (bucket, key) | ||
func parseUri(uri string) (string, string, error) { | ||
parsed, err := url.Parse(uri) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
if parsed.Scheme != "s3" { | ||
return "", "", errors.New("scheme must be 's3'") | ||
} | ||
|
||
bucket := parsed.Host | ||
if bucket == "" { | ||
return "", "", errors.New("bucket must not be empty") | ||
} | ||
|
||
var key string | ||
if parsed.Path != "" { | ||
key = parsed.Path[1:] | ||
} | ||
|
||
return bucket, key, nil | ||
} | ||
|
||
// makeUri constructs an S3 uri from the bucket and key to the format 's3://bucket/key' | ||
func makeUri(bucket string, key string) string { | ||
return fmt.Sprintf("s3://%s/%s", bucket, key) | ||
} | ||
|
||
// getPrefix returns the prefix of the key pattern before the first wildcard, if any | ||
func getPrefix(keyPattern string) string { | ||
if index := strings.Index(keyPattern, "*"); index >= 0 { | ||
return keyPattern[:index] | ||
} | ||
return keyPattern | ||
} |
Oops, something went wrong.