2016-12-04 55 views
2

我在S3文件夾(私人部分)中有一系列Python腳本/ Excel文件。 如果它們是公開的,我可以通過HTTP URL讀取它們。如何使用boto在S3上讀取二進制文件?

想知道如何在二進制中訪問它們以執行它們?

FileURL='URL of the File hosted in S3 Private folder' 
exec(FileURL) 
run(FileURL) 

回答

1

我不完全確定我理解你的問題,但這裏有一個基於我如何解釋你的問題的答案。只要你知道你的水桶名目標/鍵名,你可以做boto3以下(也許與博託,太,雖然我不確定):

#! /usr/bin/env python3 
# 
import boto3 
from botocore.exceptions import ClientError 

s3_bucket = 'myBucketName' 
s3_key  = 'myFileName' # Can be a nested key/file. 
aws_profile = 'IAM-User-with-read-access-to-bucket-and-key' 
aws_region = 'us-east-1' 

aws_session = boto3.Session(profile_name = aws_profile) 
s3_resource = aws_session.resource('s3', aws_region) 
s3_object = s3_resource.Bucket(s3_bucket).Object(s3_key) 

# In case nested key/file, get the leaf-name and use that as our local file name. 
basename = s3_key.split('/')[-1].strip() 
tmp_file = '/tmp/' + basename 
try: 
    s3_object.download_file(tmp_file) # Not .download_fileobj() 
except ClientError as e: 
    print("Received error: %s", e, exc_info=True) 
    print("e.response['Error']['Code']: %s", e.response['Error']['Code']) 

通過從您的PUBLIC URL中,您可以添加Python語句以從中解析出存儲桶名稱和密鑰/對象名稱。

我希望這會有所幫助。