【发布时间】:2021-01-02 02:24:45
【问题描述】:
我的文件夹的树结构如下:
.
├── All_seasons_combined_script.py
├── Base_SLP
│ ├── G0.xlsx
│ ├── G1.xlsx
│ ├── G2.xlsx
│ ├── G3.xlsx
│ ├── G4.xlsx
│ ├── G5.xlsx
│ ├── G6.xlsx
│ ├── H0.xlsx
│ ├── L0.xlsx
│ ├── L1.xlsx
│ └── L2.xlsx
├── Pipfile
├── Pipfile.lock
├── README.md
├── requirements.txt
└── slp_handler.py
我在slp_handler.py中有以下代码:
def yearly_slp_handler(event, context):
try:
COUNTRY = event['country']
currentYear = datetime.datetime.now().strftime("%Y")
HOLIDAYS = get_holidays(currentYear, COUNTRY)
excel_path = os.path.join(Path(), "Base_SLP")
print(excel_path)
os.chdir(excel_path)
absolute_path = Path(os.path.abspath(
os.path.dirname('Base_SLP'))).parent
print(absolute_path)
EXCEL_FILES = glob.glob('*.xlsx')
print(EXCEL_FILES)
for file in EXCEL_FILES:
time_series_df = season_df(file, HOLIDAYS)
save_name = file.split('.')[0] + '_' + \
currentYear + '_timeseries.csv'
time_series_df.to_csv(save_name, index=None)
s3 = boto3.resource('s3')
bucket_name = 'mybucket/folder'
s3.meta.client.upload_file(
Filename=save_name, Bucket=bucket_name, Key=save_name)
print('CSV dropped in the bucket folder.')
return {'Status': 200}
except Exception as e:
print(e)
return {'Status': 400}
get_holidays 和 season_df 是我用于预处理数据的几个函数。
我要做的是读取Base_SLP 文件夹中的所有excel 文件,对每个文件进行预处理并使用to_csv 将它们保存为csv 在s3 存储桶中的文件夹中。
当我将代码部署为lambda 并对其进行测试时,它给了我以下错误:
START RequestId: xxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxx Version: $LATEST
./Base_SLP
/var/task
['G0.xlsx', 'G1.xlsx', 'G2.xlsx', 'G3.xlsx', 'G4.xlsx', 'G5.xlsx', 'G6.xlsx', 'H0.xlsx', 'L0.xlsx', 'L1.xlsx', 'L2.xlsx']
[Errno 30] Read-only file system: 'G0_2020_timeseries.csv'
我希望将预处理文件转换为csv,然后必须将这个csv 放到s3 存储桶的文件夹中。我试过 onur 的answer,但还是不行。
我还尝试将csv 保存在/tmp/ 文件夹中,然后他们使用S3.Object.upload_file 将其从那里上传到s3 中的文件夹:
for file in EXCEL_FILES:
time_series_df = season_df(file, HOLIDAYS)
save_name = file.split('.')[0] + '_' + \
currentYear + '_timeseries.csv'
time_series_df.to_csv('/tmp/' + save_name, index=None)
print('DF converted to CSV.')
saving_file_path = os.path.join('folder', save_name)
print(saving_file_path)
# Create S3 object
s3 = boto3.resource('s3')
s3.Object('mybucket', 'folder').upload_file(
'/tmp/' + save_name)
print('CSV dropped in the bucket folder.')
return {'Status': 200}
但它抛出了以下错误:
Failed to upload /tmp/G0_2020_timeseries.csv to mybucket/folder/G0_2020_timeseries.csv: An error occurred (NoSuchBucket) when calling the PutObject operation: The specified bucket does not exist
存储桶和文件夹存在并具有公共访问权限。但是,它仍然抛出这个错误。
我的lambda 附加了AmazonS3FullAccess 访问权限。
我做错了什么?
【问题讨论】:
标签: python pandas amazon-web-services amazon-s3 aws-lambda