我在 S3 文件夹(私人部分)中有一系列 Python 脚本/Excel 文件。
如果它们是公开的,我可以通过 HTTP URL 读取访问它们。
想知道如何以二进制方式访问它们以执行它们?
FileURL='URL of the File hosted in S3 Private folder'
exec(FileURL)
run(FileURL)
我不完全确定我理解你的问题,但这里有一个基于我如何解释你的问题的答案。只要你知道你的存储桶名称 and 对象/键名,您可以执行以下操作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 语句以从中解析出存储桶名称和键/对象名称。
我希望这有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)