def upload_file(file_obj, bucket, file_oid, object_md, make_public=False):
'''
Upload the file object to a bucket using credentials and object metadata.
Object name is a part of its metadata.
'''
if getpass.getuser() == 'bhs':
boto_cred_file = '/home/bhs/.boto'
else:
boto_cred_file = os.path.expanduser('~') + '/.boto'
fn = str(file_oid)
dest_uri = boto.storage_uri(bucket + '/' + fn, 'gs')
try:
new_key = dest_uri.new_key()
except boto.exception.NoAuthHandlerFound as e:
print e.message
return None
new_key.update_metadata(object_md)
try:
new_key.set_contents_from_file(file_obj)
if make_public:
new_key.make_public()
except boto.exception.GSResponseError as e:
# Do we have the credentials file set up?
if not os.path.exists(boto_cred_file):
print('Credentials file {} was not found.'.format(boto_cred_file))
return None
return str(dest_uri)
评论列表
文章目录