Here's how they do它awscli:
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)
,你可以只用代碼使用這樣
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)
這並沒有解決與S3鍵爲file like object交互的目標,但它是在該方向邁出的一步。
你需要的文件是私有的或者只是最簡單的情況? – 2011-02-15 15:29:08
他們不需要是私人的。我只是想能夠使用URL在我的代碼中訪問它們。我想我真正需要的是一個抽象層,允許我使用的URL文件的工作,不管是一個Python庫或類似保險絲的東西,但它支持的URL,而不是本地文件路徑。 – 2011-02-15 18:50:18