Skip to main content

Obtain an image public URL

This page describes how to obtain the public Content Delivery Network (CDN) public URL from an image resource name.

caution

The image public URL might change if reclassified from the Vision API, for example a previously detected safe image might be reclassified as adult content, this will change the subdomain of the URL.

Sample

The following sample shows how to obtain the public URL from an image resource name.

Replace [IMAGE] with the resource name of the image that the public URL should be generated for, e.g. users/123/albums/456/images/789.

tip

Tip: do not forget to replace CLIENT-ID and CLIENT-SECRET with valid IAM Service Account client credentials.

package main

import (
"context"
"log"

"golang.org/x/oauth2/clientcredentials"
"google.golang.org/api/option"

gapic "github.com/animeapis/api-go-client/image/v1alpha1"
image "github.com/animeapis/go-genproto/image/v1alpha1"
)

var (
Image = "[IMAGE]"

ClientID = "[CLIENT-ID]"
ClientSecret = "[CLIENT-SECRET]"
)

var (
TokenURL = "https://accounts.animeshon.com/o/oauth2/token"
Endpoint = "image.animeapis.com:443"
)

func main() {
ctx := context.Background()

config := &clientcredentials.Config{
ClientID: ClientID,
ClientSecret: ClientSecret,
TokenURL: TokenURL,
}

options := []option.ClientOption{
option.WithEndpoint(Endpoint),
option.WithTokenSource(config.TokenSource(ctx)),
}

client, err := gapic.NewImageRouterClient(ctx, options...)
if err != nil {
log.Fatalf("NewClient: %s", err)
}

request := &image.GetImageRouteRequest{
Name: Image,
}

response, err := client.GetImageRoute(ctx, request)
if err != nil {
log.Fatalf("GetImageRoute: %s", err)
}

log.Printf("[cdn] public url: %s", response.GetUrl())
}
View on GitHub