Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read binary file on S3 using boto?

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)
like image 305
tensor Avatar asked Oct 17 '22 21:10

tensor


1 Answers

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.

like image 57
NYCeyes Avatar answered Oct 20 '22 10:10

NYCeyes