当前位置: 首页 > 工具软件 > Swift-T > 使用案例 >

Swift源码分析----swift-object-info

公冶翰池
2023-12-01

感谢朋友支持本博客,欢迎共同探讨交流,由于能力和时间有限,错误之处在所难免,欢迎指正!

如果转载,请保留作者信息。
博客地址:http://blog.csdn.net/gaoxingnengjisuan
邮箱地址:dong.liu@siat.ac.cn

PS:最近没有登录博客,很多朋友的留言没有看见,这里道歉!还有就是本人较少上QQ,可以邮件交流。


获取object的全局数据信息;

if __name__ == '__main__':
    parser = OptionParser()
    parser.set_defaults(check_etag=True, swift_dir='/etc/swift')
    parser.add_option(
        '-n', '--no-check-etag',
        action="store_false", dest="check_etag",
        help="Don't verify file contents against stored etag")
    parser.add_option(
        '-d', '--swift-dir',
        help="Pass location of swift directory")

    options, args = parser.parse_args()

    if len(args) < 1:
        print "Usage: %s [-n] [-d] OBJECT_FILE" % sys.argv[0]
        sys.exit(1)

    print_object_info(args[0], check_etag=options.check_etag,
                      swift_dir=options.swift_dir)

def print_object_info(datafile, check_etag=True, swift_dir='/etc/swift'):
    if not os.path.exists(datafile) or not datafile.endswith('.data'):
        print "Data file doesn't exist"
        sys.exit(1)
    try:
        ring = Ring(swift_dir, ring_name='object')
    except Exception:
        ring = None
    fp = open(datafile, 'rb')
    metadata = read_metadata(fp)
    path = metadata.pop('name', '')
    content_type = metadata.pop('Content-Type', '')
    ts = metadata.pop('X-Timestamp', '')
    etag = metadata.pop('ETag', '')
    length = metadata.pop('Content-Length', '')
    if path:
        print 'Path: %s' % path
        account, container, obj = path.split('/', 3)[1:]
        print '  Account: %s' % account
        print '  Container: %s' % container
        print '  Object: %s' % obj
        obj_hash = hash_path(account, container, obj)
        print '  Object hash: %s' % obj_hash
    else:
        print 'Path: Not found in metadata'
    if content_type:
        print 'Content-Type: %s' % content_type
    else:
        print 'Content-Type: Not found in metadata'
    if ts:
        print 'Timestamp: %s (%s)' % (datetime.fromtimestamp(float(ts)), ts)
    else:
        print 'Timestamp: Not found in metadata'

    file_len = None
    if check_etag:
        h = md5()
        file_len = 0
        while True:
            data = fp.read(64 * 1024)
            if not data:
                break
            h.update(data)
            file_len += len(data)
        h = h.hexdigest()
        if etag:
            if h == etag:
                print 'ETag: %s (valid)' % etag
            else:
                print "Etag: %s doesn't match file hash of %s!" % (etag, h)
        else:
            print 'ETag: Not found in metadata'
    else:
        print 'ETag: %s (not checked)' % etag
        file_len = os.fstat(fp.fileno()).st_size

    if length:
        if file_len == int(length):
            print 'Content-Length: %s (valid)' % length
        else:
            print "Content-Length: %s doesn't match file length of %s" % (
                length, file_len)
    else:
        print 'Content-Length: Not found in metadata'
    print 'User Metadata: %s' % metadata
    if ring is not None:
        print 'Ring locations:'
        part, nodes = ring.get_nodes(account, container, obj)
        for node in nodes:
            print ('  %s:%s - /srv/node/%s/%s/%s.data' %
                   (node['ip'], node['port'], node['device'],
                    storage_directory('objects', part, obj_hash), ts))
        print
        print 'note: /srv/node is used as default value of `devices`, '\
              'the real value is set in object-server.conf '\
              'on each storage node.'
    fp.close()

 类似资料: