文件AWS開發套件範例 GitHub 儲存庫中有更多可用的 AWSSDK 範例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
GetObjectAnnotation 搭配 AWSSDK 使用
以下程式碼範例顯示如何使用 GetObjectAnnotation。
動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:
- Python
-
- 適用於 Python 的 SDK (Boto3)
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 def get_object_annotation( self, bucket_name: str, object_key: str, annotation_name: str, ) -> dict: """ Retrieves a specific annotation by name from an S3 object. :param bucket_name: The name of the bucket containing the object. :param object_key: The key of the object. :param annotation_name: The name of the annotation to retrieve. :return: A dict containing the annotation payload (decoded), ETag, ContentLength, and LastModified. :raises ClientError: If the annotation does not exist (NoSuchAnnotation) or another error occurs. """ try: response = self.s3_client.get_object_annotation( Bucket=bucket_name, Key=object_key, AnnotationName=annotation_name, ) payload = response["AnnotationPayload"].read().decode("utf-8") result = dict( Payload=payload, ETag=response.get("ETag", "N/A"), ContentLength=response.get("ContentLength", 0), LastModified=response.get("LastModified", None), ) logger.info( "Retrieved annotation '%s' from object '%s' in bucket '%s'.", annotation_name, object_key, bucket_name, ) return result except ClientError as err: if err.response["Error"]["Code"] == "NoSuchAnnotation": logger.error( "Annotation '%s' does not exist on object '%s' in bucket '%s'.", annotation_name, object_key, bucket_name, ) raise-
如需 API 詳細資訊,請參閱《適用於 AWSPython (Boto3) 的 SDK API 參考》中的 GetObjectAnnotation。
-
GetObjectAcl
GetObjectAttributes