Boto3: s3 key not found in documentation

Created on 12 Sep 2015  路  7Comments  路  Source: boto/boto3

http://boto3.readthedocs.org/en/latest/guide/migrations3.html

as per the above documentation i got error

here is my code

import boto3

bucket = s3.Bucket('my-bucket')
for x in bucket.objects.all():
if x.key == "assets/js/dev.js":
x.put(Metadata={'Expires': 'Sun, 29 Mar 2020 00:00:00 GMT'})
print(x.metadata['Expires'])
x.put(ACL='public-read')

error
AttributeError: 's3.ObjectSummary' object has no attribute 'metadata'

closing-soon question

Most helpful comment

bucket.objects.all() returns a collection of s3.ObjectSummary which has the full s3.Object as a sub-resource. The issue in this case is that the s3.ObjectSummary does not have the metadata attribute. You can access the s3.Object sub-resource by calling .Object() on your s3.ObjectSummary. An updated version of your code might look like the following:

import boto3
import datetime

KEY = 'YOUR-KEY'
BUCKET = 'YOUR-BUCKET'

s3 = boto3.resource('s3')
bucket = s3.Bucket(BUCKET)
expire_time = datetime.datetime.now() + datetime.timedelta(days=5)

for summary in bucket.objects.all():
    if summary.key == KEY:
        target_object = summary.Object()
        target_object.put(Expires=expire_time)
        print(target_object.expires)

Could you try that out and let me know if you're still having problems?

Additionally, if you know the name and bucket of the object before hand, it is simpler to access it with s3.Object(BUCKET, KEY) or bucket.Object(KEY).

All 7 comments

bucket.objects.all() returns a collection of s3.ObjectSummary which has the full s3.Object as a sub-resource. The issue in this case is that the s3.ObjectSummary does not have the metadata attribute. You can access the s3.Object sub-resource by calling .Object() on your s3.ObjectSummary. An updated version of your code might look like the following:

import boto3
import datetime

KEY = 'YOUR-KEY'
BUCKET = 'YOUR-BUCKET'

s3 = boto3.resource('s3')
bucket = s3.Bucket(BUCKET)
expire_time = datetime.datetime.now() + datetime.timedelta(days=5)

for summary in bucket.objects.all():
    if summary.key == KEY:
        target_object = summary.Object()
        target_object.put(Expires=expire_time)
        print(target_object.expires)

Could you try that out and let me know if you're still having problems?

Additionally, if you know the name and bucket of the object before hand, it is simpler to access it with s3.Object(BUCKET, KEY) or bucket.Object(KEY).

Thanks @JordonPhillips. I also run your code snippet, it works for me.

Closing for now. Please reopen if you have any more information as we have not been able to reproduce.

@JordonPhillips

"Could you try that out and let me know if you're still having problems?"
Resulting file size is 0

If I run the following code, all the files have 0 filesize.

s3 = boto3.resource('s3')
cckey = 'cache-control'
cache_time = one_month
for obj in bucket.objects.all():
       if  obj.size == 0: # continue on directories
            continue
      object = obj.Object()
      object.put(Metadata={cckey :  ('max-age=%d, public' % (cache_time)) })

@JordonPhillips +1

@ediston Were you able to figure this out? I'm noticing the same 0 file size issue.

When I try to open one of my files in a separate tab the tab opens then immediately closes. After which point, chrome downloads the file. When I try to open the downloaded file it is empty.

It is not clear why setting cache-control causes this behavior.

Was this page helpful?
0 / 5 - 0 ratings