Upload 0 byte file to Amazon S3

2019-08-31 07:23发布

问题:

Is it possible to upload a 0 byte file to Amazon S3? My standard response would be to not allow it, but I have a site that would like to allow users to upload blank .txt files, which happen to be 0 bytes.

Amazon returns a Malformed XML response:

<Error>
<Code>MalformedXML</Code>
<Message>The XML you provided was not well-formed or did not validate against our published schema</Message>
<RequestId>234...</RequestId>
<HostId>2309fsdijflsd...w32r09s</HostId>
</Error>

I'm using boto==2.3.0 with Flask==0.8

回答1:

Yes, it's possible.

>>> import boto
>>> c = boto.connect_s3()
>>> b = c.lookup('mybucket')
>>> k = b.new_key('empty')
>>> k.set_contents_from_string('')
>>> for k in b:
       print k.name, k.size
...
empty 0
...
>>>

At least that works for me. The error you are getting suggests something else is going wrong.