如何使用boto读取S3上的二进制文件?

How to read binary file on S3 using boto?

我在 S3 文件夹(私人部分)中有一系列 Python 脚本/Excel 文件。 如果它们是 public.

,我可以通过 HTTP URL 读取它们

想知道如何以二进制形式访问它们以执行它们?

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

我不确定我是否理解您的问题,但根据我对您问题的解释,这里有一个答案。只要你知道你的 bucket nameobject/key name,你就可以使用 boto3(也许还有 boto,虽然我不确定):

#! /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 语句来解析存储桶名称和 key/object 名称。

希望对您有所帮助。