Import an image
This page describes how to programmatically import an image from a remote location such as a public web URL. The import might fail for multiple reasons, including internal throttling and rate limiting. In case of failure the response will include the remotely returned status code and details about the failure.
Sample
The following sample shows how to import a remotely accessible image with default caching options.
Replace [ALBUM]
with the resource name of the album where the imported image should be uploaded to, e.g. users/123/albums/456
.
Replace [IMAGE-URL]
with any valid and publicly accessible remote address, e.g. https://avatars.githubusercontent.com/u/48774482
.
tip
Tip: do not forget to replace CLIENT-ID
and CLIENT-SECRET
with valid IAM Service Account client credentials.
- Golang
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 (
Album = "[ALBUM]"
ImageURL = "[IMAGE-URL]"
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.NewClient(ctx, options...)
if err != nil {
log.Fatalf("NewClient: %s", err)
}
request := &image.ImportImageRequest{
Parent: Album,
Uri: ImageURL,
}
response, err := client.ImportImage(ctx, request)
if err != nil {
log.Fatalf("ImportImage: %s", err)
}
log.Printf("[error] status code: %d", response.GetError().GetStatusCode())
log.Printf("[error] details : %s", response.GetError().GetDetails())
log.Printf("[result] name : %s", response.GetResult().GetName())
}