프로젝트에서 이미지 나열
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
이 샘플에서는 특정 프로젝트에서 사용 가능한 모든 이미지를 나열하는 방법을 보여줍니다.
코드 샘플
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content demonstrates how to retrieve a list of all available images within a specified Google Cloud project using Java and Python.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples provided utilize the Compute Engine client libraries for Java and Python to interact with the Google Cloud API.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication to Compute Engine is required and can be set up using Application Default Credentials.\u003c/p\u003e\n"],["\u003cp\u003eThe sample code showcases the use of the \u003ccode\u003eImagesClient\u003c/code\u003e in both Java and Python to list the project's images by project id.\u003c/p\u003e\n"]]],[],null,["# List images in a project\n\nThis sample demonstrates how to list all the images available in a given project.\n\nCode sample\n-----------\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Java API\nreference documentation](/java/docs/reference/google-cloud-compute/latest/overview).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.Image.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ImagesClient.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ListImagesRequest.html;\n import com.google.common.collect.Lists;\n import java.io.IOException;\n import java.util.ArrayList;\n import java.util.List;\n\n public class ListImages {\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n // Project ID or project number of the Google Cloud project you want to use.\n String projectId = \"your-project-id\";\n\n listImages(projectId);\n }\n\n // Retrieve a list of images available in given project.\n public static List\u003cImage\u003e listImages(String projectId) throws IOException {\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests.\n try (https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ImagesClient.html client = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ImagesClient.html.create()) {\n https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ListImagesRequest.html request = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.ListImagesRequest.html.newBuilder()\n .setProject(projectId)\n .build();\n\n ArrayList\u003cImage\u003e images = Lists.newArrayList(client.list(request).iterateAll());\n\n System.out.printf(\"'%s' images has been retrieved successfully\", images.size());\n\n return images;\n }\n }\n }\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Python API\nreference documentation](/python/docs/reference/compute/latest).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from __future__ import annotations\n\n from collections.abc import Iterable\n\n from google.cloud import compute_v1\n\n\n def list_images(project_id: str) -\u003e Iterable[compute_v1.Image]:\n \"\"\"\n Retrieve a list of images available in given project.\n\n Args:\n project_id: project ID or project number of the Cloud project you want to list images from.\n\n Returns:\n An iterable collection of compute_v1.Image objects.\n \"\"\"\n image_client = compute_v1.ImagesClient()\n return image_client.list(project=project_id)\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=compute)."]]