Reputation: 2615
I have this ....
fileExt = {'png': "'Content-Type': 'image/png'", 'jpe': "'Content-Type':
'image/jpeg'", 'jpg': "'Content-Type': 'image/jpeg'",
'png': "Content-Type': 'image/png'", 'mp4': "'Content-Type': 'video/mpeg'",
'mp3': "'Content-Type': 'audio/mpeg'", '3gp': "'Content-Type': 'video/3gpp'",
'flv': "'Content-Type': 'video/x-flv'", 'flv': "'Content-Type': 'video/x-flv'"}
handler = fileExt.get(ext)
go = "'"+handler
key.copy(key.bucket, key.name, preserve_acl=True, metadata={go})
i'm getting this error...
Traceback (most recent call last):
File "s3ContentType.py", line 43, in <module>
key.copy(key.bucket, key.name, preserve_acl=True, metadata={go})
File "/task/__pips__/boto/s3/key.py", line 474, in copy
encrypt_key=encrypt_key)
File "/task/__pips__/boto/s3/bucket.py", line 723, in copy_key
headers = boto.utils.merge_meta(headers, metadata, provider)
File "/task/__pips__/boto/utils.py", line 172, in merge_meta
for k in metadata.keys():
AttributeError: 'set' object has no attribute 'keys'
But if i explicitly put a string inside the metadata brackets it works.
key.copy(key.bucket, key.name, preserve_acl=True, metadata={'Content-Type': 'image/png'})
What gives, how do i include a var inside the brackets
Upvotes: 2
Views: 484
Reputation: 20718
You are constructing two different objects here:
foo = {'a', 'b', 'c'}
is a set, while
bar = {'a': 'b', 'c': 'd'}
is a dictionary. You can of course reference variables when constructing a dict:
mimetype = "image/png"
bar = {'Content-Type': mimetype}
A dictionary maps keys to values (hence the :
), while the set is only a collection of distinct values. The funciton you're calling expects a dictionary, so you have to create one. If you have a dictionary and want to pass that (continuing from above), you can do that like this:
mimetype = "image/png"
bar = {'Content-Type': mimetype}
key.copy(key.bucket, key.name, preserve_acl=True, metadata=bar)
(note that the links go to python 3 documentation, but sets and dicts have not really changed since python 2)
For the edit: As the accepted answer said, you have to use a nested dictionary. The question is, why doesn't it work otherwise?
Otherwise, you're creating a set containing the string you previously stored in the fileExt dictionary, for example "Content-Type: 'image/png'". Python will not interpret this string, which is why it does not create a dictionary when you write {"Content-Type: 'image/png'"}
.
Upvotes: 2
Reputation: 127
You can use a nested dictionary as well for keeping file extensions and content like this
fileExt = {'png': {'Content-Type': 'image/png'}, 'jpe': {'Content-Type':'image/jpeg'},
'jpg': {'Content-Type': 'image/jpeg'}, 'png': {'Content-Type': 'image/png'},
'mp4': {'Content-Type': 'video/mpeg'}, 'mp3': {'Content-Type': 'audio/mpeg'},
'3gp': {'Content-Type': 'video/3gpp'}, 'flv': {'Content-Type': 'video/x-flv'},
'flv': {'Content-Type': 'video/x-flv'}}
handler = fileExt.get(ext)
then you will be directly using handler in metadata, like this
key.copy(key.bucket, key.name, preserve_acl=True, metadata=handler)
Upvotes: 1
Reputation: 239453
When you say {go}
you are actually creating a set
, not a dict
. And sets don't have keys.
print type({"Welcome"})
print type({"1":"Welcome"})
Output
<type 'set'>
<type 'dict'>
But when you say {'Content-Type': 'image/png'}
, you are actually creating a dict
and thats why it works.
Upvotes: 1