Created
June 9, 2018 17:13
-
-
Save dwardu89/df2c5e956a67e66daced18ea135ec34f to your computer and use it in GitHub Desktop.
Creates a Storage Transfer Service request to sync objects form a Google Cloud Bucket to another Google Cloud Bucket
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
package main | |
import ( | |
"golang.org/x/oauth2/google" | |
"google.golang.org/api/storagetransfer/v1" | |
"fmt" | |
"log" | |
"golang.org/x/net/context" | |
) | |
func main() { | |
var jwtJSONKey = []byte(`{ | |
"type": "service_account", | |
"project_id": "", | |
"private_key_id": "", | |
"private_key":"", | |
"client_email": "", | |
"client_id": "", | |
"auth_uri": "", | |
"token_uri": "", | |
"auth_provider_x509_cert_url": "", | |
"client_x509_cert_url": "" | |
} | |
`) | |
conf, err := google.JWTConfigFromJSON(jwtJSONKey, storagetransfer.CloudPlatformScope) | |
if err != nil { | |
log.Fatal(err) | |
} | |
client := conf.Client(context.Background()) | |
if err != nil { | |
fmt.Println(err) | |
} | |
service, err := storagetransfer.New(client) | |
if err != nil { | |
fmt.Println(err) | |
} | |
list, err := service.TransferJobs.List().Filter(`{"project_id":""}`).Do() | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(list.TransferJobs[0].Description) | |
gcsDataSource := storagetransfer.GcsData{ | |
BucketName: "source-bucket", | |
} | |
gcsDataSink := storagetransfer.GcsData{ | |
BucketName: "sink-bucket", | |
} | |
spec := storagetransfer.TransferSpec{ | |
GcsDataSource: &gcsDataSource, | |
GcsDataSink: &gcsDataSink, | |
} | |
startDate := storagetransfer.Date{ | |
Day: 9, | |
Month: 6, | |
Year: 2018, | |
} | |
schedule := storagetransfer.Schedule{ | |
ScheduleStartDate: &startDate, | |
} | |
job := storagetransfer.TransferJob{ | |
ProjectId: "", | |
TransferSpec: &spec, | |
Schedule: &schedule, | |
Status: "ENABLED", | |
Description: "Golang generated transfer job", | |
} | |
result, err := service.TransferJobs.Create(&job).Do() | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(result.Description) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment