【问题标题】:Unable to save a preprocessed data as csv to a folder in s3 bucket无法将预处理数据作为 csv 保存到 s3 存储桶中的文件夹
【发布时间】: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_holidaysseason_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


    【解决方案1】:

    您似乎正在尝试将文件保存在/var/task 中。在 lambda 环境中,只有 /tmp 可写入您的函数。

    因此,您可以尝试以下方法:

                save_name = '/tmp/' + file.split('.')[0] + '_' + \
                    currentYear + '_timeseries.csv'
    

    【讨论】:

    • 嘿@Marcin,谢谢你的帮助。我在方法 1 中尝试了您的建议,现在抛出以下错误:Parameter validation failed: Invalid bucket name "mybucket/folder": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$" or be an ARN matching the regex "^arn:(aws).*:s3:[a-z\-0-9]+:[0-9]{12}:accesspoint[/:][a-zA-Z0-9\-]{1,63}$"
    • @Junkrat 嗨。存储桶名称应为“mybucket”,不带“/文件夹”。如果您想将文件放在某个文件夹中,这应该是您保存密钥名称的一部分。例如s3.Object('mybucket', 'folder/<your_file_name>').upload_file('/tmp/' + save_name)
    猜你喜欢
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    相关资源
    最近更新 更多