AWS SDK を使用して Amazon S3 URIs を解析する - AWS SDK コードの例

Doc AWS SDK Examples リポジトリには、他にも SDK の例があります。 AWS GitHub

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK を使用して Amazon S3 URIs を解析する

次のコード例は、Amazon S3 の URI を解析してバケット名やオブジェクトキーなどの重要なコンポーネントを抽出する方法を示しています。

Java
SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

S3Uri クラスを使用して Amazon S3 の URI を解析します。

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.S3Uri; import software.amazon.awssdk.services.s3.S3Utilities; import java.net.URI; import java.util.List; import java.util.Map; /** * * @param s3Client - An S3Client through which you acquire an S3Uri instance. * @param s3ObjectUrl - A complex URL (String) that is used to demonstrate S3Uri * capabilities. */ public static void parseS3UriExample(S3Client s3Client, String s3ObjectUrl) { logger.info(s3ObjectUrl); // Console output: // 'https://s3.us-west-1.amazonaws.com/myBucket/resources/doc.txt?versionId=abc123&partNumber=77&partNumber=88'. // Create an S3Utilities object using the configuration of the s3Client. S3Utilities s3Utilities = s3Client.utilities(); // From a String URL create a URI object to pass to the parseUri() method. URI uri = URI.create(s3ObjectUrl); S3Uri s3Uri = s3Utilities.parseUri(uri); // If the URI contains no value for the Region, bucket or key, the SDK returns // an empty Optional. // The SDK returns decoded URI values. Region region = s3Uri.region().orElse(null); log("region", region); // Console output: 'region: us-west-1'. String bucket = s3Uri.bucket().orElse(null); log("bucket", bucket); // Console output: 'bucket: myBucket'. String key = s3Uri.key().orElse(null); log("key", key); // Console output: 'key: resources/doc.txt'. Boolean isPathStyle = s3Uri.isPathStyle(); log("isPathStyle", isPathStyle); // Console output: 'isPathStyle: true'. // If the URI contains no query parameters, the SDK returns an empty map. Map<String, List<String>> queryParams = s3Uri.rawQueryParameters(); log("rawQueryParameters", queryParams); // Console output: 'rawQueryParameters: {versionId=[abc123], partNumber=[77, // 88]}'. // Retrieve the first or all values for a query parameter as shown in the // following code. String versionId = s3Uri.firstMatchingRawQueryParameter("versionId").orElse(null); log("firstMatchingRawQueryParameter-versionId", versionId); // Console output: 'firstMatchingRawQueryParameter-versionId: abc123'. String partNumber = s3Uri.firstMatchingRawQueryParameter("partNumber").orElse(null); log("firstMatchingRawQueryParameter-partNumber", partNumber); // Console output: 'firstMatchingRawQueryParameter-partNumber: 77'. List<String> partNumbers = s3Uri.firstMatchingRawQueryParameters("partNumber"); log("firstMatchingRawQueryParameter", partNumbers); // Console output: 'firstMatchingRawQueryParameter: [77, 88]'. /* * Object keys and query parameters with reserved or unsafe characters, must be * URL-encoded. * For example replace whitespace " " with "%20". * Valid: * "https://s3.us-west-1.amazonaws.com/myBucket/object%20key?query=%5Bbrackets%5D" * Invalid: * "https://s3.us-west-1.amazonaws.com/myBucket/object key?query=[brackets]" * * Virtual-hosted-style URIs with bucket names that contain a dot, ".", the dot * must not be URL-encoded. * Valid: "https://my.Bucket.s3.us-west-1.amazonaws.com/key" * Invalid: "https://my%2EBucket.s3.us-west-1.amazonaws.com/key" */ } private static void log(String s3UriElement, Object element) { if (element == null) { logger.info("{}: {}", s3UriElement, "null"); } else { logger.info("{}: {}", s3UriElement, element); } }