aws s3 - 对象没有属性' server_side_encryption'

时间:2017-08-20 23:26:05

标签: python amazon-web-services amazon-s3 aws-sdk boto3

有人可以解释这两个电话的不同之处。第一个给出正确的server_side_encryption,第二个给出错误。其他属性给出相同的值 -

#!/usr/bin/python

import boto3
import botocore

s3 = boto3.resource('s3')
s3_client = boto3.client('s3')


object = s3.Object('abcdefhkjl9999','error.html')

print(object.bucket_name)
print(object.key)
print(object.last_modified)
print(object.storage_class)
print(object.server_side_encryption)


bucket = s3.Bucket('abcdefhkjl9999')
for object in bucket.objects.all():
    print("#############################")
    print(object.bucket_name)
    print(object.key)
    print(object.last_modified)
    print(object.storage_class)
    print(object.server_side_encryption)


The output is -
abcdefhkjl9999
error.html
2017-08-20 22:58:02+00:00
REDUCED_REDUNDANCY
aws:kms
#############################
abcdefhkjl9999
error.html
2017-08-20 22:58:02+00:00
REDUCED_REDUNDANCY
Traceback (most recent call last):
File "./test3.py", line 26, in <module>
print(object.server_side_encryption)
AttributeError: 's3.ObjectSummary' object has no attribute       'server_side_encryption'

2 个答案:

答案 0 :(得分:3)

当您收到的错误声明时,您尝试获取server_side_encryption属性的对象实际上不是s3.Object类型,而是类型s3.ObjectSummary }

幸运的是,您可以将对象作为子资源获取为specified here

inner = outer.Object() 然后查询属性

print(inner.server_side_encryption)

答案 1 :(得分:2)

s3.Object返回Object

bucket.objects返回ObjectSummary

对象具有这些属性

[u'Acl', u'Bucket', u'MultipartUpload', u'Version', u'accept_ranges', u'bucket_name', u'cache_control', u'content_disposition', u'content_encoding', u'content_language', u'content_length', u'content_type', 'copy', u'copy_from', u'delete', u'delete_marker', 'download_file', 'download_fileobj', u'e_tag', u'expiration', u'expires', u'get', 'get_available_subresources', u'initiate_multipart_upload', u'key', u'last_modified', 'load', 'meta', u'metadata', u'missing_meta', u'parts_count', u'put', 'reload', u'replication_status', u'request_charged', u'restore', u'restore_object', u'server_side_encryption', u'sse_customer_algorithm', u'sse_customer_key_md5', u'ssekms_key_id', u'storage_class', 'upload_file', 'upload_fileobj', u'version_id', u'wait_until_exists', u'wait_until_not_exists', u'website_redirect_location']

ObjectSummary具有这些属性

[u'Acl', u'Bucket', u'MultipartUpload', u'Object', u'Version', u'bucket_name', u'copy_from', u'delete', u'e_tag', u'get', 'get_available_subresources', u'initiate_multipart_upload', u'key', u'last_modified', 'load', 'meta', u'owner', u'put', u'restore_object', u'size', u'storage_class', u'wait_until_exists', u'wait_until_not_exists']