Um die Suche zu vereinfachen und die Dokumentation zu verbessern, teilen wir die Dokumentation der 1. Generation und der 2. Generation in separate Gruppen ein.
// Package ocr contains Go samples for creating OCR// (Optical Character Recognition) Cloud functions.packageocrimport("context""fmt""os""strings""time""cloud.google.com/go/pubsub""cloud.google.com/go/storage""cloud.google.com/go/translate"vision"cloud.google.com/go/vision/apiv1""golang.org/x/text/language")typeocrMessagestruct{Textstring`json:"text"`FileNamestring`json:"fileName"`Langlanguage.Tag`json:"lang"`SrcLanglanguage.Tag`json:"srcLang"`}// GCSEvent is the payload of a GCS event.typeGCSEventstruct{Bucketstring`json:"bucket"`Namestring`json:"name"`Metagenerationstring`json:"metageneration"`ResourceStatestring`json:"resourceState"`TimeCreatedtime.Time`json:"timeCreated"`Updatedtime.Time`json:"updated"`}// PubSubMessage is the payload of a Pub/Sub event.// See the documentation for more details:// https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessagetypePubSubMessagestruct{Data[]byte`json:"data"`}var(visionClient*vision.ImageAnnotatorClienttranslateClient*translate.ClientpubsubClient*pubsub.ClientstorageClient*storage.ClientprojectIDstringresultBucketstringresultTopicstringtoLang[]stringtranslateTopicstring)funcsetup(ctxcontext.Context)error{projectID=os.Getenv("GCP_PROJECT")resultBucket=os.Getenv("RESULT_BUCKET")resultTopic=os.Getenv("RESULT_TOPIC")toLang=strings.Split(os.Getenv("TO_LANG"),",")translateTopic=os.Getenv("TRANSLATE_TOPIC")varerrerror// Prevent shadowing clients with :=.ifvisionClient==nil{visionClient,err=vision.NewImageAnnotatorClient(ctx)iferr!=nil{returnfmt.Errorf("vision.NewImageAnnotatorClient: %v",err)}}iftranslateClient==nil{translateClient,err=translate.NewClient(ctx)iferr!=nil{returnfmt.Errorf("translate.NewClient: %v",err)}}ifpubsubClient==nil{pubsubClient,err=pubsub.NewClient(ctx,projectID)iferr!=nil{returnfmt.Errorf("translate.NewClient: %v",err)}}ifstorageClient==nil{storageClient,err=storage.NewClient(ctx)iferr!=nil{returnfmt.Errorf("storage.NewClient: %v",err)}}returnnil}
publicclassOcrProcessImageimplementsBackgroundFunction<GcsEvent>{// TODO<developer> set these environment variablesprivatestaticfinalStringPROJECT_ID=System.getenv("GCP_PROJECT");privatestaticfinalStringTRANSLATE_TOPIC_NAME=System.getenv("TRANSLATE_TOPIC");privatestaticfinalString[]TO_LANGS=System.getenv("TO_LANG").split(",");privatestaticfinalLoggerlogger=Logger.getLogger(OcrProcessImage.class.getName());privatestaticfinalStringLOCATION_NAME=LocationName.of(PROJECT_ID,"global").toString();privatePublisherpublisher;publicOcrProcessImage()throwsIOException{publisher=Publisher.newBuilder(ProjectTopicName.of(PROJECT_ID,TRANSLATE_TOPIC_NAME)).build();}}
// Get a reference to the Pub/Sub componentconst{PubSub}=require('@google-cloud/pubsub');constpubsub=newPubSub();// Get a reference to the Cloud Storage componentconst{Storage}=require('@google-cloud/storage');conststorage=newStorage();// Get a reference to the Cloud Vision API componentconstVision=require('@google-cloud/vision');constvision=newVision.ImageAnnotatorClient();// Get a reference to the Translate API componentconst{Translate}=require('@google-cloud/translate').v2;consttranslate=newTranslate();
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],[],[],[]]