D'autres AWS SDK exemples sont disponibles dans le GitHub dépôt AWS Doc SDK Examples
Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.
À utiliser HeadObject
avec un AWS SDK ou CLI
Les exemples de code suivants montrent comment utiliserHeadObject
.
- CLI
-
- AWS CLI
-
La commande suivante permet de récupérer les métadonnées d'un objet dans un compartiment nommé
my-bucket
:aws s3api head-object --bucket
my-bucket
--keyindex.html
Sortie :
{ "AcceptRanges": "bytes", "ContentType": "text/html", "LastModified": "Thu, 16 Apr 2015 18:19:14 GMT", "ContentLength": 77, "VersionId": "null", "ETag": "\"30a6ec7e1a9ad79c203d05a589c8b400\"", "Metadata": {} }
-
Pour API plus de détails, voir HeadObject
la section Référence des AWS CLI commandes.
-
- Java
-
- SDKpour Java 2.x
-
Note
Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. Déterminez le type de contenu d'un objet.
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. * <p> * For more information, see the following documentation topic: * <p> * 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(); } /** * Retrieves the content type of an object stored in an Amazon S3 bucket. * * @param s3 an instance of the {@link S3Client} class, which is used to interact with the Amazon S3 service * @param bucketName the name of the S3 bucket where the object is stored * @param keyName the key (file name) of the object in the S3 bucket */ 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); } } }
Obtenez le statut de restauration d'un objet.
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(); } /** * Checks the restoration status of an Amazon S3 object. * * @param s3 an instance of the {@link S3Client} class used to interact with the Amazon S3 service * @param bucketName the name of the Amazon S3 bucket where the object is stored * @param keyName the name of the Amazon S3 object to be checked * @throws S3Exception if an error occurs while interacting with the Amazon S3 service */ 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); } } }
-
Pour API plus de détails, voir HeadObjectla section AWS SDK for Java 2.x APIRéférence.
-
- Ruby
-
- SDKpour Ruby
-
Note
Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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 # Example usage: def run_demo bucket_name = "amzn-s3-demo-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__
-
Pour API plus de détails, voir HeadObjectla section AWS SDK for Ruby APIRéférence.
-