SWORDv3客户端库
sword3client的Python项目详细描述
SWORDv3客户端
这个客户机库将SWORDv3的所有基本特性作为Python API提供。在
示例用法
创建客户端的新实例
fromsword3clientimportSWORD3Clientclient=SWORD3Client()
有关所有可用操作的详细信息,请参见对象定义。常见用法有:
- 仅使用元数据创建对象:
- 使用包创建对象:
fromsword3commonimportconstantsfromsword3clientimportSWORD3Clientclient=SWORD3Client()package_path="/path/to/file.zip"digest={constants.DIGEST_SHA_256:"digest...."}SERVICE="http://example.com/service-document"withopen(package_path,"rb")asstream:response=client.create_object_with_package(SERVICE,stream,"test.zip",digest,content_type="application/zip",packaging=constants.PACKAGE_SWORDBAGIT,)
- 检索对象的状态:
fromsword3clientimportSWORD3Clientclient=SWORD3Client()OBJ_URL="http://example.com/object/1"status=client.get_object(OBJ_URL)
- 附加二进制文件:
fromsword3clientimportSWORD3Clientclient=SWORD3Client()OBJ_URL="http://example.com/object/1"file_path="/path/to/binary.bin"digest={sword3common.constants.DIGEST_SHA_256:"digest...."}withopen(file_path,"rb")asstream:response=client.add_binary(OBJ_URL,stream,"test.bin",digest)
- 删除对象:
fromsword3clientimportSWORD3Clientclient=SWORD3Client()OBJ_URL="http://example.com/object/1"response=client.delete_object(OBJ_URL)
- 通过引用创建对象:
fromsword3commonimportByReferencefromsword3clientimportSWORD3Clientclient=SWORD3Client()SERVICE="http://example.com/service-document"br=ByReference()br.add_file("http://example.com/file.pdf","file.pdf","application/pdf",True)response=client.create_object_by_reference(SERVICE,br)
- 分段上传大文件
fromioimportBytesIOfromsword3commonimportconstantsfromsword3clientimportSWORD3Clientclient=SWORD3Client()SERVICE="http://example.com/service-document"FILE_SIZE=1000000SEGMENT_COUNT=10SEGMENT_SIZE=100000DIGEST={constants.DIGEST_SHA_256:"digest...."}LARGE_FILE="/path/to/large/file.zip"# get the service document, which tells us important details on segmented uploadsservice_document=client.get_service(SERVICE)# initialise the upload, to get a temporary urlresp=client.initialise_segmented_upload(service_document,assembled_size=FILE_SIZE,segment_count=SEGMENT_COUNT,segment_size=SEGMENT_SIZE,digest=DIGEST)temporary_url=resp.location# send each segment to the temporary urlwithopen(LARGE_FILE,"rb")asf:foriinrange(SEGMENT_COUNT):segment=f.read(SEGMENT_SIZE)stream=BytesIO(segment)segment_response=client.upload_file_segment(temporary_url,stream,i)
- 检索有关分段上载的信息
fromsword3clientimportSWORD3Clientclient=SWORD3Client()# Temporary URL obtained from initialisation of segmented upload step (see above)TEMPORARY_URL="http://example.com/temporary_url"upload_status=client.segmented_upload_status(TEMPORARY_URL)print(upload_status.received)print(upload_status.expecting)print(upload_status.size)print(upload_status.segment_size)
- 存放分段上传的文件
fromsword3clientimportSWORD3Clientclient=SWORD3Client()SERVICE="http://example.com/service-document"# Temporary URL obtained from initialisation of segmented upload step (see above)TEMPORARY_URL="http://example.com/temporary_url"resp=client.create_object_with_temporary_file(SERVICE,TEMPORARY_URL,"test.zip","application/zip")
- 项目
标签: