golang-samples | Sample apps and code written for Google Cloud in the Go programming language | GCP library
kandi X-RAY | golang-samples Summary
Support
Quality
Security
License
Reuse
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample Here
golang-samples Key Features
golang-samples Examples and Code Snippets
Trending Discussions on golang-samples
Trending Discussions on golang-samples
QUESTION
I'm learning Google Cloud Vision API, but facing some issues. I have completely repeated all the steps from the 'Getting Started' Guide.
- download/install google cloud SDK
- activate login credentials using gcloud
- Set the environment variable GOOGLE_APPLICATION_CREDENTIALS
- Install the client library
After that I ran this code and got this error when compiling:
..\cloud.google.com\go\longrunning\autogen\operations_client.go:166:54: cannot use connPool (type "google.golang.org/api/internal".ConnPool) as type *"google.golang.org/grpc".ClientConn in argument to longrunning.NewOperationsClient
Compilation finished with exit code 2
I repeated all the steps over and over and still get this error. Windows 10 is installed on my pc with the latest software updates. I have NOT changed the code from the tutorial.
Can anyone please tell me why I'm getting this error?
ANSWER
Answered 2020-Nov-03 at 15:20Issue has been fixed.
I just needed to remove conflicting custom imports and re-install Google Vision library.
Well that was a dumb mistake, sorry to bother you.
QUESTION
I'm trying to deploy an app engine application, but no matter which code I'm trying to deploy, even if it's taken from their samples at:
https://github.com/GoogleCloudPlatform/golang-samples
I get the same error:
Step #1: error building image: getting stage builder for stage 0: MANIFEST_UNKNOWN: "Manifest with digest 'sha256:249859465bcde1cb15128ff0d9eb2bb54de67f72a834a7576e6649cfe0a27698' has media type 'application/vnd.docker.distribution.manifest.list.v2+json', but client accepts 'application/vnd.docker.distribution.manifest.v2+json'."
I'm not good at devops, so no idea howto work this around
ANSWER
Answered 2020-Oct-25 at 19:18I encountered the same issue when deploying on AppEngine Flexible today. I don't think it's something you can do. I opened an issue on github hopefully the google cloud team will fix it fast.
For info, the issue the OP has is that when running gcloud app deploy app.yaml
he expects the app do be deployed.
Cloud Build tries to fetch an image that has a media type it does not understand.
Got: application/vnd.docker.distribution.manifest.list.v2+json
Expected: application/vnd.docker.distribution.manifest.v2+json
Step #0: gcr.io/gcp-runtimes/go1-builder@sha256:7b53332a8e6418ba9a3f123c29dd5fe075504d0f9c0b683edfc7e7b75cd27822
Finished Step #0
Starting Step #1
Step #1: Already have image (with digest): gcr.io/kaniko-project/executor@sha256:f87c11770a4d3ed33436508d206c584812cd656e6ed08eda1cff5c1ee44f5870
Step #1: INFO[0000] Downloading base image gcr.io/distroless/base@sha256:884ac2144c3ee154bd67271e99bc6ef00f430092750167729b0321ab55fde5ec
Step #1: error building image: getting stage builder for stage 0: MANIFEST_UNKNOWN: "Manifest with digest 'sha256:884ac2144c3ee154bd67271e99bc6ef00f430092750167729b0321ab55fde5ec' has media type 'application/vnd.docker.distribution.manifest.list.v2+json', but client accepts 'application/vnd.docker.distribution.manifest.v2+json'."
QUESTION
I'm following two guides which appear to conflict
Go's Standard Go Project Layout recommends a /build
directory, the use of which is described as
Packaging and Continuous Integration.
Put your cloud (AMI), container (Docker), OS (deb, rpm, pkg) package configurations and scripts in the /build/package directory.
Following Google Cloud Endpoints Sample for Go using gRPC, I have a Dockerfile which copies application source code and installs any dependencies with go get
.
# /build/Dockerfile
FROM golang:alpine
# Alpine Linux package management
RUN apk update
RUN apk add git
COPY ../ /go/src/github.com/username/repository
# Downloads the packages named by the import paths, along with their
# dependencies. It then installs the named packages, like 'go install'.
# ****Don't do this in production! Use vendoring instead.****
RUN go get -v github.com/username/repository/foo-module
# Compiles and installs the packages named by the import paths.
RUN go install github.com/username/repository/foo-module
ENTRYPOINT ["/go/bin/foo-module"]
Following Standard Go Project Layout I place the aforementioned Dockerfile in /build
.
Since the Dockerfile is now in the /build
directory, I modified the COPY
command to copy the parent directory to find the application source code (COPY ../ /go/src/github.com/username/repository
).
The Docker build
command is not run directly, rather the build is started with Google Cloud Build
cloud-build-local --config=build/cloudbuild.yaml .
The cloudbuild.yaml
file is really simple and just kicks off the Docker build. The --file
flag points to build/Dockerfile
since the Cloud Build command is started from the project root and not from /build
)
# /build/cloudbuild.yaml
steps:
- id: Build
name: "gcr.io/cloud-builders/docker"
dir: ${_SOURCE}
args:
[
"build",
"--file build/Dockerfile",
".",
]
As expected, this fails
COPY failed: Forbidden path outside the build context: ../ ()
How to include files outside of Docker's build context? suggests using the --file
flag, however, this assumes the build is started from the root context. When using Google's Cloud Build, the build is started from cloudbuild.yaml
, which also located in /build
.
I could just place the Docker file in the root of my go module, but I would like to follow best practices where possible and keep cloudbuild.yaml
and Dockerfile
in /build
.
What is the correct way to achieve this while following the Standard Go Project Layout?
ANSWER
Answered 2020-Sep-19 at 18:26That's a rather long question, so lets focus on the problem encountered:
COPY ../ /go/src/github.com/username/repository
which resulted in
COPY failed: Forbidden path outside the build context: ../ ()
You don't include files outside of the context, docker doesn't allow that. Instead you change your context to include the files you need to build your image, and make the paths in your COPY/ADD commands relative to that context. Make sure to reread that, these paths are not relative to the Dockerfile location.
So with a docker build
, if you have build/Dockerfile
, you would build from the parent directory with:
docker build -f build/Dockerfile .
That trailing dot is the path to the build context which gets sent to the docker engine to perform the build. It doesn't access files directly on the client, which is why you can't include files from outside of the context (plus you don't want malicious Dockerfiles extracting data from the build server).
Then inside the Dockerfile you'd define the paths relative to that context:
COPY . /go/src/github.com/username/repository
And if for some reason you cannot build from that parent directory because of the tooling, then make the context the parent folder with a relative path:
docker build -f Dockerfile ..
QUESTION
I'm trying to create a bucket in GCP using Go, I tried this create method, that creates a bucket in Go. Here's the go playground of the same. When I try to run it, it just says timeout running go build
and Go Build Failed
. I know it's trying to download some packages, but not sure why it is getting timed out while downloading the package?. I'm new to Go and wanted to test out how bucket creation works in go. Am I missing something?.
ANSWER
Answered 2020-Aug-15 at 11:28Log in:
gcloud auth application-default login
Create this create-bucket.go
file and replace the project ID with your project ID:
// Sample storage-quickstart creates a Google Cloud Storage bucket.
package main
import (
"context"
"fmt"
"log"
"time"
"cloud.google.com/go/storage"
)
func main() {
ctx := context.Background()
// Sets your Google Cloud Platform project ID.
projectID := ""
// Creates a client.
client, err := storage.NewClient(ctx)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Sets the name for the new bucket.
bucketName := "go-new-bucket"
// Creates a Bucket instance.
bucket := client.Bucket(bucketName)
// Creates the new bucket.
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
if err := bucket.Create(ctx, projectID, nil); err != nil {
log.Fatalf("Failed to create bucket: %v", err)
}
fmt.Printf("Bucket %v created.\n", bucketName)
}
Run the go file that you created in the previous step:
go run create-bucket.go
You'll find the go-new-bucket
under the GCP storage menu:
QUESTION
As an alternative to signed urls with a url prefix, I'm trying to get signed cookies working. Google Cloud CDN is setup with a backend bucket which is configured and working for standard signed urls.
Using these Go examples I've implemented a cookie signing function in nodejs(typescript) that when provied with the test sample data produces the expected outcome.
export function signCookie(urlPrefix: any, keyName: string, key: any, experation: Date): string {
// Base64url encode the url prefix
const urlPrefixEncoded = Buffer.from(urlPrefix)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_');
// Input to be signed
const input = `URLPrefix=${urlPrefixEncoded}:Expires=${experation.getTime()}:KeyName=${keyName}`;
// Create bytes from given key string.
const keyBytes = Buffer.from(key, 'base64');
// Use key bytes and crypto.createHmac to produce a base64 encoded signature which is then escaped to be base64url encoded.
const signature = createHmac('sha1', keyBytes)
.update(input)
.digest('base64').replace(/\+/g, '-')
.replace(/\//g, '_');
// Adding the signature on the end if the cookie value
const signedValue = `${input}:Signature=${signature}`;
return signedValue;
}
When I then use the same function to produce signed cookie values for my actual cdn instance I get the following (key name and url prefix not actual):
URLPrefix=aHR0cHM6L------------------HdhcmUuaW8v:Expires=1587585646437:KeyName=my-key-name:Signature=2mJbbtYVclycXBGIpKzsJWuLXEA=
Creating a cooking using firefox dev tools I get the following two results when the cookie is attached and when it is not:
It appears that the cookie "Cloud-CDN-Cookie" is just being passed through Cloud CDN and straight to the backend bucket where it's ignored and the standard response access denied response is given.
The cloud platform logs shows no cdn intervention.
With cookie attached No cookie attached
Is there something in either the signing implementation or creation and use of the cookie that I'm doing wrong?
ANSWER
Answered 2020-Apr-25 at 13:50My Google project did not yet have the signed cookie feature enabled. Another user contacted support and once the issue was resolved for them it was resolved for me no change to code and it works.
This is my final nodejs(typescript) signed cookie implementation.
function signCookie(urlPrefix: any, keyName: string, key: any, experation: Date): string {
// Base64url encode the url prefix
const urlPrefixEncoded = Buffer.from(urlPrefix)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_');
// Input to be signed
const input = `URLPrefix=${urlPrefixEncoded}:Expires=${experation.getTime()}:KeyName=${keyName}`;
// Create bytes from given key string.
const keyBytes = Buffer.from(key, 'base64');
// Use key bytes and crypto.createHmac to produce a base64 encoded signature which is then escaped to be base64url encoded.
const signature = createHmac('sha1', keyBytes)
.update(input)
.digest('base64').replace(/\+/g, '-')
.replace(/\//g, '_');
// Adding the signature on the end if the cookie value
const signedValue = `${input}:Signature=${signature}`;
return signedValue;
}
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install golang-samples
Support
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesExplore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits
Save this library and start creating your kit
Share this Page