确定对象的内容类型。
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);
# @param object [Aws::S3::Object] An Amazon S3 object.
def initialize(object)
@object = object
# 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
# 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."
run_demo if $PROGRAM_NAME == __FILE__