utils.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:dbs-back 作者: Beit-Hatfutsot 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号