使用 Word for Java 2.x 的 MediaStore SDK 範例 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

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

使用 Word for Java 2.x 的 MediaStore SDK 範例

下列程式碼範例示範如何使用 AWS SDK for Java 2.x with MediaStore 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然 動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 CreateContainer

Java 2.x 的 SDK
注意

還有更多 on 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
注意

還有更多 on 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
注意

還有更多 on 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
注意

還有更多 on 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
注意

還有更多 on 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
注意

還有更多 on 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
注意

還有更多 on 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