致電 Amazon Lookout for Vision 操作 - Amazon Lookout for Vision

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

致電 Amazon Lookout for Vision 操作

執行下列程式碼,以確認您可以呼叫 Amazon Lookout for Vision API。此程式碼會在目前 AWS 區域中列出您 AWS 帳戶中的專案。如果您之前尚未建立專案,則回應為空白,但確認您可以呼叫該 ListProjects操作。

一般而言,呼叫範例函數需要 AWS SDK Lookout for Vision 戶端和任何其他必要參數。AWS 開發套件 Lookout for Vision 戶端在主要功能中宣告。

如果程式碼失敗,請檢查您使用的使用者是否擁有正確的權限。另外,請檢查您使用的AWS區域 Amazon Lookout for Vision 並非所有區AWS域都可用。

致電 Amazon Lookout for Vision 操作
  1. 如果您尚未安裝和設定 AWS CLI 和 AWS SDK,請先完成此步驟。如需詳細資訊,請參閱步驟 4:設定 AWS CLI 和 AWS SDKs

  2. 使用以下的範例程式碼來檢視您的專案。

    CLI

    使用 list-projects 指令列出您帳戶中的專案。

    aws lookoutvision list-projects \ --profile lookoutvision-access
    Python

    此代碼取自AWS文檔 SDK 示例 GitHub 存儲庫。請參閱此處的完整範例。

    # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 from botocore.exceptions import ClientError import boto3 class GettingStarted: @staticmethod def list_projects(lookoutvision_client): """ Lists information about the projects that are in in your AWS account and in the current AWS Region. :param lookoutvision_client: A Boto3 Lookout for Vision client. """ try: response = lookoutvision_client.list_projects() for project in response["Projects"]: print("Project: " + project["ProjectName"]) print("ARN: " + project["ProjectArn"]) print() print("Done!") except ClientError: raise def main(): session = boto3.Session(profile_name='lookoutvision-access') lookoutvision_client = session.client("lookoutvision") GettingStarted.list_projects(lookoutvision_client) if __name__ == "__main__": main()
    Java V2

    此代碼取自AWS文檔 SDK 示例 GitHub 存儲庫。請參閱此處的完整範例。

    /* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ package com.example.lookoutvision; import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; import software.amazon.awssdk.services.lookoutvision.LookoutVisionClient; import software.amazon.awssdk.services.lookoutvision.model.ProjectMetadata; import software.amazon.awssdk.services.lookoutvision.paginators.ListProjectsIterable; import software.amazon.awssdk.services.lookoutvision.model.ListProjectsRequest; import software.amazon.awssdk.services.lookoutvision.model.LookoutVisionException; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; public class GettingStarted { public static final Logger logger = Logger.getLogger(GettingStarted.class.getName()); /** * Lists the Amazon Lookoutfor Vision projects in the current AWS account and * AWS Region. * * @param lfvClient An Amazon Lookout for Vision client. * @return List<ProjectMetadata> Metadata for each project. */ public static List<ProjectMetadata> listProjects(LookoutVisionClient lfvClient) throws LookoutVisionException { logger.log(Level.INFO, "Getting projects:"); ListProjectsRequest listProjectsRequest = ListProjectsRequest.builder() .maxResults(100) .build(); List<ProjectMetadata> projectMetadata = new ArrayList<>(); ListProjectsIterable projects = lfvClient.listProjectsPaginator(listProjectsRequest); projects.stream().flatMap(r -> r.projects().stream()) .forEach(project -> { projectMetadata.add(project); logger.log(Level.INFO, project.projectName()); }); logger.log(Level.INFO, "Finished getting projects."); return projectMetadata; } public static void main(String[] args) throws Exception { try { // Get the Lookout for Vision client. LookoutVisionClient lfvClient = LookoutVisionClient.builder() .credentialsProvider(ProfileCredentialsProvider.create("lookoutvision-access")) .build(); List<ProjectMetadata> projects = Projects.listProjects(lfvClient); System.out.printf("Projects%n--------%n"); for (ProjectMetadata project : projects) { System.out.printf("Name: %s%n", project.projectName()); System.out.printf("ARN: %s%n", project.projectArn()); System.out.printf("Date: %s%n%n", project.creationTimestamp().toString()); } } catch (LookoutVisionException lfvError) { logger.log(Level.SEVERE, "Could not list projects: {0}: {1}", new Object[] { lfvError.awsErrorDetails().errorCode(), lfvError.awsErrorDetails().errorMessage() }); System.out.println(String.format("Could not list projects: %s", lfvError.getMessage())); System.exit(1); } } }