欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

openstack swift large object支持

程序员文章站 2024-02-20 16:43:16
...

Swift对大对象的支持,其大小限制的设置是在/etc/swift/swift.conf里面进行配置

(具体的可配置参数参见:https://github.com/openstack/swift/blob/master/etc/swift.conf-sample#L15

[swift-constraints]

max_file_size = 5368709122

默认设置(或者不设置)的情况下大小是5G。为了方便测试,我改成了小于2M的值。


1、创建两个container,一个用来存放文件的chunks--large_file_segments,另外一个用来存放合成之后的大对象--large_files。

2、上传文件chunks,我这里上传的是三个文件块:

for i in range(1,4):
    count = str(i)
    with open('''D:\\temp\\part00000'''+ count, 'rb') as f:
        fileData = f.read()
    print len(fileData)
    conn.request("PUT", "/v1/AUTH_test/large_file_segments/part00000"+str(i), fileData, headers)
    response = conn.getresponse()
    print response.status, response.reason
    print "part"+count+"is:\n"
    jsonData = response.getheaders()
    print jsonData, response.read()

每上传一个chunk就会返回一些object信息,其中etag会在后续的manifest里面使用到。

ps:在这里面,文件的打开方式最好使用二进制打开,不然,最后会发现上传的文件大小和本地文件的大小会不一致

3、创建manifest文件

根据swift的doc说明,manifest文件是一个json格式的文件,标签如下:

Key Description
path the path to the segment object (not including account) /container/object_name
etag (optional) the ETag given back when the segment object was PUT
size_bytes (optional) the size of the complete segment object in bytes
range (optional) the (inclusive) range within the object to use as a segment. If omitted, the entire object is used

我使用的manifest如下:

[
    {
        "path": "large_file_segments/part00001",
	"etag": "99fa4ab9367da597c28f9f24c60130d7",
	"size_bytes": 1562164
    },
    {
        "path": "large_file_segments/part00002",
	"etag": "e9fb0c4edd17818ff8015657bf29a5e9",
	"size_bytes": 1555586
    },
    {
        "path": "large_file_segments/part00003",
	"etag": "20157504203d5211974290d55db9d09b",
	"size_bytes": 238798
    }
]

4、上传manifest文件:

conn.request("PUT", "/v1/AUTH_test/large_files/object2.txt?multipart-manifest=put", fileData, headers) 
上传完成之后,就会在large_files的container里面合并生成一个object