擅长:python、mysql、java
<p>对于那些想模拟类似于boto2方法的人,可以尝试</p>
<pre><code>import boto3
from cStringIO import StringIO
s3c = boto3.client('s3')
contents = 'My string to save to S3 object'
target_bucket = 'hello-world.by.vor'
target_file = 'data/hello.txt'
fake_handle = StringIO(contents)
# notice if you do fake_handle.read() it reads like a file handle
s3c.put_object(Bucket=target_bucket, Key=target_file, Body=fake_handle.read())
</code></pre>
<hr/>
<p><strong>对于Python3:</strong></p>
<p>在Python3中都是<a href="https://docs.python.org/3.0/whatsnew/3.0.html" rel="noreferrer">StringIO and cStringIO are gone</a>。使用<code>StringIO</code>导入方式如下:</p>
<pre><code>from io import StringIO
</code></pre>
<p>要同时支持这两个版本:</p>
<pre><code>try:
from StringIO import StringIO
except ImportError:
from io import StringIO
</code></pre>