Welcome to the Bifrost documentation for Google Cloud Storage (GCS)! This guide will walk you through the steps of using Bifrost to upload files to GCS.
Google Cloud Storage is a popular cloud storage service that allows you to store and access your data on Google's infrastructure. Bifrost provides a simple and intuitive way to upload files to GCS without having to write complex code.
Before you can start using Bifrost to upload files to Google Cloud Storage, you'll need to make sure you have the following:
- A Google Cloud account with GCS access
- A GCS bucket to upload files to
- Bifrost installed on your local machine
- Login to your Google Cloud account and navigate to the GCS console
- Click on the "Create bucket" button and follow the prompts to create a new bucket Note the name of your bucket as you will need it later
- Create a new service account by navigating to the IAM & Admin console and selecting "Service accounts" from the left-hand menu
- Click on the "Create Service Account" button and follow the prompts to create a new service account
- Once you have created the service account, navigate to the "Keys" tab and click on the "Create Key" button
- Select "JSON" as the key type and download the JSON key file Note the path to your JSON key file as you will need it later
- Install Bifrost using:
go get github.com/bifrost-cloud/bifrost
- Initialize a new Bifrost client and mount a GCS bridge using the following code:
package main
import (
"fmt"
"github.com/opensaucerer/bifrost"
)
func main() {
bridge, err := bifrost.NewRainbowBridge(&bifrost.BridgeConfig{
DefaultBucket: "bifrost",
DefaultTimeout: 10,
Provider: bifrost.GoogleCloudStorage,
CredentialsFile: "/path/to/service/account/json", // this is not required if you are using google's default credentials
EnableDebug: true,
PublicRead: true,
})
if err != nil {
// bifrost comes with some error codes
if err.(bifrost.Error).Code() == bifrost.ErrInvalidProvider {
fmt.Println("Whoops, you didn't specify a valid provider!")
return
}
fmt.Println(err.(bifrost.Error).Code(), err)
return
}
defer bridge.Disconnect()
fmt.Printf("Connected to %s\n", bridge.Config().Provider)
}
And that's it! You have now mounted a Bifrost bridge to your GCS account and can start uploading files via this bridge.
Now that you have mounted a Bifrost bridge to GCS, you can use Bifrost to upload files to GCS using the following code:
// Upload a file
uploadedFile, err := bridge.UploadFile(bifrost.File{
Path: "../shared/image/aand.png",
Filename: "a_and_ampersand.png",
Options: map[string]interface{}{
bifrost.OptMetadata: map[string]string{
"originalname": "aand.png",
},
},
})
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Uploaded file: %s to %s\n", uploadedFile.Name, uploadedFile.Preview)
As you can see, uploading a file to GCS using Bifrost is as simple as calling the UploadFile method on the Bifrost client with the path to the file on your local machine and the name to give the file on GCS.
If you want to upload multiple files using Bifrost with GCS, you can use the UploadMultiFile method provided by the GCS bridge in Bifrost. Here is an example code snippet:
// Upload multiple files
f, _ := os.Open("../shared/image/hair.jpg")
uploadedFiles, err := bridge.UploadMultiFile(bifrost.MultiFile{
Files: []bifrost.File{
{
Path: "../shared/image/aand.png",
Filename: "a_and_ampersand.png",
Options: map[string]interface{}{
bifrost.OptMetadata: map[string]string{
"originalname": "aand.png",
},
},
},
{
Path: "../shared/image/bifrost.webp",
Filename: "bifrost_bridge.webp",
Options: map[string]interface{}{
bifrost.OptMetadata: map[string]string{
"originalname": "bifrost.jpg",
"universe": "Marvel",
},
},
},
{
Path: "",
Handle: f,
Filename: "sammy.jpg",
Options: map[string]interface{}{
bifrost.OptMetadata: map[string]string{
"originalname": "hair.jpg",
"specie": "Human",
},
bifrost.OptACL: bifrost.ACLPublicRead,
},
},
},
GlobalOptions: map[string]interface{}{
bifrost.OptACL: bifrost.ACLPrivate,
},
})
if err != nil {
fmt.Println(err)
return
}
for _, file := range uploadedFiles {
fmt.Printf("Uploaded file: %s to %s\n", file.Name, file.Preview)
}
We hope you found this guide helpful in using Bifrost with GCS. If you have any questions or feedback, please don't hesitate to open an issue!