-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestmeta.py
More file actions
executable file
·60 lines (49 loc) · 2.4 KB
/
Copy pathtestmeta.py
File metadata and controls
executable file
·60 lines (49 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
import datetime
from collections import defaultdict
from csv import DictReader
import logging
import mimetypes
import os
import pprint
import re
import sys
from boto.s3.connection import S3Connection
from boto.s3.key import Key
logging.basicConfig(level=logging.INFO)
src_bucket_name = 'nasavideos-shentonfreud' # mistyped my name :-(
src_key_name = 'nasavideos/media-01/18965236avi' # no leading /
dst_bucket_name = 'metadatatest-shentonfreude'
dst_key_name = '%s.wmv' % datetime.datetime.now()
dst_key_name = dst_key_name[:19].replace(' ', '_')
conn = S3Connection() # same as boot.connect_s3()
src_bucket = conn.lookup(src_bucket_name)
src_key = Key(src_bucket, src_key_name) # needs bucket-type and name string
logging.info('src_key .bucket=%s .name=%s' % (src_key.bucket.name, src_key.name))
# Gets nothing, why
logging.info('src content_type=%s metadata=%s size=%s' % (src_key.content_type, src_key.metadata, src_key.size))
# Metadata names must be HTTP Header compliant -- no 'separators' like colons; RFC2616
# Merge new meta with original
metadata = src_key.metadata # does this ever get anything
if metadata:
logging.info('src metadata=%s' % metadata)
metadata.update({'Content-Type': 'video/video', # WORKS, .content_type does not seem to
'dc-title' : 'dc-title',
'dc-author' : 'dc-author',
})
# I don't have to create the dst_key first, can do it on copy
dst_bucket = conn.create_bucket(dst_bucket_name)
dst_key = src_key.copy(dst_bucket_name, dst_key_name,
metadata=metadata, preserve_acl=True)
# Gets nothing, why?
# logging.info('x-amz-meta-dc-title=%s', dst_key.get_metadata('x-amz-meta-dc-title')) # sees metas here
# logging.info('dc-title=%s', dst_key.get_metadata('dc-title'))
# This does not work -- check_key doesn't get anything useful.
# Does get valid key with .size but no meta: cks = list(check_bucket.list())[-2]
# check_conn = S3Connection()
# check_bucket = check_conn.lookup(dst_bucket_name)
# check_key = Key(check_bucket, dst_key_name)
# logging.info('check_key size=%s metadata=%s' % check_key.size, check_key.metadata) # Gets None, why??
# logging.info('check_key get_metadata dc-title=%s' % check_key.get_metadata('dc-title'))
# logging.info('check_key get_metadata x-amz-meta-dc-title=%s' % check_key.get_metadata('x-amz-neta-dc-title'))
# import pdb; pdb.set_trace()