<p><a href="https://github.com/aws/aws-cli/blob/e2295b022db35eea9fec7e6c5540d06dbd6e588b/awscli/customizations/s3/utils.py#L192-L216" rel="noreferrer">Here's how they do</a>在<a href="https://aws.amazon.com/cli/" rel="noreferrer">awscli</a>中:</p>
<pre class="lang-python prettyprint-override"><code>def find_bucket_key(s3_path):
"""
This is a helper function that given an s3 path such that the path is of
the form: bucket/key
It will return the bucket and the key represented by the s3 path
"""
s3_components = s3_path.split('/')
bucket = s3_components[0]
s3_key = ""
if len(s3_components) > 1:
s3_key = '/'.join(s3_components[1:])
return bucket, s3_key
def split_s3_bucket_key(s3_path):
"""Split s3 path into bucket and key prefix.
This will also handle the s3:// prefix.
:return: Tuple of ('bucketname', 'keyname')
"""
if s3_path.startswith('s3://'):
s3_path = s3_path[5:]
return find_bucket_key(s3_path)
</code></pre>
<p>你可以用这样的代码</p>
<pre class="lang-python prettyprint-override"><code>from awscli.customizations.s3.utils import split_s3_bucket_key
import boto3
client = boto3.client('s3')
bucket_name, key_name = split_s3_bucket_key(
's3://example-bucket-name/path/to/example.txt')
response = client.get_object(Bucket=bucket_name, Key=key_name)
</code></pre>
<p>这并没有将与s3键交互作为<a href="https://docs.python.org/2/library/stdtypes.html#bltin-file-objects" rel="noreferrer">file like object</a>的目标,但这是朝着这个方向迈出的一步。</p>