How to read binary file on S3 using boto?

老子叫甜甜 提交于 2019-12-23 17:21:38

问题


I have a series of Python Script / Excel File in S3 folder (Private section). I can read access them through HTTP URL if they are public.

Wondering how I can access in binary them for executing them ?

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

回答1:


I'm not totally sure I understood your question, but here is one answer based on how I interpreted your question. As long as you know your bucket name and object/key name, you can do the following with boto3 (and maybe with boto, too, although I'm unsure):

#! /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'])

By the way, from your PUBLIC URL, you can add Python statements to parse out the bucket name and key/object name from it.

I hope this helps.



来源:https://stackoverflow.com/questions/40955662/how-to-read-binary-file-on-s3-using-boto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!