admin管理员组文章数量:1333442
I am writing a Python script using python-swiftclient
and zipfile
to zip and upload files to the Swift API endpoint of an Openstack Object Store. I store the zipped data in memory as an io.BytesIO
object.
Code snippet:
arc_name = 'test.zip'
zip_buffer = io.BytesIO()
with open zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, True) as zip_file:
for file in files:
with open(file, 'rb') as src_file:
zip_file.writestr(arc_name, src_file.read())
...
zip_data = zip_buffer.getvalue()
checksum_base64 = base64.b64encode(hashlib.md5(zip_data).digest()).decode()
swift_conn = swiftclient.Connection(<creds>)
container_name = 'swift-test'
swift_conn.put_object(container=container_name, contents=zip_data, content_type=None, obj=arc_name, etag=checksum_base64)
The error is:
swiftclient.exceptions.ClientException: Object PUT failed: https://..../swift/v1/swift-test/test.zip 422 Unprocessable Entity
From other HTTP 422 error questions my thinking is the issue is content_type
(MIME type). I've tried 'application/zip'
and 'multipart/mixed'
but always see the same error.
If another MIME type is more appropriate, or I'm missing something else, I'd be grateful for any help.
I am writing a Python script using python-swiftclient
and zipfile
to zip and upload files to the Swift API endpoint of an Openstack Object Store. I store the zipped data in memory as an io.BytesIO
object.
Code snippet:
arc_name = 'test.zip'
zip_buffer = io.BytesIO()
with open zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, True) as zip_file:
for file in files:
with open(file, 'rb') as src_file:
zip_file.writestr(arc_name, src_file.read())
...
zip_data = zip_buffer.getvalue()
checksum_base64 = base64.b64encode(hashlib.md5(zip_data).digest()).decode()
swift_conn = swiftclient.Connection(<creds>)
container_name = 'swift-test'
swift_conn.put_object(container=container_name, contents=zip_data, content_type=None, obj=arc_name, etag=checksum_base64)
The error is:
swiftclient.exceptions.ClientException: Object PUT failed: https://..../swift/v1/swift-test/test.zip 422 Unprocessable Entity
From other HTTP 422 error questions my thinking is the issue is content_type
(MIME type). I've tried 'application/zip'
and 'multipart/mixed'
but always see the same error.
If another MIME type is more appropriate, or I'm missing something else, I'd be grateful for any help.
Share Improve this question asked Nov 20, 2024 at 13:11 DaveDave 4607 silver badges13 bronze badges1 Answer
Reset to default 0I found the issue, and content_type
was a red herring. The issue was the etag
.
checksum_hash = hashlib.md5(zip_data)
checksum_string = checksum_hash.hexdigest()
checksum_base64 = base64.b64encode(checksum_hash.digest()).decode()
swift_conn.put_object(container=container_name,
contents=zip_data,
content_type=None,
obj=arc_name,
etag=checksum_base64) # fails
swift_conn.put_object(container=container_name,
contents=zip_data,
content_type=None,
obj=arc_name,
etag=checksum_string) # works
Also works with content_type='multipart/mixed'
, which is recommended with zip archives. So the upload was simply being rejected because of mismatched checksums.
The reason I erroneously used checksum_base64
was that coming from boto3
previously, the put_object
function call used ContentMD5=checksum_base64
.
本文标签: mime typesError 422 Unprocessable Entity errors with Openstack pythonswiftclientStack Overflow
版权声明:本文标题:mime types - Error 422: Unprocessable Entity errors with Openstack python-swiftclient - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742354375a2459120.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论