使用python在myBucket中上传CSV并在S3中读取文件使用python
问题描述:
如何将CSV文件从本地上传到我的S3存储桶并读取该scv文件。 请帮我一把。使用python在myBucket中上传CSV并在S3中读取文件使用python
bucket = aws_connection.get_bucket('mybucket')
#with this i am able to create bucket
folders = bucket.list("","/")
for folder in folders:
print folder.name
现在想要将csv上传到我的csv并读取该文件。
答
让你在使用boto2我建议搬到boto3 - 请参见下面的几个简单的例子
boto2
上传例如
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')
下载例子
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
上传例如
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')
s3.Object('mybucket', 'hello.txt').put(Body=open('/tmp/hello.txt', 'rb'))
或多个简单
import boto3
s3 = boto3.resource('s3')
s3.meta.client.upload_file('/tmp/hello.txt', 'mybucket', 'hello.txt')
下载例如
import boto3
s3 = boto3.resource('s3')
s3.meta.client.download_file('mybucket', 'hello.txt', '/tmp/hello.txt')
print(open('/tmp/hello.txt').read())
以及如何给予权限..如何使私人bucket.IF我的桶已经theres.How我可以使它私人? –
最好的是直接从aws控制台 –
与桶策略一起工作很好的答案。是否有你喜欢的原因 '''s3 = boto3.resource('s3') s3.meta.client.download_file('mybucket','hello.txt','/tmp/hello.txt')''' ' 而不是 '''s3 = boto3.client('s3') s3.upload_file('/tmp/hello.txt','mybucket','hello.txt')''' ? – Peter