将 HeadObject 与 Amazon SDK 或命令行工具结合使用 - Amazon Simple Storage Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

HeadObject 与 Amazon SDK 或命令行工具结合使用

以下代码示例演示如何使用 HeadObject

CLI
Amazon CLI

以下命令检索名为 my-bucket 的存储桶中对象的元数据:

aws s3api head-object --bucket my-bucket --key index.html

输出:

{ "AcceptRanges": "bytes", "ContentType": "text/html", "LastModified": "Thu, 16 Apr 2015 18:19:14 GMT", "ContentLength": 77, "VersionId": "null", "ETag": "\"30a6ec7e1a9ad79c203d05a589c8b400\"", "Metadata": {} }
  • 有关 API 详细信息,请参阅《Amazon CLI 命令参考》中的 HeadObject

Java
SDK for Java 2.x
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

确定对象的内容类型。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.HeadObjectRequest; import software.amazon.awssdk.services.s3.model.HeadObjectResponse; import software.amazon.awssdk.services.s3.model.S3Exception; /** * 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 GetObjectContentType { public static void main(String[] args) { final String usage = """ Usage: <bucketName> <keyName>> Where: bucketName - The Amazon S3 bucket name.\s keyName - The key name.\s """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String bucketName = args[0]; String keyName = args[1]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() .region(region) .build(); getContentType(s3, bucketName, keyName); s3.close(); } 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); } } }

获取对象的还原状态。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.HeadObjectRequest; import software.amazon.awssdk.services.s3.model.HeadObjectResponse; import software.amazon.awssdk.services.s3.model.S3Exception; public class GetObjectRestoreStatus { public static void main(String[] args) { final String usage = """ Usage: <bucketName> <keyName>\s Where: bucketName - The Amazon S3 bucket name.\s keyName - A key name that represents the object.\s """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String bucketName = args[0]; String keyName = args[1]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() .region(region) .build(); checkStatus(s3, bucketName, keyName); s3.close(); } 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 详细,请参阅《Amazon SDK for Java 2.x API 参考》中的 HeadObject

Ruby
适用于 Ruby 的 SDK
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

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 # Example usage: 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 详细,请参阅《Amazon SDK for Ruby API 参考》中的 HeadObject

有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将此服务与 Amazon SDK 结合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。