我的文件夹有以下树结构:
.
├── 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}
其中CCD_ 2&season_df
是我用于预处理数据的两个函数。
我要做的是读取Base_SLP
文件夹中的所有excel
文件,预处理每个文件,并使用s3存储桶中的文件夹中的to_csv
将它们保存为csv
。
当我将代码部署为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的答案,但仍然不起作用。
我还尝试将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
访问权限。
我做错了什么?
您似乎正在尝试将文件保存在/var/task
中。在lambda环境中,只有/tmp
可写入函数。
因此,您可以尝试以下操作:
save_name = '/tmp/' + file.split('.')[0] + '_' +
currentYear + '_timeseries.csv'