如何将csv上传到s3存储桶中的文件夹中?

时间:2018-04-26 09:32:12

标签: python sql csv amazon-s3

我的要求是我想将csv文件从localhost上传到s3存储桶中的文件夹,但我不知道如何在下面的代码中给出文件夹名称。 有人可以帮我吗?

import boto3
import os

def upload_files(path):
    session = boto3.Session(
        aws_access_key_id='',
        aws_secret_access_key='',
        region_name='us-east-1'
    )
    s3 = session.resource('s3')
    bucket = s3.Bucket('ag-tpch')

    for subdir, dirs, files in os.walk(path):
        for file in files:
            full_path = os.path.join(subdir, file)
            with open(full_path, 'rb') as data:
                bucket.put_object(Key=full_path[len(path)+1:], Body=data)

if __name__ == "__main__":
    upload_files('E:/expenses/shape')

1 个答案:

答案 0 :(得分:0)

在您的代码中,您尝试将“E:/ expenses / shape”下的所有文件上传到S3。

我已将其更改为单个文件,您可以根据自己的要求进行修改。

import boto3
import os

def upload_file(path):
session = boto3.Session(
    aws_access_key_id='',
    aws_secret_access_key='',
    region_name='us-east-1'
)
s3 = session.resource('s3')
bucket = s3.Bucket('ag-tpch')

with open(path, 'rb') as data:
            bucket.put_object(Key='some/path/to-s3/test1.csv'], Body=data)
//your s3 path will be /some/path/to-s3/test-x.csv
if __name__ == "__main__":
upload_files('E:/expenses/shape/test1.csv')