-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
168 additions
and
52 deletions.
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
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,3 @@ | ||
{ | ||
"bucket_name": "your-bucket-name" | ||
} |
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,74 @@ | ||
package gcs | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io/ioutil" | ||
"log" | ||
"fmt" | ||
|
||
"cloud.google.com/go/storage" | ||
"github.com/google/uuid" | ||
) | ||
|
||
type Config struct { | ||
Bucket string `json:"bucket_name"` | ||
} | ||
|
||
type GCSProducer struct { | ||
client storage.Client | ||
bucket string | ||
} | ||
|
||
func (p *GCSProducer) Initialize(configFile string) { | ||
var config Config | ||
file, err := ioutil.ReadFile(configFile) | ||
err = json.Unmarshal(file, &config) | ||
if err != nil { | ||
log.Fatalf("Failed to parse configuration parameters: %s", err) | ||
} | ||
|
||
ctx := context.Background() | ||
// Use Google Application Default Credentials to authorize and authenticate the client. | ||
// More information about Application Default Credentials and how to enable is at | ||
// https://developers.google.com/identity/protocols/application-default-credentials. | ||
client, err := storage.NewClient(ctx) | ||
if err != nil { | ||
log.Fatalf("Failed to create client: %v", err) | ||
} | ||
|
||
p.client = *client | ||
p.bucket = config.Bucket | ||
} | ||
|
||
func (p *GCSProducer) Produce(k []byte, v []byte, o any) { | ||
ctx := context.Background() | ||
|
||
bucket := p.bucket | ||
var key string | ||
|
||
if k == nil || len(k) == 0 { | ||
// generate a UUID as index | ||
id := uuid.New() | ||
key = id.String() + "/.json" | ||
} else { | ||
key = string(k) + "/.json" | ||
} | ||
|
||
objectHandle := p.client.Bucket(bucket).Object(key) | ||
writer := objectHandle.NewWriter(ctx) | ||
kvPair := fmt.Sprintf("%s=%s\n", key, v) | ||
|
||
_, err := writer.Write([]byte(kvPair)) | ||
if err != nil { | ||
log.Fatalf("Failed to write to GCS: %v", err) | ||
} | ||
|
||
writer.Close() | ||
|
||
} | ||
|
||
func (p *GCSProducer) Close() error { | ||
p.client.Close() | ||
return nil | ||
} |