AWSDocAWS SDKGitHub サンプルリポジトリには、さらに多くの SDK サンプルがあります
翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
AWS SDK を使用して、Amazon S3 バケット内のオブジェクトの存在とコンテンツタイプを調べる
次のコード例は、S3 バケット内のオブジェクトの存在とコンテンツタイプを調べる方法を示しています。
- Java
-
- SDK for Java 2.x
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 オブジェクトのコンテンツタイプを調べる
public static void getContentType (S3Client s3, String bucketName, String keyName) { try { HeadObjectRequest objectRequest = HeadObjectRequest.builder() .key(keyName) .bucket(bucketName) .build(); HeadObjectResponse objectHead = s3.headObject(objectRequest); String type = objectHead.contentType(); System.out.println("The object content type is "+type); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
オブジェクトの復元ステータスを取得する
public static void checkStatus(S3Client s3, String bucketName, String keyName) { try { HeadObjectRequest headObjectRequest = HeadObjectRequest.builder() .bucket(bucketName) .key(keyName) .build(); HeadObjectResponse response = s3.headObject(headObjectRequest); System.out.println("The Amazon S3 object restoration status is "+response.restore()); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
-
API の詳細については、AWS SDK for Java 2.xAPI HeadObjectリファレンスのを参照してください。
-
- Ruby
-
- SDK for Ruby
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 require "aws-sdk-s3" # Wraps Amazon S3 object actions. class ObjectExistsWrapper attr_reader :object # @param object [Aws::S3::Object] An Amazon S3 object. def initialize(object) @object = object end # Checks whether the object exists. # # @return [Boolean] True if the object exists; otherwise false. def exists? @object.exists? rescue Aws::Errors::ServiceError => e puts "Couldn't check existence of object #{@object.bucket.name}:#{@object.key}. Here's why: #{e.message}" false end end # Replace bucket name and object key with an existing bucket and object that you own. def run_demo bucket_name = "doc-example-bucket" object_key = "my-object.txt" wrapper = ObjectExistsWrapper.new(Aws::S3::Object.new(bucket_name, object_key)) exists = wrapper.exists? puts "Object #{object_key} #{exists ? 'does' : 'does not'} exist." end run_demo if $PROGRAM_NAME == __FILE__
-
API の詳細については、AWS SDK for RubyAPI HeadObjectリファレンスのを参照してください。
-
バケットからウェブサイト設定を削除する
バケットの存在を調べる