Doc AWS SDK ExamplesWord リポジトリには、さらに多くの GitHub の例があります。 AWS SDK
翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
MediaStore for Java 2.x を使用したSDKの例
次のコード例は、 AWS SDK for Java 2.x MediaStore を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。
アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。
各例には、完全なソースコードへのリンクが含まれています。ここでは、コンテキストでコードを設定および実行する方法の手順を確認できます。
トピック
アクション
次のコード例は、CreateContainer
を使用する方法を示しています。
- Java 2.x のSDK
-
注記
GitHub には他にもあります。用例一覧を検索し、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); } } }
-
API の詳細については、CreateContainer AWS SDK for Java 2.x リファレンスの API を参照してください。
-
次のコード例は、DeleteContainer
を使用する方法を示しています。
- Java 2.x のSDK
-
注記
GitHub には他にもあります。用例一覧を検索し、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); } } }
-
API の詳細については、DeleteContainer AWS SDK for Java 2.x リファレンスの API を参照してください。
-
次のコード例は、DeleteObject
を使用する方法を示しています。
- Java 2.x のSDK
-
注記
GitHub には他にもあります。用例一覧を検索し、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(); } }
-
API の詳細については、DeleteObject AWS SDK for Java 2.x リファレンスの API を参照してください。
-
次のコード例は、DescribeContainer
を使用する方法を示しています。
- Java 2.x のSDK
-
注記
GitHub には他にもあります。用例一覧を検索し、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 ""; } }
-
API の詳細については、DescribeContainer AWS SDK for Java 2.x リファレンスの API を参照してください。
-
次の例は、GetObject
を使用する方法を説明しています。
- Java 2.x のSDK
-
注記
GitHub には他にもあります。用例一覧を検索し、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(); } }
-
API の詳細については、GetObject AWS SDK for Java 2.x リファレンスの API を参照してください。
-
次のコード例は、ListContainers
を使用する方法を示しています。
- Java 2.x のSDK
-
注記
GitHub には他にもあります。用例一覧を検索し、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); } } }
-
API の詳細については、ListContainers AWS SDK for Java 2.x リファレンスの API を参照してください。
-
次のコード例は、PutObject
を使用する方法を示しています。
- Java 2.x のSDK
-
注記
GitHub には他にもあります。用例一覧を検索し、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(); } }
-
API の詳細については、PutObject AWS SDK for Java 2.x リファレンスの API を参照してください。
-