MediaStore exemples d'utilisation SDK pour Java 2.x - Exemples de code de l'AWS SDK

D'autres AWS SDK exemples sont disponibles dans le GitHub dépôt AWS Doc SDK Examples.

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

MediaStore exemples d'utilisation SDK pour Java 2.x

Les exemples de code suivants vous montrent comment effectuer des actions et implémenter des scénarios courants à l'aide du AWS SDK for Java 2.x with MediaStore.

Les actions sont des extraits de code de programmes plus larges et doivent être exécutées dans leur contexte. Alors que les actions vous montrent comment appeler des fonctions de service individuelles, vous pouvez les visualiser dans leur contexte dans leurs scénarios associés.

Chaque exemple inclut un lien vers le code source complet, où vous trouverez des instructions sur la façon de configurer et d'exécuter le code en contexte.

Rubriques

Actions

L'exemple de code suivant montre comment utiliserCreateContainer.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import software.amazon.awssdk.services.mediastore.MediaStoreClient; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.mediastore.model.CreateContainerRequest; import software.amazon.awssdk.services.mediastore.model.CreateContainerResponse; import software.amazon.awssdk.services.mediastore.model.MediaStoreException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateContainer { public static long sleepTime = 10; public static void main(String[] args) { final String usage = """ Usage: <containerName> Where: containerName - The name of the container to create. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String containerName = args[0]; Region region = Region.US_EAST_1; MediaStoreClient mediaStoreClient = MediaStoreClient.builder() .region(region) .build(); createMediaContainer(mediaStoreClient, containerName); mediaStoreClient.close(); } public static void createMediaContainer(MediaStoreClient mediaStoreClient, String containerName) { try { CreateContainerRequest containerRequest = CreateContainerRequest.builder() .containerName(containerName) .build(); CreateContainerResponse containerResponse = mediaStoreClient.createContainer(containerRequest); String status = containerResponse.container().status().toString(); while (!status.equalsIgnoreCase("Active")) { status = DescribeContainer.checkContainer(mediaStoreClient, containerName); System.out.println("Status - " + status); Thread.sleep(sleepTime * 1000); } System.out.println("The container ARN value is " + containerResponse.container().arn()); System.out.println("Finished "); } catch (MediaStoreException | InterruptedException e) { System.err.println(e.getMessage()); System.exit(1); } } }
  • Pour API plus de détails, voir CreateContainerla section AWS SDK for Java 2.x APIRéférence.

L'exemple de code suivant montre comment utiliserDeleteContainer.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import software.amazon.awssdk.services.mediastore.MediaStoreClient; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.mediastore.model.CreateContainerRequest; import software.amazon.awssdk.services.mediastore.model.CreateContainerResponse; import software.amazon.awssdk.services.mediastore.model.MediaStoreException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateContainer { public static long sleepTime = 10; public static void main(String[] args) { final String usage = """ Usage: <containerName> Where: containerName - The name of the container to create. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String containerName = args[0]; Region region = Region.US_EAST_1; MediaStoreClient mediaStoreClient = MediaStoreClient.builder() .region(region) .build(); createMediaContainer(mediaStoreClient, containerName); mediaStoreClient.close(); } public static void createMediaContainer(MediaStoreClient mediaStoreClient, String containerName) { try { CreateContainerRequest containerRequest = CreateContainerRequest.builder() .containerName(containerName) .build(); CreateContainerResponse containerResponse = mediaStoreClient.createContainer(containerRequest); String status = containerResponse.container().status().toString(); while (!status.equalsIgnoreCase("Active")) { status = DescribeContainer.checkContainer(mediaStoreClient, containerName); System.out.println("Status - " + status); Thread.sleep(sleepTime * 1000); } System.out.println("The container ARN value is " + containerResponse.container().arn()); System.out.println("Finished "); } catch (MediaStoreException | InterruptedException e) { System.err.println(e.getMessage()); System.exit(1); } } }
  • Pour API plus de détails, voir DeleteContainerla section AWS SDK for Java 2.x APIRéférence.

L'exemple de code suivant montre comment utiliserDeleteObject.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.mediastore.MediaStoreClient; import software.amazon.awssdk.services.mediastore.model.DescribeContainerRequest; import software.amazon.awssdk.services.mediastore.model.DescribeContainerResponse; import software.amazon.awssdk.services.mediastoredata.MediaStoreDataClient; import software.amazon.awssdk.services.mediastoredata.model.DeleteObjectRequest; import software.amazon.awssdk.services.mediastoredata.model.MediaStoreDataException; import java.net.URI; import java.net.URISyntaxException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DeleteObject { public static void main(String[] args) throws URISyntaxException { final String usage = """ Usage: <completePath> <containerName> Where: completePath - The path (including the container) of the item to delete. containerName - The name of the container. """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String completePath = args[0]; String containerName = args[1]; Region region = Region.US_EAST_1; URI uri = new URI(getEndpoint(containerName)); MediaStoreDataClient mediaStoreData = MediaStoreDataClient.builder() .endpointOverride(uri) .region(region) .build(); deleteMediaObject(mediaStoreData, completePath); mediaStoreData.close(); } public static void deleteMediaObject(MediaStoreDataClient mediaStoreData, String completePath) { try { DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder() .path(completePath) .build(); mediaStoreData.deleteObject(deleteObjectRequest); } catch (MediaStoreDataException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } private static String getEndpoint(String containerName) { Region region = Region.US_EAST_1; MediaStoreClient mediaStoreClient = MediaStoreClient.builder() .region(region) .build(); DescribeContainerRequest containerRequest = DescribeContainerRequest.builder() .containerName(containerName) .build(); DescribeContainerResponse response = mediaStoreClient.describeContainer(containerRequest); mediaStoreClient.close(); return response.container().endpoint(); } }
  • Pour API plus de détails, voir DeleteObjectla section AWS SDK for Java 2.x APIRéférence.

L'exemple de code suivant montre comment utiliserDescribeContainer.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.mediastore.MediaStoreClient; import software.amazon.awssdk.services.mediastore.model.DescribeContainerRequest; import software.amazon.awssdk.services.mediastore.model.DescribeContainerResponse; import software.amazon.awssdk.services.mediastore.model.MediaStoreException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DescribeContainer { public static void main(String[] args) { final String usage = """ Usage: <containerName> Where: containerName - The name of the container to describe. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String containerName = args[0]; Region region = Region.US_EAST_1; MediaStoreClient mediaStoreClient = MediaStoreClient.builder() .region(region) .build(); System.out.println("Status is " + checkContainer(mediaStoreClient, containerName)); mediaStoreClient.close(); } public static String checkContainer(MediaStoreClient mediaStoreClient, String containerName) { try { DescribeContainerRequest describeContainerRequest = DescribeContainerRequest.builder() .containerName(containerName) .build(); DescribeContainerResponse containerResponse = mediaStoreClient.describeContainer(describeContainerRequest); System.out.println("The container name is " + containerResponse.container().name()); System.out.println("The container ARN is " + containerResponse.container().arn()); return containerResponse.container().status().toString(); } catch (MediaStoreException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; } }
  • Pour API plus de détails, voir DescribeContainerla section AWS SDK for Java 2.x APIRéférence.

L'exemple de code suivant montre comment utiliserGetObject.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import software.amazon.awssdk.core.ResponseInputStream; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.mediastore.MediaStoreClient; import software.amazon.awssdk.services.mediastore.model.DescribeContainerRequest; import software.amazon.awssdk.services.mediastore.model.DescribeContainerResponse; import software.amazon.awssdk.services.mediastoredata.MediaStoreDataClient; import software.amazon.awssdk.services.mediastoredata.model.GetObjectRequest; import software.amazon.awssdk.services.mediastoredata.model.GetObjectResponse; import software.amazon.awssdk.services.mediastoredata.model.MediaStoreDataException; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class GetObject { public static void main(String[] args) throws URISyntaxException { final String usage = """ Usage: <completePath> <containerName> <savePath> Where: completePath - The path of the object in the container (for example, Videos5/sampleVideo.mp4). containerName - The name of the container. savePath - The path on the local drive where the file is saved, including the file name (for example, C:/AWS/myvid.mp4). """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String completePath = args[0]; String containerName = args[1]; String savePath = args[2]; Region region = Region.US_EAST_1; URI uri = new URI(getEndpoint(containerName)); MediaStoreDataClient mediaStoreData = MediaStoreDataClient.builder() .endpointOverride(uri) .region(region) .build(); getMediaObject(mediaStoreData, completePath, savePath); mediaStoreData.close(); } public static void getMediaObject(MediaStoreDataClient mediaStoreData, String completePath, String savePath) { try { GetObjectRequest objectRequest = GetObjectRequest.builder() .path(completePath) .build(); // Write out the data to a file. ResponseInputStream<GetObjectResponse> data = mediaStoreData.getObject(objectRequest); byte[] buffer = new byte[data.available()]; data.read(buffer); File targetFile = new File(savePath); OutputStream outStream = new FileOutputStream(targetFile); outStream.write(buffer); System.out.println("The data was written to " + savePath); } catch (MediaStoreDataException | IOException e) { System.err.println(e.getMessage()); System.exit(1); } } private static String getEndpoint(String containerName) { Region region = Region.US_EAST_1; MediaStoreClient mediaStoreClient = MediaStoreClient.builder() .region(region) .build(); DescribeContainerRequest containerRequest = DescribeContainerRequest.builder() .containerName(containerName) .build(); DescribeContainerResponse response = mediaStoreClient.describeContainer(containerRequest); return response.container().endpoint(); } }
  • Pour API plus de détails, voir GetObjectla section AWS SDK for Java 2.x APIRéférence.

L'exemple de code suivant montre comment utiliserListContainers.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.mediastore.MediaStoreClient; import software.amazon.awssdk.services.mediastore.model.Container; import software.amazon.awssdk.services.mediastore.model.ListContainersResponse; import software.amazon.awssdk.services.mediastore.model.MediaStoreException; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListContainers { public static void main(String[] args) { Region region = Region.US_EAST_1; MediaStoreClient mediaStoreClient = MediaStoreClient.builder() .region(region) .build(); listAllContainers(mediaStoreClient); mediaStoreClient.close(); } public static void listAllContainers(MediaStoreClient mediaStoreClient) { try { ListContainersResponse containersResponse = mediaStoreClient.listContainers(); List<Container> containers = containersResponse.containers(); for (Container container : containers) { System.out.println("Container name is " + container.name()); } } catch (MediaStoreException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • Pour API plus de détails, voir ListContainersla section AWS SDK for Java 2.x APIRéférence.

L'exemple de code suivant montre comment utiliserPutObject.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.mediastore.MediaStoreClient; import software.amazon.awssdk.services.mediastoredata.MediaStoreDataClient; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.services.mediastoredata.model.PutObjectRequest; import software.amazon.awssdk.services.mediastoredata.model.MediaStoreDataException; import software.amazon.awssdk.services.mediastoredata.model.PutObjectResponse; import software.amazon.awssdk.services.mediastore.model.DescribeContainerRequest; import software.amazon.awssdk.services.mediastore.model.DescribeContainerResponse; import java.io.File; import java.net.URI; import java.net.URISyntaxException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class PutObject { public static void main(String[] args) throws URISyntaxException { final String USAGE = """ To run this example, supply the name of a container, a file location to use, and path in the container\s Ex: <containerName> <filePath> <completePath> """; if (args.length < 3) { System.out.println(USAGE); System.exit(1); } String containerName = args[0]; String filePath = args[1]; String completePath = args[2]; Region region = Region.US_EAST_1; URI uri = new URI(getEndpoint(containerName)); MediaStoreDataClient mediaStoreData = MediaStoreDataClient.builder() .endpointOverride(uri) .region(region) .build(); putMediaObject(mediaStoreData, filePath, completePath); mediaStoreData.close(); } public static void putMediaObject(MediaStoreDataClient mediaStoreData, String filePath, String completePath) { try { File myFile = new File(filePath); RequestBody requestBody = RequestBody.fromFile(myFile); PutObjectRequest objectRequest = PutObjectRequest.builder() .path(completePath) .contentType("video/mp4") .build(); PutObjectResponse response = mediaStoreData.putObject(objectRequest, requestBody); System.out.println("The saved object is " + response.storageClass().toString()); } catch (MediaStoreDataException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static String getEndpoint(String containerName) { Region region = Region.US_EAST_1; MediaStoreClient mediaStoreClient = MediaStoreClient.builder() .region(region) .build(); DescribeContainerRequest containerRequest = DescribeContainerRequest.builder() .containerName(containerName) .build(); DescribeContainerResponse response = mediaStoreClient.describeContainer(containerRequest); return response.container().endpoint(); } }
  • Pour API plus de détails, voir PutObjectla section AWS SDK for Java 2.x APIRéférence.