Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I upload a CSV file in myBucket and Read File in S3 AWS using Python

How do I upload a CSV file from my local machine to my AWS S3 bucket and read that CSV file?

bucket = aws_connection.get_bucket('mybucket')
#with this i am able to create bucket
folders = bucket.list("","/")
  for folder in folders:
  print folder.name

Now I want to upload csv into my csv and read that file.

like image 795
TB.M Avatar asked Nov 21 '16 11:11

TB.M


People also ask

How do I import a CSV file into an S3 bucket?

Navigate to All Settings > Raw Data Export > CSV Upload. Toggle the switch to ON. Select Amazon S3 Bucket from the dropdown menu. Enter your Access Key ID, Secret Access Key, and bucket name.

How do I read a csv file in S3 Pyspark?

Spark Read CSV file from S3 into DataFramecsv("path") or spark. read. format("csv"). load("path") you can read a CSV file from Amazon S3 into a Spark DataFrame, Thes method takes a file path to read as an argument.


1 Answers

So you're using boto2 -- I would suggest to move to boto3. Please see below some simple examples:

boto2

upload example

import boto 
from boto.s3.key import Key
bucket = aws_connection.get_bucket('mybucket')
k = Key(bucket)
k.key = 'myfile'
k.set_contents_from_filename('/tmp/hello.txt')

download example

import boto
from boto.s3.key import Key

bucket = aws_connection.get_bucket('mybucket')
k = Key(bucket)
k.key = 'myfile'
k. get_contents_to_filename('/tmp/hello.txt')

boto3

upload example

import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')
s3.Object('mybucket', 'hello.txt').put(Body=open('/tmp/hello.txt', 'rb'))

or simply

import boto3
s3 = boto3.resource('s3')
s3.meta.client.upload_file('/tmp/hello.txt', 'mybucket', 'hello.txt')

download example

import boto3
s3 = boto3.resource('s3')
s3.meta.client.download_file('mybucket', 'hello.txt', '/tmp/hello.txt')
print(open('/tmp/hello.txt').read())
like image 83
Frederic Henri Avatar answered Oct 11 '22 06:10

Frederic Henri