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'
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.
@nueverest
This must help: http://www.dhimanvivek.com/boto/s3-add-metadata-cache-control-header-to-key
Most helpful comment
bucket.objects.all()returns a collection ofs3.ObjectSummarywhich has the fulls3.Objectas a sub-resource. The issue in this case is that thes3.ObjectSummarydoes not have themetadataattribute. You can access thes3.Objectsub-resource by calling.Object()on yours3.ObjectSummary. An updated version of your code might look like the following: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)orbucket.Object(KEY).